没什么限定的话,先翻转,在一位一位加,记得进位就行了。。

public class Solution
{
public String addStrings(String num1, String num2)
{ StringBuilder sb = new StringBuilder(num1);
num1 = sb.reverse().toString();
sb = new StringBuilder(num2);
num2 = sb.reverse().toString(); if(num1.length() > num2.length())
{
String temp = num1;
num1 = num2;
num2 = temp;
}
String res = new String();
int carry = 0;
for(int i = 0; i < num1.length();i++)
{
int val = num1.charAt(i)+num2.charAt(i)- '0'-'0'+ carry;
if(val > 9) carry = 1;
else carry = 0;
val %= 10;
res+=Integer.toString(val);
} for(int i = num1.length(); i < num2.length();i++)
{
int val = num2.charAt(i) - '0' + carry;
if(val > 9) carry = 1;
else carry = 0;
val %= 10;
res+= Integer.toString(val);
}
if(carry == 1) res += 1; sb = new StringBuilder(res);
return sb.reverse().toString();
}
}

415. Add Strings的更多相关文章

  1. 36. leetcode 415. Add Strings

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

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

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

  3. 【leetcode】415. Add Strings

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

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

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

  5. 【LeetCode】415. Add Strings 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] 题目地址:https:/ ...

  6. LeetCode - 415. Add Strings

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

  7. [LeetCode&Python] Problem 415. Add Strings

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

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

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

  9. 415 Add Strings 字符串相加

    给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和.注意:    num1 和num2 的长度都小于 5100.    num1 和num2 都只包含数字 0-9.    num1 和 ...

随机推荐

  1. 【转载】介绍“Razor”— ASP.NET的一个新视图引擎

    最近在做一个项目,用的MVC razor 视图,因为之前没用这个视图做过,于是查阅文档资料,共享一下. https://msdn.microsoft.com/zh-cn/ff849693 内容主要是讲 ...

  2. Node之express

    Express 是一个简洁.灵活的 node.js Web 应用开发框架, 它提供一系列强大的特性,帮助你创建各种 Web 和移动设备应用. 如何安装: npm install -g express ...

  3. 『重构--改善既有代码的设计』读书笔记----Change Reference to Value

    如果你有一个引用对象,很小且不可改变,而且不易管理,你就需要考虑将他改为一个值对象.在Change Value to Reference我们说过,要在引用对象和值对象之间做选择,有时候并不容易,有了重 ...

  4. angular 跳转页面时传参

    首先,你需要已经配置过你的rout,比如: $stateProvider .state('firstPage',{ url:'/Page/firstPage', templateUrl: 'Page/ ...

  5. wordpress4.0.1源码学习和摘录--函数

    1.根据类型获取当前时间 function current_time( $type, $gmt = 0 ) { switch ( $type ) { case 'mysql': return ( $g ...

  6. 研究在SAE上搭建最新wordpress

    安装SAE上的wordpress,创建应用选择wordpress模板,安装后是3.4版本 新建一个版本2,下载最新wordpress安装包并解压到版本2中 初步猜想修改地方: 数据库配置:wp-con ...

  7. Python函数式编程初级学习

    函数式编程即函数可以作为参数传入函数,也可以返回函数. 1.高阶函数     函数可以作为参数传入函数.     def add(x,y,f):         return f(x)+f(y)   ...

  8. 【C语言】中的stdbool.h头文件

    C语言中的stdbool.h头文件 一.相关基础知识 二.具体内容 Win7下安装的VS2015中的stdbool.h的位置为: F:\Program Files (x86)\Microsoft Vi ...

  9. Find your present (2) (位异或)

    Problem Description In the new year party, everybody will get a "special present".Now it's ...

  10. IOC(控制反转)与DI(依赖注入)的个人理解。

    控制反转IOC(Inversion of Control)的三个需要理清问题: 1.谁控制了谁,控制了什么东西?IOC容器控制了依赖对象的创建. 2.谁得到了反转? 一般的应用程序是,直接创建依赖于该 ...