• add-strings


    https://leetcode.com/problems/add-strings/
    
    package com.company;
    
    
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Random;
    
    public class Main {
        public String addStrings(String num1, String num2) {
            StringBuilder sb = new StringBuilder();
            if (num1.length() > num2.length()) {
                String tmp = num1;
                num1 = num2;
                num2 = tmp;
            }
            int i = num1.length() - 1;
            int j = num2.length() - 1;
            int carry = 0;
            int ival = 0;
    
            for (; i>=0 && j >= 0; i--,j--) {
                ival = carry + num1.charAt(i) - '0' + num2.charAt(j) - '0';
                if (ival > 9) {
                    carry = ival / 10;
                    ival %= 10;
                }
                else {
                    carry = 0;
                }
                sb.append((char)('0' + ival));
            }
    
            // num2 is always longer than num1
            while (j >= 0) {
                ival = carry + num2.charAt(j) - '0';
                if (ival > 9) {
                    carry = ival / 10;
                    ival %= 10;
                }
                else {
                    carry = 0;
                }
                sb.append((char)('0' + ival));
                j--;
            }
    
            // At first, it's missed
            if (carry > 0) {
                sb.append((char)('0' + carry));
            }
    
            return sb.reverse().toString();
        }
    
    
    
        public static void main(String[] args) {
            // write your code here
            System.out.println("Hello");
    
            String num1 = "1";
            String num2 = "9";
            Main obj = new Main();
            String ret = obj.addStrings(num1, num2);
            System.out.printf("ret: %s
    ", ret);
    
        }
    }
  • 相关阅读:
    redis--pipelining管道
    插入排序
    选择排序
    冒泡排序
    网页表格导入导出Excel
    easyUI的datagrid表格的使用
    软件工程实践总结(个人)
    Beta答辩总结
    Beta冲刺7
    Beta冲刺6
  • 原文地址:https://www.cnblogs.com/charlesblc/p/5942018.html
Copyright © 2020-2023  润新知