415. Add Strings

Easy

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.
package leetcode.easy;

public class AddStrings {
public String addStrings(String num1, String num2) {
final int m = num1.length();
final int n = num2.length();
int p1 = m - 1, p2 = n - 1;
int carry = 0;
StringBuilder sb = new StringBuilder();
while (p1 >= 0 || p2 >= 0 || carry > 0) {
int sum = carry + (p1 >= 0 ? num1.charAt(p1) - '0' : 0) + (p2 >= 0 ? num2.charAt(p2) - '0' : 0);
carry = sum / 10;
sb.append((char) (sum % 10 + '0'));
p1--;
p2--;
}
return sb.reverse().toString();
} @org.junit.Test
public void test() {
System.out.println(addStrings("0", "0"));
}
}

LeetCode_415. Add Strings的更多相关文章

  1. 【leetcode】415. Add Strings

    problem 415. Add Strings solution: class Solution { public: string addStrings(string num1, string nu ...

  2. 36. leetcode 415. Add Strings

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

  3. [LeetCode] 415 Add Strings && 67 Add Binary && 43 Multiply Strings

    这些题目是高精度加法和高精度乘法相关的,复习了一下就做了,没想到难住自己的是C++里面string的用法. 原题地址: 415 Add Strings:https://leetcode.com/pro ...

  4. 447. Add Strings

    原文题目: 447. Add Strings 解题: 字符串的当做整数来做加法,其实就是大数加法的简化版本 思路: 1)考虑不同位数,如"1234"+“45”,需要先处理低两位,再 ...

  5. LeetCode——Add Strings

    LeetCode--Add Strings Question Given two non-negative integers num1 and num2 represented as string, ...

  6. [LeetCode] Add Strings 字符串相加

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

  7. LeetCode Add Strings

    原题链接在这里:https://leetcode.com/problems/add-strings/ 题目: Given two non-negative numbers num1 and num2  ...

  8. [LeetCode] 415. Add Strings 字符串相加

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

  9. Add Strings大整数加法十进制求和 & Add Binary二进制求和

    [抄题]: 以字符串的形式给出两个非负整数 num1 和 num2,返回 num1和 num2 的和. 比如一个50位+一个100位. 给定 num1 = "123",num2 = ...

随机推荐

  1. 关于struct和typedef struct

    以 struct TelPhone{ ]; ]; }; 为例 这里先定义了一个 TelPhone的结构体. 加入需要为TelPhone定义一个别名: 其语法为 typedef TelPhone TP: ...

  2. JSON.stringify(),JSON.parse(),toJSON()使用方法总结

    今天在看<你不知道的javascript-中>第四章‘强制类型转换’的时候,发现JSON.stringify(),JSON.parse(),toJSON()有很多细节,自己也就总结测试了一 ...

  3. mac 使用 brew 安装 nginx 及各种命令

    一.安装 brew install nginx 或 sudo brew install nginx 二.启动 brew services start nginx 或 sudo brew service ...

  4. 移动平台前端开发总结(ios,Android)

    首先我们来看看webkit内核中的一些私有的meta标签,这些meta标签在开发webapp时起到非常重要的作用 <meta content="width=device-width; ...

  5. 查看mysql执行时间

    mysql的 profiling不是默认打开的 查看profiling是否找开 mysql> show variables like "%pro%"; +---------- ...

  6. 36氪首发 | 掘金RPA百亿新蓝海,弘玑Cyclone获DCM、源码千万美元A轮融资

    https://36kr.com/p/5213638?ktm_source=feed 在“没风口”的2019年,RPA算是一个“小风口”了. 36氪了解到,近日国内数家RPA公司已完成或即将完成融资, ...

  7. Dubbo源码分析:Server

    Server接口是开启一个socket服务.服务实现有netty,mina,grizzly的. 抽象时序图 获取NettyServer时序图 Transporter类图 Server 类图

  8. javaWeb开发中entityBean的习惯用法

    entity bean的作用是将与数据库交互的过程封装成对象,在servelet编写的过程中,只需要会写java,不必考虑与数据库交互细节. 实体类: 基本与数据库的表相对应,表示一个实在的对象. 例 ...

  9. robot framework设置更高级别的关键字

    robot framework中除了内置的关键字,以及低级别的用户自定义关键字外,为了使用例更加整洁,我们还可以形成更高级别的关键字 方法如下: 在Keywords里面设置 其中Run Success ...

  10. MySQL 已有大数据量表进行分区踩坑

    一.背景mysql 表中已有 4 亿数据,为提高查询效率,需创建分区,一开始计划是创建 HASH 分区,结果报错:ERROR 1659 (HY000): Field 'partno' is of a ...