xxxxxxxxxx
class Solution {
public int maxSubArray(int[] nums) {
int currSum = 0;
int max = Integer.MIN_VALUE;
for (int i = 0; i < nums.length; i ++)
{
currSum = Math.max(currSum + nums[i], nums[i]);
max = Math.max(currSum, max);
}
return max;
}
}
xxxxxxxxxx
class Solution {
public int[] searchRange(int[] nums, int target) {
int res[] = new int[]{-1, -1};
int start = 0;
int last = nums.length-1;
while(start <= last)
{
int mid = start + (last - start)/2;
if(nums[mid] == target)
{
res[0] = mid;
last = mid - 1;
}
else if(nums[mid] < target)
{
start = mid+1;
}
else
{
last = mid -1;
}
}
start =0;
last = nums.length-1;
while(start <= last)
{
int mid = start + (last - start)/2;
if(nums[mid] == target)
{
res[1] = mid;
start = mid + 1;
}
else if(nums[mid] < target)
{
start = mid+1;
}
else
{
last = mid -1;
}
}
return res;
}
}