LeetCode - 原点にソートされた配列&&ロボットリターン(イージー)の二乗

ソートされた配列の977乗(簡単)

    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.

溶液

私の解決策

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;
    }
}

公式ソリューション

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;
    }
}

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

概要

この問題は二つの主要なアイデアがあります。

  • 最初は、ストレージ内部のB乗新しい配列要素を最初に考えることは、最も可能性があり、その後、Javaは、アレイBをソートするソートアルゴリズム独自のコール
  • 第二の方法は、次に、前者が大きい場合、私は開始インデックスを記録するデジタル配列Aが、デジタル記録終了添字jの第1の比較A [i]とA [j]は、絶対値で、新しいアレイBを作成します配列Bの終わりに元の正方形、私++は、サイクルBの端部が配列を返すまで、正方形テールB、Jの配列に別段の後者、および。

注:
1.第2の方法は、多分私はあまりにも多くの食べ物です、良い考えではありません!

原点657ロボットリターン(簡単)

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.

溶液

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);
    }
}

概要

この質問は振り出しに戻っどうかの動きの一連の後、ロボットの意味を決定することです。ちょうど2つのカウンタxとyを設定し、初期値は0です。あなたはロボットのx ++を移動すると、ダウンx--するのは、右y--上に、Y ++に残され、最終的にxとyは0、0を返しますが、true、そうでない場合はfalseであるかどうかを判断します。

注:
1; switch文の高効率
2.文字列に文字列が次に通過できるため、それぞれの文の文字列、
3は、論理AND&左が偽である場合、オペレータは、オペレータが権利をチェックします; &&短絡と、操作者は偽のままにすると、直接falseを戻し、操作者がショート回路と同様の権利を、チェックしません。

おすすめ

転載: www.cnblogs.com/victorxiao/p/11106772.html