LeetCode (LCP 01)--Guess the number

topic

LCP 01. Guess the number

Little A and Little B are playing guessing numbers. Little B randomly chooses one from 1, 2, 3 each time, and Little A also chooses one guess from 1, 2, 3 each time. They played this game three times in total. Please go back to Little A. How many times did you guess it right?
The input guess array is the guess made by Little A each time, and the answer array is the choice made by Little B each time. The length of guess and answer are both equal to 3.

Example 1:

输入:guess = [1,2,3], answer = [1,2,3]
输出:3
解释:小A 每次都猜对了。

Example 2:

输入:guess = [2,2,3], answer = [3,2,1]
输出:1
解释:小A 只猜对了第二次

limit:

The length of guess = 3
The length of answer = 3
The element of guess is one of {1, 2, 3}.
The element of answer is one of {1, 2, 3}.

Problem solution (Java)

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

Guess you like

Origin blog.csdn.net/weixin_46841376/article/details/113881027