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

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. 比较ArrayList和LinkedList

    比较一:添加内容 涉及方法:add public void add_test(){ List<Person> addlist = new ArrayList<Person>() ...

  2. boost::function实践——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》

    代码段1: #include <boost/function.hpp> #include <iostream> float mul_ints(int x, int y) { r ...

  3. Python之练习Demo

    遍历本地文件系统 (sys, os, path),例如写一个程序统计一个目录下所有文件大小并按各种条件排序并保存结果,代码如下: #coding:GBK import os; def SortList ...

  4. php 常用几个函数

    function foo($arg){    $arg_num = func_num_args(); // 获取函数参数的个数    $args = func_get_args();    // 获取 ...

  5. CSS中:nth-child和JQuery:eq的区别

    $("li:nth-child(n)")选择器与$("li:eq(n)")选择器的不同之处在于:$("li:eq(n)")选择器只匹配一个元 ...

  6. php5.4安装ecshopphp5.4问题及解决

    includes/cls_template.php line422 将 $tag_sel = array_shift(explode(" ", $tag)); 这句话拆开为两句. $tag_exp = ...

  7. 【python之旅】python的基础一

    一.关于模块那些事 python的强大之处在于他有着丰富且强大的标准库和第三方库,很对功能都有相应的python库支持 例如: sys模块: # Author :GU import sys print ...

  8. linux下配置NFS服务器

    (声明:本文大部分文字摘自Linux NFS服务器的安装与配置) 一.NFS简介     NFS 是Network File System的缩写,即网络文件系统.一种使用于分散式文件系统的协定,由Su ...

  9. Apriori算法在购物篮分析中的运用

    购物篮分析是一个很经典的数据挖掘案例,运用到了Apriori算法.下面从网上下载的一超市某月份的数据库,利用Apriori算法进行管理分析.例子使用Python+MongoDB 处理过程1 数据建模( ...

  10. 【HTTP】Speed and Mobility: An Approach for HTTP 2.0 to Make Mobile Apps and the Web Faster

    This week begins face to face meetings at the IETF on how to approach HTTP 2.0 and improve the Inter ...