如何计算String中整数位的总和

时间:2015-10-14 15:55:51

标签: java

我有String s = "103 123 4444 99 2000",我想将其安排到"2000 103 123 4444 99"。表示最小的数字总和将出现在左侧,最大的数字总和将出现在右侧。怎么做?

3 个答案:

答案 0 :(得分:1)

我不太清楚我明白,但我会尝试像

这样的东西
  • split将字符串转换为数组
  • 将数组中的每个项目转换为here
  • 所述的数字
  • 排序arry
  • 将数组的内容连接回字符串。

但我不明白为什么 4444 不是你问的开头.a

答案 1 :(得分:1)

这应该做:

public void reorderIntStrings () {

    //String s = "103 123 4444 99 2000";// --> prints [2000, 103, 123, 4444, 99]
    String s = "2000 10003 1234000 44444444 9999 11 11 22 123";// --> prints [2000, 11, 11, 10003, 22, 123, 1234000, 44444444, 9999]

    //use TreeMap to store the sums as keys. From the javadoc: 'The map is sorted according to the natural ordering of its keys'
    Map<Integer, List<String>> chunks = new TreeMap<> ();

    //split the original string by spaces and loop through the chunks
    for ( String chunk : s.split ( " " ) ) {
        //single chunk's digits sum accumulator
        int sum = 0;
        //loops through the chunk's digits
        for ( int i = 0; i < chunk.length (); i++ ) {
            sum += Integer.parseInt ( "" + chunk.charAt ( i ) );
        }
        //puts the sum as key and the chunk as value in the TreeMap
        //this way when the loop ends the map will be filled with ordered keys and the relative chunks could be printed as needed
        //the map value is a list of to string to collect all the chunks resulting in the same sum
        List<String> list = chunks.get ( sum );
        if ( list == null ) {
            list = new ArrayList<> ();
            chunks.put ( sum, list );
        }
        list.add ( chunk );
    }

    //flattens the results
    List<String> finalList = new ArrayList<> ();
    for ( List<String> v : chunks.values () ) {
        finalList.addAll ( v );
    }

    System.out.println ( finalList );
}

答案 2 :(得分:0)

我的解决方案可能有点矫枉过正......

    import java.util.ArrayList;
    import java.util.Collections;
    public class TestClass {

        public static void main(String[] args) {

            String numberString = "200 100 333 111";
            String[] numberTokens = numberString.split(" ");

            ArrayList<SpecialNumber> list = new ArrayList<SpecialNumber>();
            for (String s : numberTokens) {
                list.add(new SpecialNumber(s));
            }

            Collections.sort(list);
            for (SpecialNumber s : list) {

                System.out.println(s.getNumberString());
            }   
        }
    }
    class SpecialNumber implements Comparable<SpecialNumber>{

        private int sumOfDigits;
        private String numberString;

        public int getSumOfDigits() {
            return this.sumOfDigits;
        }   
        public String getNumberString() {
            return numberString;
        }

        public SpecialNumber(String numberString) {
            this.sumOfDigits = this.countSum(numberString);
            this.numberString = numberString;
        }

        private int countSum(String numberString) {

            int sum = 0;

            for(int i=0; i<numberString.length(); i++) {

                try {
                    Character c_digit = numberString.charAt(i);
                    int digit = Integer.parseInt(c_digit.toString());
                    sum += digit;
                }catch (NumberFormatException nfe) {
                    System.out.println(nfe.toString());
                }
            }
            return sum;
        }

        @Override
        public int compareTo(SpecialNumber o) {
            return this.sumOfDigits - o.sumOfDigits ;
        }
    }