LintCode 题目:找数字
程序员文章站
2022-03-24 23:45:58
...
URL:https://www.lintcode.com/problem/find-the-number/description
描述
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
(1)通过率:100%(使用函数)
在代码段中添加:
int n = count(nums.begin(),nums.end(),k);
if(n>0)
return true;
else
return false;
即可:
(1)通过率:100%(使用循环)
在代码段中添加:
for (int i = 0; i < nums.size(); i++) {
/* code */
if(nums[i]==k){
return true;
}
}
return false;
即可:
上一篇: 如何给asp,html,javascript,php添加注释语句
下一篇: 命令行解析