C++算法零基础100题-搜索排序旋转数组

61 阅读1分钟

题目链接:33. 搜索旋转排序数组 - 力扣(LeetCode)

题目描述

image.png

解体思路

  1. 首先我们得到了一个旋转过后的数组
  2. 然后我们将这个数组for循环遍历。
  3. 如果说找到需要的元素了之后我们再返回结果
  4. 如果没有找到需要的元素我们就返回-1

代码实现

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