GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode-algorithms/src/AddString.java at master · anishLearnsToCode/leetcode-algorithms Expand file tree/ Copy pathAddString.javaMore file actions 27 lines (25 loc) · 947 Bytes / Copy pathAddString.javaFile metadata and controls27 lines (25 loc) · 947 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public class AddString { public static void main(String[] args) { String number1 = "5"; String number2 = "10"; System.out.println(addStrings(number1, number2)); } public static String addStrings(String num1, String num2) { StringBuilder result = new StringBuilder(); int carry = 0, length = Math.max(num1.length(), num2.length()); for (int index = 0 ; index < length ; index++) { int digit1 = getDigit(num1, index); int digit2 = getDigit(num2, index); int sum = digit1 + digit2 + carry; result.append(sum % 10); carry = sum / 10; } return result.append(carry > 0 ? carry : "").reverse().toString(); } private static int getDigit(String number, int index) { if (index < number.length()) { return number.charAt(number.length() - index - 1) - '0'; } return 0; } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |