79. Word Search
怎么情人节碰到的第一道题目就那么麻烦。。。无语。。具体参考链接里面的了
http://www.cnblogs.com/zslhq/p/6004604.html
80. Remove Duplicates from Sorted Array II
这道题用到两个index,主index不动,让小index往前跑,如果值相同一直往前跑,遇到值不同的地方停下,然后主index等与小index。
public class Solution {
public int removeDuplicates(int[] nums) {
if (nums == null) {
return 0;
}
int curr = 0;
int i,j;
for (i = 0; i < nums.length;) {
int nowval = nums[i];
for (j = i; j < nums.length; j++) {
if (nums[j] != nowval ) {
break;
}
if (j - i < 2) {
nums[curr++] = nowval;
}
}
i = j;
}
return curr;
}
}
81. Search in Rotated Sorted Array II
和Search in Rotated Sorted Array I差不多,主要是当等于mid时候要移动index,然后和前面一样用二分法解决。
public class Solution {
public boolean search(int[] nums, int target) {
int left=0;
int right=nums.length-1;
while(left<=right){
int mid = (left+right)/2;
if(nums[mid]==target)
return true;
if(nums[left]<nums[mid]){
if(nums[left]<=target && target<nums[mid]){
right=mid-1;
}else{
left=mid+1;
}
}else if(nums[left]>nums[mid]){
if(nums[mid]<target&&target<=nums[right]){
left=mid+1;
}else{
right=mid-1;
}
}else{
left++;
}
}
return false;
}
}