[GA] array - LCP 1. Guess

Topic :( power button (LeetCode) https://leetcode-cn.com/problems/guess-numbers)

A small play and a small B Guess. Small B per 1, 2, 3 from a randomly selected, each time selecting a small A guess from 1, 2, 3. They conducted a total of three times in this game, return to the small A guessed a few times?

guess input array A small each guess, answer choices for each small array B. guess and 3 are equal to the length of the answer.

Example 1:

Input: guess = [1,2,3], answer = [1,2,3]
Output: 3
Explanation: A small every guess.
 

Example 2:

Input: guess = [2,2,3], answer = [3,2,1]
Output: 1
Explanation: A small only a second guess.
 

limit:

3 the length of the guess =
answer length = 3
guess element values {1, 2, 3} one.
answer element values {1,, one 23}.

 

answer:

class Solution {
    public int game(int[] guess, int[] answer) {
        int count = 0;
        for (int i = 0; i < 3; i++) {
            if (guess[i] == answer[i]) {
                count++;
            }
        }
        return count;
    }
}

 

 Useful enumeration, from that [limit] can be seen

return (guess[0]==answer[0]?1:0)+(guess[1]==answer[1]?1:0)+(guess[2]==answer[2]?1:0);

So do not mind-set -

 

Guess you like

Origin www.cnblogs.com/utomboy/p/12579821.html