34. Search for a Range

来源:互联网 发布:弯矩图绘制软件 编辑:程序博客网 时间:2024/04/29 18:27

题目:https://leetcode.com/problems/search-for-a-range/

代码:

public class Solution {    public int[] searchRange(int[] nums, int target) {        int[] position = {-1,-1};        for(int i=0;i<nums.length;i++)        {            if(nums[i]==target)            {                position[0] = i;                break;            }        }        if(position[0]==-1)            return position;        if(nums.length==1&&position[0]==0)        {            position[1] = 0;            return position;        }        int i;        for( i=position[0]+1;i<nums.length;i++)        {            if(nums[i]!=target)            {                position[1] = i-1;                break;            }        }        if(i==nums.length)            position[1] = i-1;        return position;    }}1ms
0 0
原创粉丝点击