LintCode 1505. 找数字 Java算法

描述

Given an unsorted array of n elements, find if the element k is present in the array or not.
Complete the findnNumber function in the editor below.It has 2 parameters:
1 An array of integers, arr, denoting the elements in the array.
2 An integer, k, denoting the element to be searched in the array.
The function must return true or false denoting if the element is present in the array or not

说明

  • 1 <= n <= 10^5
  • 1 <= arr[i] <= 10^9

样例

- Example:
Input:
arr = [1, 2, 3, 4, 5]
k = 1
Output: true

解析

public class Solution {
    public boolean findnNumber(int[] nums, int k) {
        for (int num : nums) {
            if (num == k) {
                return true;
            }
        }
        return false;
    }
}

运行结果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/SmallTeddy/article/details/108447368