LC 344 Reverse String
Link:
https://leetcode.com/problems/reverse-string/description/
It's a very easy problem using double pointer. Just don't directly use the library.
class Solution {
public void reverseString(char[] s) {
int i=0;
int j=s.length-1;
while(j>i){
char tmp=s[i];
s[i]=s[j];
s[j]=tmp;
i++;
j--;
}
}
}
LC 541 Reverse String II
Link:https://leetcode.com/problems/reverse-string-ii/
It's basically an upgrade of the first problem, yet I made a mistake:
this was my original code:
class Solution {
public String reverseStr(String s, int k) {
char[] arr = s.toCharArray();
int i = 0;
int j = k - 1;
while (i < arr.length ) {
while (i < j) {
char c = arr[i];
arr[i] = arr[j];
arr[j] = c;
i++;
j--;
}
i += 2 * k;
j = Math.min(arr.length - 1, j + 2 * k);
}
return new String(arr);
}
}
Obviously, the problem was I ignored that in the inner loop, i and j are modified and couldn't return to its initial values( I have to add another two parameters)
But I can just simply add a method, it will simpify the code and makes everything ok
class Solution {
public String reverseStr(String s, int k) {
char[] arr = s.toCharArray();
int i = 0;
while (i < arr.length) {
int j = j = Math.min(arr.length - 1, i + k - 1);
reverse(arr, i, j);
i += 2 * k;
}
return new String(arr);
}
void reverse(char[] array, int start, int end) {
while (start < end) {
char c = array[start];
array[start] = array[end];
array[end] = c;
start++;
end--;
}
}
}
KAMA54 Replace numbers
Linkhttps://kamacoder.com/problempage.php?pid=1064
The thing is, I don't know how to do it using double pointer in c++, and I either don't know how to create a dynamic-sized string, I just have to follow the answer,
learned:
scanner is used to read user's data, system.in means the data user puts in;
nextline is the line of string user puts in;
stringbuilder is a dynamic-sized string in order to put in uncertain number of characters;
isDigit;charAt;append method;
import java.util.Scanner;
class Main{
public static void main(String[] args){
Scanner in =new Scanner(System.in);
String s= in.nextLine();
StringBuilder sb=new StringBuilder();
for(int i=0;i<s.length();i++){
if(Character.isDigit(s.charAt(i))){
sb.append("number");
}
else sb.append(s.charAt(i));
}
System.out.println(sb);
}
}
LC 151 reverse words in a string
https://leetcode.com/problems/reverse-words-in-a-string/description/
I have to admit I don't know how to do this, I can reverse the whole string, yet how to remove the spaces is tricky, and the solution gives the double pointer method, reverse the word can also use double pointer, basically double pointers are performed three times in different ways in this problem, which is really impressive.
Code:
class Solution {
public String reverseWords(String s) {
StringBuilder sb = new StringBuilder();
sb = removeSpace(s);
reverseString(sb, 0, sb.length() - 1);
reverseWord(sb);
return sb.toString();
}
StringBuilder removeSpace(String s) {
int i = 0;
int j = s.length() - 1;
while (s.charAt(i) == ' ') {
i++;
}
while (s.charAt(j) == ' ') {
j--;
}
StringBuilder sb = new StringBuilder();
while (i <= j) {
char tmp = s.charAt(i);
if (tmp != ' ' || sb.charAt(sb.length() - 1) != ' ') {
sb.append(s.charAt(i));
}
i++;
}
return sb;
}
void reverseString(StringBuilder sb, int start, int end) {
while (start < end) {
char tmp = sb.charAt(start);
sb.setCharAt(start, sb.charAt(end));
sb.setCharAt(end, tmp);
start++;
end--;
}
}
void reverseWord(StringBuilder sb) {
int i = 0;
int j = 1;
int n = sb.length();
while (i < n) {
while (j < n && sb.charAt(j) != ' ') {
j++;
}
reverseString(sb, i, j - 1);
i=j+1;
j=i+1;
}
}
}
Kama 55 rotate the string
link:题目页面
At first, I thought I could just create a new StringBuilder sb, and append the last k elements in the first k positions in sb, and do the rest, and the time complexity will be O(n), space complexity will also be O(n)
yet I don't understand why the solution comes up with a method using double pointer because it's just not convenient at all, reverse all the characters, and then reverse the first k elements and finally the last n-k elements, it's just not even faster
And btw, I hate this website, because I have to write the code where user can input the values,
and main method doesn't return values so I have to print out the result
AND, I AM ABSOLUTELT PROFICIENT AT WRITING REVERSE NOW, IT'S JUST THE SAME PATTERN
import java.util.Scanner;
class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int k=Integer.parseInt(in.nextLine());
String s=in.nextLine();
int n=s.length();
char[] arr=s.toCharArray();
reverse(arr,0,n-1);
reverse(arr,0,k-1);
reverse(arr,k,n-1);
System.out.println(new String(arr));
}
private static void reverse(char[] arr,int i,int j){
while(i<j){
char tmp=arr[i];
arr[i]=arr[j];
arr[j]=tmp;
i++;
j--;
}
}
}