-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q58_翻转字符串.java
55 lines (45 loc) · 1.5 KB
/
Q58_翻转字符串.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
48
49
50
51
52
53
54
55
package com.algorithm.demo.剑指Offer;
/**
* 输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。为简单起见,标点符号和普通字母一样处理。
* 例如输入字符串“I am a student.”,则输出"student. a am I"。
* .tneduts a ma I student. a am I
*/
public class Q58_翻转字符串 {
public static void main(String[] args) {
reverseSentence("I am a student.");
}
public static String reverseSentence(String str) {
if (str == null)
return null;
int left = 0;
int right = str.length() - 1;
//翻转整个句子
reverse(str, left, right);
//翻转所有单词
return "";
}
public static void reverse(String str, int left, int right) {
char[] chars = str.toCharArray();
while (left <= right) {
char temp = chars[left];
chars[left] = chars[right];
chars[right] = temp;
left++;
right--;
}
System.out.println(" " + new String(chars));
}
/**
* @param string: a string
* @return: return a string
*/
public static String reverseString(String string) {
if (string == null || string.length() == 0) return string;
int length = string.length();
char[] temp = string.toCharArray();
for (int i = 0; i < length; i++) {
temp[i] = string.charAt(length - i - 1);
}
return new String(temp);
}
}