Remove Duplicates from Sorted Array
The title requires returning the resulting array length, but does not allow the generation of new space, only on the original array, so the return length is equivalent to returning the processed array.
Because it is the sorted array, so the same number is adjacent, with the adjacent two pointers to represent, if not equal, nums[j++]=nums[i], that is, the corresponding position of the array equals the previous one. If equal, J does not change, equivalent to remove the same, the back of the array to move a position. Finally, fill in the last value of the array. and returns the array length.
"Jump Game" (Hard for me)
Using reach to indicate that an array of subscript I can reach the farthest subscript position, the cyclic judging condition I<=reach means I can reach, traverse 0 to N-1, see if the array of these positions can reach, if I>reach exit the loop i!=n, can not reach the end, otherwise, Traverse to the Last Exit loop when I=n, can reach the end.
Search in rotated Sorted Array
Left and Mid and right refer to the array value where the position resides.
Using the dichotomy method, the title shows that, after rotation, the largest must not be on the right, if mid<right, then the description is 6712345-like, for the ①mid<target<=right type, then target on the right side, left=mid+1; ② Otherwise, target is on the left, Right=mid-1;③ if mid>=right, then 3456712 class, for the Left<=target<mid type, then target on the left, Right=mid-1;④ otherwise , Target is on the right, left=mid+1;
Update mid until Left>right exits the loop.
Boundary value: Target==mid back to mid
Target==right should be left to move, ①
Target==left should move right ③
Search for a Range
The topic requires time complexity O (LOGN), noting that the time complexity of the binary search is O (logn), using binary search.
Consistent with the 33 question, the main thing is to get the position of the first and last identical elements.
Find the maximum position (find the smallest position)
while (Left<=right) {
Mid= (Left+right)/2;
if (nums[mid]<=target) {//equal and less than, all right half move
left=mid+1;
}
if (nums[mid]>target) {
Right=mid-1;
}
if (Nums[mid]==target)//Record The current moment, equal to the ID value of target, left>right exit loop, which is the rightmost ID value
Id=mid;
}
Maximum Subarray
The key to DP dynamic programming is to obtain a formula that describes the relationship between the current position and the previous position.
The maximum value of the i-1 is obtained, and the maximum value is calculated at the first. Find a relationship between two states
for (int i=1;i<nums.length;i++) {
Maxnow=math.max (Nums[i],maxnow+nums[i]); //********
Maxsofar=math.max (Maxsofar,maxnow);
}
return Maxsofar;
Maxnow and Maxnow+nums[i], because it is a continuous number and
Maxnow said that the maximum value of i-1, then the maximum value at the first is nums[i] or maxnow+nums[i] (must be calculated at the value of i). Then use the Maxsofar to record the maximum value before the cutoff to I.
Climbing Stairs-------DP
Dp[1]=1; There is a possibility of a step up the stage
dp[2]=2; Up to two steps with either A + or 22 possible
for (int i=3;i<=n;i++) {
DP[I]=DP[I-1]+DP[I-2]; Take 4 steps For example, either walk 2 steps from level 2nd, or walk 1 steps from level 3rd, there is dp[2]+dp[3] kind of possible.
}
Plus One
int add=1;
for (int i=digits.length-1;i>=0;i--) {
if (digits[i]+add==10) {//cannot be written as digits[i]==9 because it is equal to 9 if there is no rounding back, or not
digits[i]=0;
add=1;
}
else{
Digits[i]=digits[i]+add;
Add=0;
}
}
if (add==1) {
int res[]=new int[digits.length+1];
System.arraycopy (digits,0,res,1,digits.length); If 99 becomes 100, you need to add 1 more, one more space, use arr The Aycopy function copies the array.
Res[0]=1;
return res;
}
Else
return digits;
}
198 House Robber
Since two consecutive numbers cannot be added, divided into odd and even positions, the odd position is either the sum of the last odd points plus the value of the odd number or the last even value. The even position is the same. Use a, B to represent the two cases respectively and then return a maximum value.
Paths Unique
Section i,j, either from I-1,j or from i,j-1, that is num[i][j]=num[i-1][j]+num[i][j-1].
Note the initial value, row No. 0, and the value of column No. 0 are all 1.
279. Perfect Squares
S[0]=0
S[1]=1
s[2]=2
S[3]=3
S[4]=min (s[4-1]+1,s[4-4]+1) =1
S[17]=min (s[17-1]+1,s[17-4]+1,s[17-9]+1,s[17-16]+1)
The number of complete squares to be obtained and for N, i.e., and for n-1,n-4,n-9 ... The minimum value of the value + 1;
134. Gas station
Suppose starting from 0, if Sum=sum+gas-cost>0 end indicates the place to reach is B end++;
If sum<0 indicates that a point between 0 and B cannot be reached from 0 to B (since 0 can reach a point prior to B, the sum of 0 to that point is greater than 0, minus and then more unreachable) then the point to begin moves forward start=gas.length,start-- , if Sum<0 is unable to circle, return 1 if sum>0 &&start==gas.length is 0, return 0. If Sum>0 &&start!=gas.length, start from start and return to start
206. Reverse Linked List
!!!!!!!!!!! Use the idea of pointers to think about problems
Newhead=head; Newhead pointer pointing to head
Iterative methods:
Recursive method:
The idea is consistent, each time the old linked list of the head node is deleted, and the new linked list has been flipped head. Use Newhead to represent the head of the new list, head of the old linked list, head.next=newhead. Use NextNode to represent the next node of the head and save it temporarily.
237. Delete Node in a Linked List
The deleted node cannot find the previous node, so it is not possible to use the traditional method to connect the previous node to the latter node, so copy the node sing Woo the next node, then delete the next one.
A->b->c->d
If you want to remove B
Then change to A->c->c->d
Delete the second C
A->c->d accomplished
234. Palindrome Linked List
In the beginning, set of pointers fast and slow starting at the head.
1, 1, 2, 1, NULL
sf
(1) Move:fast pointer goes to the end, and slow goes to the middle.
1, 1, 2, 1 even
s F
1, 1, 2, 1->1 odd
s F
Make Left=head Right=slow to the left a little bit shorter (odd words)
(2) Reverse:the right half is reversed, and Newhead pointer becomes the 2nd head.
1-1 2 <-1
left newhead
(3) Compare:run the pointer left and Newhead together and Compare.
1-1 2 <-1