-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseVowelsofaString.java
More file actions
59 lines (56 loc) · 1.39 KB
/
ReverseVowelsofaString.java
File metadata and controls
59 lines (56 loc) · 1.39 KB
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
56
57
58
59
package easy;
/**
* ClassName: ReverseVowelsofaString.java
* Author: chenyiAlone
* Create Time: 2019/7/3 9:03
* Description: No.345
* 思路:
* 1. switch 来判断字母
* 2. String 不能修改,使用 StringBuilder 来修改单词
*
*
* Write a function that takes a string as input and reverse only the vowels of a string.
*
* Example 1:
*
* Input: "hello"
* Output: "holle"
* Example 2:
*
* Input: "leetcode"
* Output: "leotcede"
* Note:
* The vowels does not include the letter "y".
*
*/
public class ReverseVowelsofaString {
public String reverseVowels(String s) {
int len = s.length();
StringBuilder ret = new StringBuilder(s);
for (int i = 0, j = len - 1; i < j; i++, j--) {
while (i + 1 < len && unVow(ret, i)) i++;
while (j > 0 && unVow(ret, j)) j--;
if (i >= j) break;
char temp = ret.charAt(i);
ret.setCharAt(i, ret.charAt(j));
ret.setCharAt(j, temp);
}
return ret.toString();
}
private boolean unVow(StringBuilder sb, int p) {
char c = sb.charAt(p);
switch(c) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U': return false;
}
return true;
}
}