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. selenium常用的API(五)获取title、刷新、前进和后退

    获取网页title的属性值 #encoding=utf-8 from selenium import webdriver import unittest import time class Visit ...

  2. async-validator 表单验证注意事项

    1. this.$refs[formName].validate()里面的内容不执行 解决问题出处:https://segmentfault.com/q/1010000009679079 问题描述:1 ...

  3. mssql提权

    MSSQL的提权:下面是三种方法一种利用xp_cmshell组件,还有一种sp_OACreate组件,COM组件 xp_cmshell组件的开启: 1.sql2005版本以后默认为关闭状态,需要开启命 ...

  4. 持续集成学习8 jenkins权限控制

    一.总体配置 1.系统管理---> Configure Global Security 2.配置基于角色授权 创建角色 ----> 分配角色 代表着所有以dev-开头的 job全部都分配给 ...

  5. Python爬虫 | Beautifulsoup解析html页面

    引入 大多数情况下的需求,我们都会指定去使用聚焦爬虫,也就是爬取页面中指定部分的数据值,而不是整个页面的数据.因此,在聚焦爬虫中使用数据解析.所以,我们的数据爬取的流程为: 指定url 基于reque ...

  6. WinDbg常用命令系列---|(进程状态)

    |(进程状态) 简介 (|) 命令显示指定进程的状态或当前正在调试你的所有进程. 使用形式 | Process 参数 Process 指定要显示的进程. 如果省略此参数,将显示所有正在调试的进程. 支 ...

  7. XA 事务

    4.11.3 什么是XA 事务? <数据库程序员面试笔试宝典>第4章数据库基础,本章主要介绍数据库基础部分的面试题,比较适合应届毕业生,也适合由其他岗位转数据库岗位的人员.本节为大家介绍什 ...

  8. Qt 反射,moc,Q_INVOKABLE

    使用Q_INVOKABLE来修饰成员函数,目的在于被修饰的成员函数能够被元对象系统所唤起 Q_INVOKABLE与QMetaObject::invokeMethod均由元对象系统唤起.这一机制在Qt ...

  9. Django自带后台admin的使用配置

    Django自带后台使用配置参考官网地址:https://docs.djangoproject.com/en/1.11/ref/contrib/admin/ ,本文章值是介绍简单配置,如果需要详细内容 ...

  10. 浅谈 Miller-Robbin 与 Pollard Rho

    前言 $Miller-Robbin$ 与 $Pollard Rho$ 虽然都是随机算法,不过用起来是真的爽. $Miller Rabin$ 算法是一种高效的质数判断方法.虽然是一种不确定的质数判断法, ...