美文网首页
33.搜索旋转排序数组

33.搜索旋转排序数组

作者: HITZGD | 来源:发表于2018-10-21 18:47 被阅读0次

    题目
    假设按照升序排序的数组在预先未知的某个点上进行了旋转。

    ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。

    搜索一个给定的目标值,如果数组中存在这个目标值,则返回它的索引,否则返回 -1 。

    你可以假设数组中不存在重复的元素。

    你的算法时间复杂度必须是 O(log n) 级别。

    示例 1:
    输入: nums = [4,5,6,7,0,1,2], target = 0
    输出: 4

    示例 2:
    输入: nums = [4,5,6,7,0,1,2], target = 3
    输出: -1

    思路:
    好像就是用for循环查找就可以,,,

    #include <vector>
    using namespace std;
    class Solution {
    public:
        int search(vector<int>& nums, int target) {
            int result;
            for (int i = 0; i < nums.size(); i ++)
            {
                if (nums[i] == target)
                {
                    result = i;
                    return result;
                }
            }
            return -1;
        }
    };
    

    相关文章

      网友评论

          本文标题:33.搜索旋转排序数组

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