This topic is very simple. Record other people's practices for self-study.
They are: almost all functions are called & functions are implemented by themselves
1. All call functions:
public class Solution {
public String reverseWords(String s) {
String words[] = s.split(" ");
StringBuilder res=new StringBuilder();
for (String word: words)
res.append(new StringBuffer(word).reverse().toString() + " ");
return res.toString().trim();
}
}
Among them:
The split function splits strings according to regular expressions
for(String word:words) traversal string
Appnd function, note that StringBuilder provides, String does not support
reverse() function, inversion
StringBuilder.toString(), type conversion, commonly used for me
trim() function, delete the blank at the beginning and end, commonly used for me
2. Customization of the above functions:
public class Solution {
public String reverseWords(String s) {
String words[] = split(s);
StringBuilder res=new StringBuilder();
for (String word: words)
res.append(reverse(word) + " ");
return res.toString().trim();
}
public String[] split(String s) {
ArrayList < String > words = new ArrayList < > ();
StringBuilder word = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ' ') {
words.add(word.toString());
word = new StringBuilder();
} else
word.append( s.charAt(i));
}
words.add(word.toString());
return words.toArray(new String[words.size()]);
}
public String reverse(String s) {
StringBuilder res=new StringBuilder();
for (int i = 0; i < s.length(); i++)
res.insert(0,s.charAt(i));
return res.toString();
}
}
Processing strings from a single character Perspective
Each character of a line of strings is processed
First, break sentences into words according to spaces.
Then iterate over each word as an array.
Finally, connect the words and return them.