-1

我遇到了一篇文章,展示了如何按字母顺序排列 char 数组。看到可以这样做,我想按输入字符串的字符顺序输出输入字符串的每个字符的字母顺序。

我有点卡住了。我可以按字母顺序重新排序字符串,但我不知道下一步该做什么。

例子是 'monkey' 到 '354216'

因为 'ekmnoy' e 在给定字符集中按字母顺序排在第一位,所以 e = 1 , k 是排序后的第二个 alpha 字符,所以 k = 2,依此类推。

如果您无法理解,我可以提供更多示例来说明清楚。

代码

    String str = "airport";
        Character[] chars = new Character[str.length()];
        for (int z = 0; z < chars.length; z++) {
            chars[z] = str.charAt(z);
        }


        Arrays.sort(chars, new Comparator<Character>() {
            public int compare(Character c1, Character c2) {
                int cmp = Character.compare(
                        Character.toLowerCase(c1.charValue()),
                        Character.toLowerCase(c2.charValue()));
                if (cmp != 0) {
                    return cmp;
                }
                return Character.compare(c1.charValue(), c2.charValue());
            }
        });


        StringBuilder sb = new StringBuilder(chars.length);
        for (char c : chars) {
            sb.append(c);
        }
        str = sb.toString();

        System.out.println(sb);

输出

aioprrt

预期产出

Orange -> aegnOr
561432 -  123456

Monkey -> ekMnoy
354216 -> 123456
4

3 回答 3

1

I dont know what you want to do with double characters, but if you add this few lines to your code at the end you are getting the right result. Iterate over the sorted String and replace the charakters in the original String with their indices in the sorted String.

String originalStr = "airport";
for(int i = 0; i<str.length(); i++) {
    originalStr = originalStr.replace(str.charAt(i), String.valueOf(i+1).charAt(0));
}
System.out.println(originalStr);

Output: 1254357

If you want to get the output: 1254367 use replaceFirst:

 originalStr = originalStr.replaceFirst(String.valueOf(str.charAt(i)), String.valueOf(i+1));


Input:Orange
Output:561432

Input:Monkey
Output:354216

The whole code:

    String str = "airport";
    String originalStr = str; //creat a backup of str because you change it in your code
    Character[] chars = str.toCharArray();


    Arrays.sort(chars, new Comparator<Character>() {
        public int compare(Character c1, Character c2) {
            int cmp = Character.compare(
                    Character.toLowerCase(c1.charValue()),
                    Character.toLowerCase(c2.charValue()));
            if (cmp != 0) {
                return cmp;
            }
            return Character.compare(c1.charValue(), c2.charValue());
        }
    });

    str = String.valueOf(chars);
    System.out.println(str);


    //Iterate over the sorted String and replace the charakters in the original String with their indices in the sorted String
    for(int i = 0; i<str.length(); i++) {
        originalStr = originalStr.replaceFirst(String.valueOf(str.charAt(i)), String.valueOf(i+1));
    }
    System.out.println(originalStr);
于 2014-01-09T07:28:57.940 回答
1

一旦您按顺序排列了字符(在与原始数组不同的数组中),然后通过遍历原始字符串并从排序字符串中选择每个字符的索引来创建第三个数组。

input:  edcba
sorted: abcde
index:  01234

伪代码...

for( int i = 0; i < input.length(); i++ ) {
    index[i] = sorted.indexOf(input[i]);
}

结果应该是给定输入的 43210。

请注意,超过 10 个字符的字符串会导致输出不明确,可以通过在输出中插入空格来处理。例子:

abcdefghijk -> 
012345678910
于 2014-01-09T07:25:09.727 回答
0

您可以使用以下代码:

package Test;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

public class Arrange {
    public static void main(String[] args) {
        String str = "money";
        List<Test> strs=new LinkedList<Test>();
        List<Test> final_result=new LinkedList<Test>();
        for(int i=0;i<str.length();i++)
        {
            Test t=new Test(i, ""+str.charAt(i), 0);
            strs.add(t);
        }
        Collections.sort(strs,new Comparator<Test>() {
               @Override
               public int compare(Test o1, Test o2) {
                   return (o1.getS().compareToIgnoreCase(o2.getS()));
               }
           });
        Integer i=1;
        for (Test st : strs) {
           st.setJ(i);
           final_result.add(st);
           i++;
       }
        Collections.sort(final_result,new Comparator<Test>() {
            @Override
            public int compare(Test o1, Test o2) {
                return (o1.getI().compareTo(o2.getI()));
            }
        });
        for (Test test : final_result) {
            System.out.println(test.getJ());
        }
   }
}

class Test{
    private Integer i;
    private String s;
    private Integer j;
    public Test() {
        // TODO Auto-generated constructor stub
    }
    public Test(Integer i, String s, Integer j) {
        super();
        this.i = i;
        this.s = s;
        this.j = j;
    }
    public Integer getI() {
        return i;
    }
    public void setI(Integer i) {
        this.i = i;
    }
    public String getS() {
        return s;
    }
    public void setS(String s) {
        this.s = s;
    }
    public Integer getJ() {
        return j;
    }
    public void setJ(Integer j) {
        this.j = j;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((i == null) ? 0 : i.hashCode());
        result = prime * result + ((j == null) ? 0 : j.hashCode());
        result = prime * result + ((s == null) ? 0 : s.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Test other = (Test) obj;
        if (i == null) {
            if (other.i != null)
                return false;
        } else if (!i.equals(other.i))
            return false;
        if (j == null) {
            if (other.j != null)
                return false;
        } else if (!j.equals(other.j))
            return false;
        if (s == null) {
            if (other.s != null)
                return false;
        } else if (!s.equals(other.s))
            return false;
        return true;
    }
}
于 2014-01-09T08:38:52.853 回答