-1

我正在尝试检查一个字符串是否是回文,但它似乎不起作用,因为当我发送一个我知道不是回文的字符串时,它返回它是一个回文,有人可以帮忙吗?它也不会添加到变量counter中。

package UnaryStack.RubCol1183;

public class CheckPalindrome {

static int counter = 0;

/** Decides whether the parentheses, brackets, and braces
in a string occur in left/right pairs.
@param expression a string to be checked
@return true if the delimiters are paired correctly */
public static boolean checkBalance(String expression)
{
    StackInterface<Character> temporaryStack = new LinkedStack<Character>();
    StackInterface<Character> reverseStack = new LinkedStack<Character>();
    StackInterface<Character> originalStack = new LinkedStack<Character>();


    int characterCount = expression.length();
    boolean isBalanced = true;
    int index = 0;
    char nextCharacter = ' ';


    for (;(index < characterCount); index++)
    {
        nextCharacter = expression.charAt(index);
        switch (nextCharacter)
        {
        case '.': case '?': case '!': case '\'': case ' ': case ',':
            break;
        default: 
        {
            {
                reverseStack.push(nextCharacter);
                temporaryStack.push(nextCharacter);
                originalStack.push(temporaryStack.pop());
            }

            {
                char letter1 = Character.toLowerCase(originalStack.pop());
                char letter2 = Character.toLowerCase(reverseStack.pop());
                isBalanced = isPaired(letter1, letter2);
                if(isBalanced == false){
                    counter++;
                }
            }

            break;
        }
        } // end switch
    } // end for
    return isBalanced;
} // end checkBalance

// Returns true if the given characters, open and close, form a pair
// of parentheses, brackets, or braces.
private static boolean isPaired(char open, char close)
{
    return (open == close);
} // end isPaired

public static int counter(){
    return counter;
}


}//end class
4

3 回答 3

1

您的实现似乎比需要的复杂得多。

//Check for invalid characters first if needed.
StackInterface<Character> stack = new LinkedStack<Character>();

for (char ch: expression.toCharArray()) {
    Character curr = new Character(ch);
    Character peek = (Character)(stack.peek());
    if(!stack.isEmpty() && peek.equals(curr)) {
        stack.pop();
    } else {
        stack.push(curr)
    }
}
return stack.isEmpty();

老实说,在这里使用堆栈已经过时了。我会使用以下方法。

int i = 0;
int j = expression.length() - 1;
while(j > i) {
    if(expression.charAt(i++) != expression.charAt(j--)) return false;
}
return true;
于 2013-04-23T13:49:02.397 回答
0

您在 reverseStack 和 originalStack 中放置了完全相同的元素,因为您推入temporaryStack 的所有内容都将立即推入originalStack。这根本不符合逻辑。

reverseStack.push(nextCharacter);
temporaryStack.push(nextCharacter);
originalStack.push(temporaryStack.pop());

因此表达式

isBalanced = isPaired(letter1, letter2);

将始终返回 true。

于 2013-04-23T13:48:26.443 回答
0

我发现了在方法 checkBalace() 中发现的逻辑错误,并将代码完成为完整的工作代码。这是我完成的代码的样子:

public class CheckPalindrome {

static int counter;

/** Decides whether the parentheses, brackets, and braces
in a string occur in left/right pairs.
@param expression a string to be checked
@return true if the delimiters are paired correctly */
public static boolean checkBalance(String expression)
{
    counter = 0;
     StackInterface<Character> temporaryStack = new LinkedStack<Character>();
     StackInterface<Character> reverseStack = new LinkedStack<Character>();
     StackInterface<Character> originalStack = new LinkedStack<Character>();

     boolean isBalanced = true;

    int characterCount = expression.length();

    int index = 0;
    char nextCharacter = ' ';


    for (;(index < characterCount); index++)
    {
        nextCharacter = expression.charAt(index);
        switch (nextCharacter)
        {
        case '.': case '?': case '!': case '\'': case ' ': case ',':
            break;
        default: 
        {
            {
                reverseStack.push(nextCharacter);
                temporaryStack.push(nextCharacter);

            }

            break;
        }
        } // end switch


} // end for

while(!temporaryStack.isEmpty()){
    originalStack.push(temporaryStack.pop());
}

while(!originalStack.isEmpty()){
    char letter1 = Character.toLowerCase(originalStack.pop());
    char letter2 = Character.toLowerCase(reverseStack.pop());
    isBalanced = isPaired(letter1, letter2);
    if(isBalanced == false){
        counter++;
    }
}


    return isBalanced;
} // end checkBalance

// Returns true if the given characters, open and close, form a pair
// of parentheses, brackets, or braces.
private static boolean isPaired(char open, char close)
{
    return (open == close);
} // end isPaired

public static int counter(){
    return counter;
}


}

我在for之外使用了 2 while 方法,从而修复了指出的逻辑错误。我还在方法内部将值 0 分配给counter以解决我遇到的一个小问题。如果我仍然有错误,请随时修改代码,但我认为我没有出错,再说一遍,我是初学者。

于 2013-04-25T18:46:52.693 回答