Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 重新排列字母以生成回文_Java_Palindrome - Fatal编程技术网

Java 重新排列字母以生成回文

Java 重新排列字母以生成回文,java,palindrome,Java,Palindrome,我用Java编写了这个简单的函数来重新排列给定字符串中的字母以生成回文,或者如果不可能,只打印-1并返回 出于某种原因,我不明白为什么这不起作用(因为它没有通过自动评分脚本)。我测试了我能想到的每一个案例,它确实通过了 有人能提供一些关于这方面的见解吗?谢谢 /** * Pseudo-code: * (0) Compute the occurrences of each characters. * (0')(Also remember how many groups

我用Java编写了这个简单的函数来重新排列给定
字符串中的字母以生成回文,或者如果不可能,只打印
-1
并返回

出于某种原因,我不明白为什么这不起作用(因为它没有通过自动评分脚本)。我测试了我能想到的每一个案例,它确实通过了

有人能提供一些关于这方面的见解吗?谢谢

/**
 * Pseudo-code:
 *      (0) Compute the occurrences of each characters. 
 *      (0')(Also remember how many groups of characters have an odd number of members
 *      (1) If the number remembered above is greater than 1
 *          (meaning there are more than one group of characters with an odd
 *           number of members),
 *          print -1 and return (no palindrome!)
 *      (2) Else, for each group of character
 *              - if the number of member is odd, save it to a var called 'left'
 *              - put a char from the group at the current position, and 
 *                another one at postion [len - cur -1].
 *      (3) If a variable 'left' is defined, put it in the middle of the string
 *      
 * @param wd 
 */

private static void findPalin(String wd)
{
    if (wd.isEmpty())
    {
        // Empty String is a palindrome itself!
        System.out.println("");
        return;
    }
    HashMap<Character, Integer> stats = new HashMap<Character, Integer>();

    int len = wd.length();
    int oddC = 0;
    for (int n = 0; n < len; ++n)
    {
        Integer prv = stats.put(wd.charAt(n), 1);
        if (prv != null)
        {
            if (prv % 2 == 0)
                ++oddC;
            else
                --oddC;
            stats.put(wd.charAt(n), ++prv);
        }
        else
            ++oddC;
    }

    if (oddC > 1)
        System.out.println(-1);
    else
    {
        int pos = 0;
        char ch[] = new char[len];
        char left = '\0';

        for (char theChar :  stats.keySet())
        {

            Integer c = stats.get(theChar);

            if (c % 2 != 0)
            {
                left = theChar;
                --c;
            }

            while (c > 1)
            {
                ch[len - pos - 1] = ch[pos] = theChar;
                ++pos;
                --c;
            }

        }
        if (left != '\0')
            ch[(len - 1) / 2] = left;

        for (char tp : ch)
            System.out.print(tp);
        System.out.println();
    }
}
/**
*伪代码:
*(0)计算每个字符的出现次数。
*(0')(还记得有多少组字符的成员数为奇数
*(1)如果上面记住的数字大于1
*(表示有多组字符具有奇数。)
*成员人数),
*打印-1并返回(无回文!)
*(2)否则,对于每组字符
*-如果成员数为奇数,则将其保存到名为“left”的变量中
*-将组中的一个字符放在当前位置,然后
*另一个在[len-cur-1]位置。
*(3)如果定义了一个变量“左”,把它放在字符串的中间。
*      
*@param wd
*/
私有静态void findPalin(字符串wd)
{
if(wd.isEmpty())
{
//空字符串本身就是回文!
System.out.println(“”);
返回;
}
HashMap stats=newhashmap();
int len=wd.length();
int-oddC=0;
对于(int n=0;n1)
系统输出打印项数(-1);
其他的
{
int pos=0;
char ch[]=新字符[len];
左字符='\0';
for(char-theChar:stats.keySet())
{
整数c=stats.get(theChar);
如果(c%2!=0)
{
左=焦耳;
--c;
}
同时(c>1)
{
ch[len-pos-1]=ch[pos]=theChar;
++pos;
--c;
}
}
如果(左!='\0')
ch[(len-1)/2]=左;
for(char-tp:ch)
系统输出打印(tp);
System.out.println();
}
}

我猜您没有测试空指针。@deantoni这可能是其中一个测试用例,但报告说我所有测试用例都失败了,这让我很困惑。