A non-empty zero-indexed array A consisting of N is given.
A LOGO turtle stands at (0,0) heading North. It moves A[0] steps forward and turns by 90 degrees clockwise. Then it moves A[1] steps forward and turns clockwise by 90 degrees. And so on.
For example, given:
A[0] = 1 A[1] = 3 A[2] = 2
A[3] = 5 A[4] = 4 A[5] = 4
A[6] = 6 A[7] = 3 A[8] = 2
The turtle walks as follows:
(0,0) -> (0,1) 1st move, 1 step North
(0,1) -> (3,1) 2nd move, 3 steps East
(3,1) -> (3,-1) 3rd move, 2 steps South
(3,-1) -> (-2,-1) 4th move, 5 steps West
(-2,-1) -> (-2,3) 5th move, 4 steps North
(-2,3) -> (2,3) 6th move, 4 steps East
(2,3) -> (2,-3) 7th move, 6 steps South
(2,-3) -> (-1 3) 8th move, 3 steps West
(-1,-3) -> (-1,-1) 9th move, 2 steps North
In the 7th and 9th moves the turtle touches its previous path, namely:
at point (2,1) in the 7th move,
at point (2,-1) in the 7th move,
at point (-1,-1) in the 9th move
Write a function:
class Solution { public int solution(int[] A); }
that, given a description of the turtle's walk in array A, returns the number of the first move in which the turtle touches its previous path, or 0 if no such situation occurs.
For example, given array A as defined above, the function should return 7, because the turtle touches its previous path at point (2,1) in the 7th move.
Assume that:
N is an integer within the range [1..100,000];
each element of array A is an integer within the range [1..1,000,000].
Complexity:
expected worst-case time complexity is O(N);
expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.
A non-empty zero-indexed array A consisting of N is given.A LOGO turtle stands at (0,0) heading North. It moves A[0] steps forward and turns by 90 degrees clockwise. Then it moves A[1] steps forward and turns clockwise by 90 degrees. And so on.For example, given: A[0] = 1 A[1] = 3 A[2] = 2 A[3] = 5 A[4] = 4 A[5] = 4 A[6] = 6 A[7] = 3 A[8] = 2The turtle walks as follows: (0,0) -> (0,1) 1st move, 1 step North (0,1) -> (3,1) 2nd move, 3 steps East (3,1) -> (3,-1) 3rd move, 2 steps South (3,-1) -> (-2,-1) 4th move, 5 steps West (-2,-1) -> (-2,3) 5th move, 4 steps North (-2,3) -> (2,3) 6th move, 4 steps East (2,3) -> (2,-3) 7th move, 6 steps South (2,-3) -> (-1 3) 8th move, 3 steps West (-1,-3) -> (-1,-1) 9th move, 2 steps NorthIn the 7th and 9th moves the turtle touches its previous path, namely: at point (2,1) in the 7th move, at point (2,-1) in the 7th move, at point (-1,-1) in the 9th moveWrite a function:class Solution { public int solution(int[] A); }that, given a description of the turtle's walk in array A, returns the number of the first move in which the turtle touches its previous path, or 0 if no such situation occurs.For example, given array A as defined above, the function should return 7, because the turtle touches its previous path at point (2,1) in the 7th move.Assume that:N is an integer within the range [1..100,000];each element of array A is an integer within the range [1..1,000,000].Complexity:expected worst-case time complexity is O(N);expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).Elements of input arrays can be modified.
การแปล กรุณารอสักครู่..

อาร์เรย์ที่ไม่ว่างเปล่าเป็นศูนย์การจัดทำดัชนีที่ประกอบด้วย N จะได้รับ.
เต่า LOGO ยืนอยู่ที่ (0,0) มุ่งไปทางเหนือ มันย้าย [0] ก้าวไปข้างหน้าและจะเปิด 90 องศาตามเข็มนาฬิกา จากนั้นก็ย้าย [1] ก้าวไปข้างหน้าและจะเปิดตามเข็มนาฬิกา 90 องศา . และอื่น ๆ
ตัวอย่างเช่นกำหนด:
[0] = 1 A [1] = 3 A [2] = 2
[3] = 5 [4] = 4 [5] = 4
[6] = 6 [7] = 3 A [8] = 2
เต่าเดินดังนี้
(0,0) -> (0,1) ย้ายที่ 1 ขั้นตอนที่ 1 นอร์ท
(0,1) -> (3,1) ครั้งที่ 2 ย้าย 3 ขั้นตอนตะวันออก
(3,1) -> (3, -1) ย้ายที่ 3 ขั้นตอนที่ 2 ภาคใต้
(3, -1) -> (-2, -1) ย้ายที่ 4, 5 ขั้นตอนในเวสต์
(-2, - 1) -> (-2.3) ย้าย 5, 4 ขั้นตอนที่นอร์ท
(-2.3) -> (2,3) ย้าย 6 ขั้นตอนที่ 4 ตะวันออก
(2,3) -> (2, -3) ย้ายที่ 7 6 ขั้นตอนใต้
(2, -3) -> (-1 3) ย้าย 8 ขั้นตอนที่ 3 เวสต์
(-1, -3) -> (-1, -1) ย้าย 9 ขั้นตอนที่ 2 นอร์ท
ในวันที่ 7 และ 9 เคลื่อนย้ายเต่าสัมผัสเส้นทางก่อนหน้านี้คือ:
ที่จุด (2,1) ในการย้ายที่ 7
ที่จุด (2, -1) ในการย้ายที่ 7
ที่จุด (-1, -1) ในการย้ายที่ 9
เขียน ฟังก์ชั่น
โซลูชั่นชั้น {int วิธีการแก้ปัญหาของประชาชน (int [] A); }
ว่าได้รับรายละเอียดของการเดินเต่าในอาร์เรย์ที่ส่งกลับจำนวนของย้ายครั้งแรกที่เต่าสัมผัสเส้นทางเดิมหรือ 0 หากไม่มีสถานการณ์เช่นนี้เกิดขึ้น.
ตัวอย่างเช่นกำหนดอาร์เรย์ตามที่กำหนดข้างต้นฟังก์ชั่น . ควรกลับ 7 เพราะเต่าสัมผัสเส้นทางก่อนหน้านี้ที่จุด (2,1) ในการย้ายที่ 7
สมมติว่า:
N เป็นจำนวนเต็มภายในช่วง [1..100,000];
องค์ประกอบของอาร์เรย์แต่ละเป็นจำนวนเต็มภายใน ช่วง [1..1,000,000].
ความซับซ้อน:
คาดว่าซับซ้อนเวลาที่เลวร้ายที่สุดคือ O (n);
คาดว่าพื้นที่ซับซ้อนเลวร้ายที่สุดกรณีเป็น O (1) นอกเหนือจากการจัดเก็บข้อมูลการป้อนข้อมูล (ไม่นับการจัดเก็บข้อมูลที่จำเป็นสำหรับการป้อนข้อมูลการขัดแย้ง).
องค์ประกอบของ การป้อนข้อมูลอาร์เรย์สามารถแก้ไขได้
การแปล กรุณารอสักครู่..
