LeetCode--Squares of a Sorted Array && Robot Return to Origin (Easy)

977. Squares of a Sorted Array (Easy)

    Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.

Example 1:

Input: [-4,-1,0,3,10]
Output: [0,1,9,16,100]

Example 2:

Input: [-7,-3,2,3,11]
Output: [4,9,9,49,121]
 
Note:

1 <= A.length <= 10000
-10000 <= A[i] <= 10000
A is sorted in non-decreasing order.

solution

My solution

class Solution {
    public int[] sortedSquares(int[] A) {
        int[] B = new int[A.length];
        int i = 0, j = A.length - 1;
        for (int k = A.length - 1; k >= 0; k--)
        {
            if (Math.abs(A[i]) > Math.abs(A[j]))
            {
                B[k] = A[i] * A[i];
                i++;
            }
            else
            {
                B[k] = A[j] * A[j];
                j--;
            }
        }
        return B;
    }
}

The official solution

class Solution {
    public int[] sortedSquares(int[] A) {
        int N = A.length;
        int[] ans = new int[N];
        for (int i = 0; i < N; ++i)
            ans[i] = A[i] * A[i];
        Arrays.sort(ans);  //排序,默认为自然顺序
        return ans;
    }
}

reference:
https://leetcode.com/problems/squares-of-a-sorted-array/solution/

to sum up

This problem There are two main ideas:

  • The first is the most likely to think first with a new array element squared B A storage inside, and then call the java own sorting algorithm to sort the array B.
  • The second method is to create a new array B, then a digital array A i recording start index, the digital recording end subscript j First Comparative A [i] and A [j] is the absolute value of, if the former is large, then the square of the former into the end of the array B, then i ++; otherwise the latter into an array of square tail B, J, and then, until the end of cycle B returns an array.

Notes:
1. The second method is not a good thought, maybe I am too much food! !

657. Robot Return to Origin (Easy)

There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.

The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.

Note: The way that the robot is "facing" is irrelevant. "R" will always make the robot move to the right once, "L" will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.

Example 1:

Input: "UD"
Output: true 
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.
 

Example 2:

Input: "LL"
Output: false
Explanation: The robot moves left twice. It ends up two "moves" to the left of the origin. We return false because it is not at the origin at the end of its moves.

solution

class Solution {
    public boolean judgeCircle(String moves) {
        int x = 0 , y = 0;
        for (char move : moves.toCharArray())
        {
            switch (move)
            {
                case 'U' : x++; break;
                case 'D' : x--; break;
                case 'L' : y++; break;
                case 'R' : y--; break;
            }
        }
        return (x == 0 && y == 0);
    }
}

to sum up

This question is to determine the meaning of the robot after a series of whether the move back to square one. Just set the two counters x and y, the initial values ​​are 0. If you move the robot x ++, down to x--, left on the y ++, right on y--, and finally determine whether x and y are 0, 0 returns true, otherwise false.

Notes:
a higher efficiency of the switch statement 1.;
2. string to the character array can then traverse the for-each statement string;
3. & logical AND, the operator if the left is false, then the operator checks the right ; && short circuit and, if the operator is left false, directly returns false, the operator does not check the right, similar to a short circuit;

Guess you like

Origin www.cnblogs.com/victorxiao/p/11106772.html