0
点赞
收藏
分享

微信扫一扫

LintCode 题目:找数字

吃面多放酱 2022-06-29 阅读 78

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;

即可:

LintCode 题目:找数字_i++

 

(1)通过率:100%(使用循环)

在代码段中添加:

for (int i = 0; i < nums.size(); i++) {
/* code */
if(nums[i]==k){
return true;
}
}
return false;

即可:

LintCode 题目:找数字_i++_02

 


举报

相关推荐

0 条评论