题目地址:
https://www.lintcode.com/problem/find-the-number/description
给定一个数组 A A A,判断 k k k是否在其中。
代码如下:
public class Solution {
/**
* @param nums: a integer array
* @param k: a integer
* @return: return true or false denoting if the element is present in the array or not
*/
public boolean findnNumber(int[] nums, int k) {
// write your code here
if (nums == null || nums.length == 0) {
return false;
}
for (int num : nums) {
if (num == k) {
return true;
}
}
return false;
}
}
时间复杂度 O ( l A ) O(l_A) O(lA),空间 O ( 1 ) O(1) O(1)。