-
Notifications
You must be signed in to change notification settings - Fork 1
/
Q58_reverseWords.java
47 lines (43 loc) · 1.26 KB
/
Q58_reverseWords.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package jianzhi_offer;
/**
* @author weiyuwang
* @since 2019/2/3 20:54
*/
public class Q58_reverseWords {
public static void main(String[] args){
System.out.println(reverseString("abcdefg", 2));
}
/**
* LeetCode 151
*
* 给定一个字符串,逐个翻转字符串中的每个单词。
*/
public String reverseWords(String s) {
if(s == null) return "";
s = s.trim();
String[] strs = s.split(" ");
StringBuilder sb = new StringBuilder();
for (String str : strs) {
sb.insert(0, str);
if (!str.equals("")) {
sb.insert(0, " ");
}
}
return sb.toString().trim();
}
/**
* 定义函数实现将字符串前面的 k 个字符移到字符串的尾部
* @param str
*/
public static String reverseString(String str, int k) {
if (str == null || str.length() < k) {
return null;
}
StringBuilder sb = new StringBuilder();
sb.append(reverse(str.substring(0, k))).append(reverse(str.substring(k, str.length()))).reverse();
return sb.toString();
}
public static String reverse(String string) {
return new StringBuilder(string).reverse().toString();
}
}