Day 1 | 704. Binary Search | 27. Remove Element | 35. Search Insert Position | 34. First and Last Position of Element in Sorted Array
Day 2 | 977. Squares of a Sorted Array | 209. Minimum Size Subarray Sum | 59. Spiral Matrix II
Day 3 | 203. Remove Linked List Elements | 707. Design Linked List | 206. Reverse Linked List
Day 4 | 24. Swap Nodes in Pairs| 19. Remove Nth Node From End of List| 160.Intersection of Two Lists
Day 6 | 242. Valid Anagram | 349. Intersection of Two Arrays | 202. Happy Numbe | 1. Two Sum
Day 7 | 454. 4Sum II | 383. Ransom Note | 15. 3Sum | 18. 4Sum
Directory
LeetCode 344. Reverse String
Solution:
class Solution {
public void reverseString(char[] s) {
int left = 0;
int right = s.length - 1;
while(left < right){
s[left] ^= s[right];
s[right] ^= s[left];
s[left] ^= s[right];
left++;
right--;
}
}
}
class Solution {
public void reverseString(char[] s) {
int left = 0;
int right = s.length-1;
while(left < right){
char temp = s[right];
s[right] = s[left];
s[left] = temp;
right--;
left++;
}
}
}
Thought:
- We can adopt
Double Pointer Method
- Use
^=
to exchange the position of left and right
LeetCode 541. Reverse String II
Solution:
class Solution {
public String reverseStr(String s, int k) {
char[] ch = s.toCharArray();
for(int i = 0; i < ch.length; i+=2*k){
int left = i;
// determine the position of right
int right = Math.min(i+k-1, ch.length-1);
while(left < right){
ch[left] ^= ch[right];
ch[right] ^= ch[left];
ch[left] ^= ch[right];
left++;
right--;
}
}
return new String(ch);
}
}
Thought:
- Let
i
move forward2*k
each time - Notice that use
Math.min()
to determine the position of right - Use
new String(ch)
to convert char to String
LeetCode 剑指 Offer 05. 替换空格
Solution:
class Solution {
public String replaceSpace(String s) {
if(s.length() == 0 || s == null)
return s;
StringBuilder str = new StringBuilder();
// 扩充2倍空格的空间数量
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) == ' ')
str.append(" ");
}
// 没有空格则直接返回
if(str.length() == 0)
return s;
// 有空格则定义两个指针
int left = s.length() - 1;
s += str.toString();
int right = s.length() - 1;
char[] ch = s.toCharArray();
while(left >= 0){
if(ch[left] == ' '){
ch[right--] = '0';
ch[right--] = '2';
ch[right] = '%';
}else{
ch[right] = ch[left];
}
left--;
right--;
}
return new String(ch);
}
}
Thought:
- Expand the array space by 2 times the number of spaces
- Define two pointers, fill the new array from right to left
LeetCode 151. Reverse Words in a String
Solution:
class Solution {
public String reverseWords(String s) {
char[] ch = s.toCharArray();
// 1、remove the extra space at the beginning, end and middle
ch = removeSpace(ch);
// 2、reverse the whole String
reverseString(ch, 0, ch.length-1);
// 3、reverse each word
reverseEachWord(ch);
return new String(ch);
}
char[] removeSpace(char[] ch){
int slow = 0;
for(int fast= 0; fast < ch.length; fast++){
// use a fast pointer to remove all space
if(ch[fast] != ' '){
// add space behind each word
if(slow != 0)
ch[slow++] = ' ';
// When the `fast` pointer encounters a space or traverses to the end of the string, it proves that a word has been traversed.
while(fast < ch.length && ch[fast] != ' '){
ch[slow++] = ch[fast++];
}
}
}
// Create a new char array for resizing.
char[] newChar = new char[slow];
System.arraycopy(ch, 0, newChar, 0, slow);
return newChar;
}
void reverseString(char[] ch, int left, int right){
if (right >= ch.length) {
System.out.println("set a wrong right");
return;
}
while(left < right){
ch[left] ^= ch[right];
ch[right] ^= ch[left];
ch[left] ^= ch[right];
left++;
right--;
}
}
void reverseEachWord(char[] ch){
int left = 0;
// <= is to make the `right` always points to the position after the end of the word.
for(int right = 0; right <= ch.length; right++){
// When the `right` pointer reaches the end of the string or the space behind a word.
if(right == ch.length || ch[right]==' '){
reverseString(ch, left, right-1);
left = right + 1;
}
}
}
}
Thought:
- We need three steps to solving this question:
- Remove the extra space at the beginning, end and middle
Double Pointer Method
- Use the
fast
pointer to remove all space - Use the
slow
pointer to add a space before every word, excluding the first word. - When the
fast
pointer encounters a space or traverses to the end of the string, it proves that a word has been traversed. - Create a new char array for resizing.
- Reverse the whole string
- Reverse each word
<=
is to make theright
always points to the position after the end of the word.- When the
right
pointer rechees the end of the string or the space behind a word.
LeetCode 剑指 Offer 58 - II. 左旋转字符串
Solution:
class Solution {
public String reverseLeftWords(String s, int n) {
char[] ch = s.toCharArray();
// reverse the first n characters
reverseString(ch, 0, n-1);
// revere the characters after n
reverseString(ch, n, ch.length-1);
// reverse all characters
reverseString(ch, 0, ch.length-1);
return new String(ch);
}
void reverseString(char[] ch, int left, int right){
int slow = 0;
while(left < right){
ch[left] ^= ch[right];
ch[right] ^= ch[left];
ch[left] ^= ch[right];
left++;
right--;
}
}
}
Thought:
- reverse the first n characters
- revere the characters after n
- reverse all characters