1. 题目描述(中等难度)

[warning] 34. 在排序数组中查找元素的第一个和最后一个位置

2. 解法一: 暴力解法

class Solution {
    public int[] searchRange(int[] nums, int target) {
        int[] res = new int[2];
        boolean flag = true;
        for(int i=0;i<nums.length;i++){
            if(flag && nums[i] == target){
                res[0] = i;
                flag = false;
            }
            if(!flag && nums[i] == target){
                res[1] = i;
            }
        }
        if(flag && res[0] == 0 && res[1] == 0){
            res[0] = -1;
            res[1] = -1;
        }
        return res;
    }
}
© gaohueric all right reserved,powered by Gitbook文件修订时间: 2021-12-08 23:22:22

results matching ""

    No results matching ""