【发布时间】:2017-07-05 00:54:21
【问题描述】:
我正在尝试开发一种方法来反转字符串中的元音。为此,我开发了自己的小堆栈。我在字符串中向后迭代两次,一次是用我找到的每个元音填充堆栈,第二次用存储在堆栈顶部的元音替换元音。我添加了一堆打印语句来确定失败的地方,并且似乎在 setCharAt 方法上失败了。出于测试目的,我提供了一个字符串aeiou,我希望得到uoiea。这些字符在每次迭代中都会被替换,但不幸的是,它们不会保持这种状态。在下一次迭代中,在上一次迭代中被替换的字符恢复到它们之前的状态。结果,我返回了ueiou,其中只有字符串中的第一个字符符合预期(第三个字符是 noop)。这是我的代码。任何提示都表示赞赏。
import java.util.*;
public class Solution {
static String result;
static class StackOfVowels {
static Node top;
public StackOfVowels() {
Node top = null;
}
static class Node {
Node next = null;
char vowel = '\0';
Node(char value) {
this.vowel = value;
}
}
public void push(char item) {
Node node = new Node(item);
if (top == null) {
top = node;
}
else {
node.next = top;
top = node;
}
}
public char top() {
if (top == null) throw new EmptyStackException();
return top.vowel;
}
public void pop() {
int result = -1;
if (top != null) {
result = top.vowel;
top = top.next;
}
}
}
public static String reverseVowels(String s) {
StackOfVowels stackOfVowels = new StackOfVowels();
for(int i = s.length()-1; i >= 0; i--) {
char c = s.charAt(i);
if ((c == 'a') || (c == 'e') || (c == 'i') || (c == 'o') || (c == 'u')) {
System.out.println("Initial sequence of iterations identified vowel: " + c);
stackOfVowels.push(c);
System.out.println("Pushed to stack, current top is: " + stackOfVowels.top());
}
}
for(int j = s.length()-1; j >= 0; j--) {
char b = s.charAt(j);
if ((b == 'a') || (b == 'e') || (b == 'i') || (b == 'o') || (b == 'u')) {
System.out.println("Second sequence of iterations identified vowel: " + b);
StringBuilder newstr = new StringBuilder(s);
char d = stackOfVowels.top();
System.out.println("Variable d set to top of: " + stackOfVowels.top());
newstr.setCharAt(j, d);
result = newstr.toString();
System.out.println("Here is the new string: " + result);
stackOfVowels.pop();
System.out.println("Stack was popped");
}
}
return result;
}
public static void main(String[] args) {
String s = "aeiou";
reverseVowels(s);
System.out.println("Final result: " + result);
}
}
【问题讨论】:
-
也许摆脱静态的
result并使用从该方法返回的值 - 如果这不起作用,然后尝试调试它 -
谢谢你的想法。我的打印语句告诉我,第二个 for 循环中的结果是错误的,然后在最后返回的结果也是错误的。
-
每次循环您都执行
new StringBuilder(s),这基本上会将您重置回初始字符串,因此只有您最后的更改才会生效。将它移到循环之外,它应该会更好地工作。 -
为什么
StringBuilder newstr = new StringBuilder(s);在你的循环中? -
我注意到
public StackOfVowels() { Node top = null; }的其他事情毫无意义。public void pop() { int result = -1;result有什么用?