Java 如何从字符串中计算整数的位数之和
我有一个字符串s=1031234444992000,我想把它排列成2000103123444499。表示数字的最小和将出现在左侧,最大和将出现在右侧。如何做到这一点?我不太清楚我是否理解,但我想试试类似的方法 将字符串转换为数组 如前所述,将数组的每个项转换为数字 整理行李 将数组的内容连接回字符串。 但是从你的要求来看,我不明白为什么4444不在一开始Java 如何从字符串中计算整数的位数之和,java,Java,我有一个字符串s=1031234444992000,我想把它排列成2000103123444499。表示数字的最小和将出现在左侧,最大和将出现在右侧。如何做到这一点?我不太清楚我是否理解,但我想试试类似的方法 将字符串转换为数组 如前所述,将数组的每个项转换为数字 整理行李 将数组的内容连接回字符串。 但是从你的要求来看,我不明白为什么4444不在一开始 public void reorderIntStrings () { //String s = "103 123 4444 99 2
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 );
}
我的解决方案可能有点过头了
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 ;
}
}
首先,尝试编写一个sumOfDigits方法,该方法返回整数n的位数之和。我已经尝试过这个n%9==0&&n!=0 ? 9:n%9;,如何比较它们的数字之和?发布您迄今为止的代码,因为1+2+3=6<4+4+4=16<9+9=18您的代码运行良好,但您应该在代码中添加一些注释。为什么要使用TreeMap,如何对TreeMap的值进行排序?当然,TreeMap会对订单做出具体的保证?嗨,镇静,我添加了解释解决方案的注释。TreeMap是一个实现为红黑树的映射,它保证了键的自然顺序。这看起来不错,但是这个字符串怎么样:2000 10003 1234000 44444444 9999 11 22 123,它不起作用。我编辑了解决方案,添加了一个字符串列表作为mav值类型。通过这种方式,可以收集产生相同总和的所有数据块。如果您使用比较器而不是尝试创建可比较的类型,则可以避免整个特殊数字。是的,您是对的。或者应该像Max那样使用Treemap。树映射总是按其键排序。