给定一组自然数,数字的值有可能会大于2^64 ,要求计算出所有数字的和
import org.junit.Test; import java.util.ArrayList; public class Solution { /** * 计算一组数字的和 * @param numbers string字符串ArrayList 一组自然数 * @return string字符串 */ public String sum (ArrayList<String> numbers) { // write code here String result="0"; for (String number : numbers) { if(number==null||number.length()==0){ continue; } int resultLen = result.length(); int curNumLen = number.length(); int sum=0; int remain; StringBuilder stringBuilder = new StringBuilder(); while (resultLen>0||curNumLen>0){ int resultNum=0; if(resultLen>0){ resultNum = result.charAt(--resultLen) - '0'; } int curNum=0; if(curNumLen>0){ curNum = number.charAt(--curNumLen) - '0'; } sum=sum+resultNum+curNum; remain=sum%10; stringBuilder.append(remain); sum/=10; } if(sum!=0){ stringBuilder.append(sum); } result=stringBuilder.reverse().toString(); } return result; } @Test public void test(){ String num1="123456"; String num2="123456789"; String num3="123456789123"; ArrayList<String> strings = new ArrayList<>(); strings.add(num1); strings.add(num2); strings.add(num3); System.out.println(sum(strings)); } }
package top.handsomecoder; import java.math.BigDecimal; import java.util.ArrayList; public class Solution { /** * 计算一组数字的和 * @param numbers string字符串ArrayList 一组自然数 * @return string字符串 */ public String sum (ArrayList<String> numbers) { BigDecimal initN = new BigDecimal("0"); for (String str : numbers) { BigDecimal bd1 = new BigDecimal(str); initN=initN.add(bd1); } return initN.toString(); } }
public class Solution { /** * 计算一组数字的和 * @param numbers string字符串ArrayList 一组自然数 * @return string字符串 */ public String sum (ArrayList<String> numbers) { String cur = numbers.get(0); for(int i = 1; i < numbers.size(); i++) { cur = add(cur, numbers.get(i)); } return cur; } private String add(String num1, String num2) { StringBuilder ansBuilder = new StringBuilder(); int ptr1 = num1.length() - 1, ptr2 = num2.length() - 1; boolean carry = false; while (ptr1 >= 0 && ptr2 >= 0) { int sum = num1.charAt(ptr1--) - '0' + num2.charAt(ptr2--) - '0'; if (carry) { sum++; carry = false; } if (sum >= 10) { sum -= 10; carry = true; } ansBuilder.append(sum); } while (ptr1 >= 0) { int num = num1.charAt(ptr1--) - '0'; if (carry) { num++; carry = false; } if (num >= 10) { num -= 10; carry = true; } ansBuilder.append(num); } while (ptr2 >= 0) { int num = num2.charAt(ptr2--) - '0'; if (carry) { num++; carry = false; } if (num >= 10) { num -= 10; carry = true; } ansBuilder.append(num); } if (carry) { ansBuilder.append(1); } return ansBuilder.reverse().toString(); } }
public static String sum(List<String> numList) { numList = numList.stream() .filter(num -> StringUtils.isNotBlank(num)) .map(num -> num.replaceAll(" ", "")) .collect(Collectors.toList()); if (CollectionUtils.isEmpty(numList)) { return "0"; } StringBuffer sum = new StringBuffer(numList.get(0)); for (int i = 1; i < numList.size(); i++) { StringBuffer next = new StringBuffer(numList.get(i)); Integer sumLength = sum.length(); Integer nextLength = next.length(); Stack<Character> stack = new Stack<>(); boolean hasCarry = false; while (sumLength > 0 || nextLength > 0 || hasCarry) { Integer sumNum = 0; Integer nextNum = 0; if (sumLength > 0) { String sumLast = sum.substring(sumLength - 1, sumLength); sumNum = Integer.parseInt(sumLast); } if (nextLength > 0) { String nextLast = next.substring(nextLength - 1, nextLength); nextNum = Integer.parseInt(nextLast); } Integer thisTurnInt = sumNum + nextNum + (hasCarry ? 1 : 0); Character thisTurn = new Character(thisTurnInt.toString().charAt(thisTurnInt.toString().length() - 1)); stack.push(new Character(thisTurn)); if (thisTurnInt > 9) { hasCarry = true; } else { hasCarry = false; } sumLength--; nextLength--; } sum = new StringBuffer(); while (stack.size() > 0) { sum.append(stack.pop()); } } return sum.toString(); }