zoukankan      html  css  js  c++  java
  • 415. Add Strings

    题目:

    Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.

    Note:

    1. The length of both num1 and num2 is < 5100.
    2. Both num1 and num2 contains only digits 0-9.
    3. Both num1 and num2 does not contain any leading zero.
    4. You must not use any built-in BigInteger library or convert the inputs to integer directly.

    链接:https://leetcode.com/problems/add-strings/#/description

    3/24/2017

    没有想到这道题会提交这么多次不成功。

    问题:

    1. i, j的初始值不要越界

    2. int -> char, char -> int的互换。Character.toInteger(char)是没有的,int - '0'没有问题,但是((a + b + c) % 10) + '0'想得到char是不行的。(char)((a + b + c) % 10)返回的是unicode?比如"0", "0"返回结果是"u0000"而不是"0"。这个过程碰到的问题太多,至少有4,5个问题,需要解决。

    3. 每一步算sum和c的顺序,注意算c在后面,不要用当前值把低位的进位覆盖。

     1 public class Solution {
     2     public String addStrings(String num1, String num2) {
     3         StringBuilder sum = new StringBuilder();
     4         int a, b, c = 0;
     5         int s = 0;
     6         int i = num1.length() - 1;
     7         int j = num2.length() - 1;
     8 
     9         for (; i >= 0 && j >= 0; i--, j--) {
    10             a = num1.charAt(i) - '0';
    11             b = num2.charAt(j) - '0';
    12             sum.append(Character.forDigit((a + b + c) % 10, 10));
    13             if (c + a + b >= 10) c = 1;
    14             else c = 0;
    15         }
    16         for (; i >= 0; i--) {
    17             a = num1.charAt(i) - '0';
    18             sum.append(Character.forDigit((a + c) % 10, 10));
    19             if (c + a >= 10) c = 1;
    20             else c = 0;
    21         }
    22         for (; j >= 0; j--) {
    23             b = num2.charAt(j) - '0';
    24             sum.append(Character.forDigit((b + c) % 10, 10));
    25             if (c + b >= 10) c = 1;
    26             else c = 0;
    27         }
    28         if (c == 1) sum.append('1');
    29         return sum.reverse().toString();
    30     }
    31 }

    别人的精简算法:注意第8行,这都可以?简直奇妙!同时只用了一个循环,类似的题目很多,希望自己也能记住。

     1 public class Solution {
     2     public String addStrings(String num1, String num2) {
     3         StringBuilder sb = new StringBuilder();
     4         int carry = 0;
     5         for(int i = num1.length() - 1, j = num2.length() - 1; i >= 0 || j >= 0 || carry == 1; i--, j--){
     6             int x = i < 0 ? 0 : num1.charAt(i) - '0';
     7             int y = j < 0 ? 0 : num2.charAt(j) - '0';
     8             sb.append((x + y + carry) % 10);
     9             carry = (x + y + carry) / 10;
    10         }
    11         return sb.reverse().toString();
    12     }
    13 }

    其他讨论:https://discuss.leetcode.com/category/543/add-strings

  • 相关阅读:
    Saltstack_使用指南09_远程执行-编写执行模块
    Saltstack_使用指南08_远程执行-返回程序
    Saltstack_使用指南07_远程执行-执行模块
    Saltstack_使用指南06_远程执行-指定目标
    CentOS7 Docker私有仓库搭建及删除镜像 【转】
    Python Docker 查看私有仓库镜像【转】
    Saltstack_使用指南05_数据系统-Pillar
    Saltstack_使用指南04_数据系统-Grains
    Saltstack_使用指南03_配置管理
    Saltstack_使用指南02_远程执行-验证
  • 原文地址:https://www.cnblogs.com/panini/p/6615322.html
Copyright © 2011-2022 走看看