美文网首页
Leetcode面试题 08.03. 魔术索引

Leetcode面试题 08.03. 魔术索引

作者: LonnieQ | 来源:发表于2020-03-13 00:05 被阅读0次

题目

魔术索引。 在数组A[0...n-1]中,有所谓的魔术索引,满足条件A[i] = i。给定一个有序整数数组,编写一种方法找出魔术索引,若有的话,在数组A中找出一个魔术索引,如果没有,则返回-1。若有多个魔术索引,返回索引值最小的一个。

示例1:
 输入:nums = [0, 2, 3, 4, 5]
 输出:0
 说明: 0下标的元素为0
示例2:

 输入:nums = [1, 1, 1]
 输出:1
提示:

nums长度在[1, 1000000]之间

C++解法

#include <iostream>
#include <math.h>
#include <set>
#include <vector>
using namespace std;
class Solution {
public:
    int findMagicIndex(vector<int>& nums) {
        int length = (int)nums.size();
        for (int i = 0; i < length; ++i) if (nums[i] == i) return i;
        return -1;
    }
};

int main(int argc, const char * argv[]) {
    vector<int> nums {1, 2, 3, 5, 6, 5};
    cout << Solution().findMagicIndex(nums) << endl;
    return 0;
}

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/magic-index-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

相关文章

  • LeetCode 面试题 08.03. 魔术索引 | Pytho

    面试题 08.03. 魔术索引 题目来源:力扣(LeetCode)https://leetcode-cn.com/...

  • LeetCode 面试题 08.03. 魔术索引 | Pytho

    面试题 08.03. 魔术索引 题目来源:力扣(LeetCode)https://leetcode-cn.com/...

  • Leetcode面试题 08.03. 魔术索引

    题目 魔术索引。 在数组A[0...n-1]中,有所谓的魔术索引,满足条件A[i] = i。给定一个有序整数数组,...

  • 面试题 08.03. 魔术索引

    题意:魔术索引。 在数组A[0...n-1]中,有所谓的魔术索引,满足条件A[i] = i。给定一个有序整数数组,...

  • 【LeetCode】面试题 08.03. 魔术索引(每日一题7/

    打卡练手感,思想永不掉线 解题思路: 魔术索引的意思就是 索引和值相等(index == value)。 定义re...

  • LeetCode 魔术索引

    魔术索引。 在数组A[0...n-1]中,有所谓的魔术索引,满足条件A[i] = i。给定一个有序整数数组,编写一...

  • Day07-SQL存储引擎

    上节回顾 1. 聚集索引与辅助索引的区别?(面试题) 聚集索引构建B树过程(面试题) 辅助索引构建B树过程(面试题...

  • 魔术索引

    题目: 题目的理解: 循环判断数字是否与索引相等。 python实现 提交 难得有一个100%了。 // END ...

  • 魔术索引I

    在数组A[0..n-1]中,有所谓的魔术索引,满足条件A[i]=i。给定一个升序数组,元素值各不相同,编写一个方法...

  • 魔术索引II

    题目 在数组A[0..n-1]中,有所谓的魔术索引,满足条件A[i]=i。给定一个不下降序列,元素值可能相同,编写...

网友评论

      本文标题:Leetcode面试题 08.03. 魔术索引

      本文链接:https://www.haomeiwen.com/subject/caunjhtx.html