欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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;

即可:

LintCode 题目:找数字

 

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

在代码段中添加:

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

即可:

LintCode 题目:找数字