leetcode415---字符串大数相加

Given two non-negative numbers 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.

我的第一个想法就是选好两个字符串,然后模拟加法进位,然后用一个字符串接着,同时记住进位,最后返回这个字符串的反转即可。

没有想到什么比较好的解决思路

public class Solution {
    public String addStrings(String num1, String num2) {
        StringBuilder sb = new StringBuilder();
        int carry = 0;
        for(int i = num1.length() - 1, j = num2.length() - 1; i >= 0 || j >= 0; i--, j--){
            int x = i < 0 ? 0 : num1.charAt(i) - ‘0‘;
            int y = j < 0 ? 0 : num2.charAt(j) - ‘0‘;
            sb.append((x + y + carry) % 10);
            carry = (x + y + carry) / 10;
        }
        if(carry != 0)
            sb.append(carry);
        return sb.reverse().toString();
    }
}
时间: 2024-08-26 21:09:45

leetcode415---字符串大数相加的相关文章

字符串大数相加,转换,去除前导0,字符串数值比较函数模板

string S(ll n){stringstream ss;string s;ss<<n;ss>>s;return s;} ll N(string s){stringstream ss;ll n;ss<<s;ss>>n;return n;} string rm0(string s){// 去除前导0函数 int i; for(i=0;i<s.size()-1;i++) if(s[i]!='0') break; return s.substr(i);

大数相加相乘

用乘法行列式多次相乘多次相加,时间复杂度为O(nm),n,m分别为两个数字长度. #include<stdio.h> #include<iostream> #include<string> #include<sstream> using namespace std; string a,b,ans,zero,product;///输入a,b,ans是最终答案,zero是乘法行列式补零用的,product暂时存储单位数乘积 int len1,len2,cha,m

高精度问题之大数相加(原来就是用字符串相加,模拟手算这么简单!)

解题心的: 就是基本的一对一模拟手算..借助c++的string 不用逆序运算了.很方便的补0.  最后处理下前导0的问题. #include <iostream> #include <string> using namespace std; // 实现大数相加 结果存放在num中 void bigIntergerAdd(string &num, string add) { int goBit = 0; // 存放进位 // 先交换下顺序 加数的位数要比较少 if (num

java-两个大数相加

题目要求:用字符串模拟两个大数相加. 一.使用BigInteger类.BigDecimal类 public static void main(String[] args) { String a="8888899999999888";  String b="88888888888888";  String str=new BigInteger(a).add(new BigInteger(b)).toString();  System.out.println(str);

模板,大数相加

char a[Max],b[Max],c[Max],sum[Max]; void jia(char str1[],char str2[]) { int i,j,k,z; k=0;z=0; for(i=strlen(str1)-1,j=strlen(str2)-1;i>=0||j>=0;i--,j--) //核心,加法以及进位 { if(i>=0) z+=str1[i]-'0'; if(j>=0) z+=str2[j]-'0'; c[k++]=z%10+'0'; z=z/10; }

大数相加(不开辟额外空间)

转载请注明出处:http://blog.csdn.net/ns_code/article/details/25555743 大数相加可以借助多种方法来实现,这里提供了一种链表节点的数据域为int型(用char型也可以,这样更省空间)的思路.这篇文章采用常用的转变为字符串进行处理的方法,下面说下我用字符串实现大数相加的思路: 假设输入了如下两个字符串(其中上面的红色部分表示数组的下标,下面的绿色和黄色部分表示各字符): s1: s2: 很明显,s1的实际长度为4,s2的实际长度为7,将二者在最低位

栈的应用—大数相加

package org.Stone6762.MStack.adopt;    import java.util.Scanner;    import org.Stone6762.MStack.imple.LinkStack;    /**  * @author_Stone6762  * @Description_大数相加  * */  public class BigAdd {        /**      * @Describe_将表示数字的字符串从高位到低位的形式压入栈_并去除其中的空格 

iOS中计算两个大数相加算法(OC实现)

我们知道计算机的数据类型不同,所能表示的数据量级也不相同,比如: unsigned int : 0-4294967295   int : -2147483648-2147483647 unsigned long : 0-4294967295long :  -2147483648-2147483647long long : -9223372036854775808 ~ 9223372036854775807unsigned long long : 0 ~ 18446744073709551615

大数相加也算经典题之一了吧

大数相加 关于C语言大数(有千百位数的)问题,一般都是使用字符串来记录的.这里,将分享一下大数的代码. *分析 *就大数相加而言,首先末位对其,然后换成整数相加在加上进位 *记录进位,并对结果取模换成字符存入 *重复上述过程,直到公共部分加完 *然后把未加完的加进去 代码如下 1 char* BigSum(char *a,char*b)//传入的为两个大数 2 { 3 char* c=(char*)calloc(N+1,sizeof(char));//其中N为宏定义大数的最大位数 4 int l

ACM 大数相加

大数问题 基本都可以归结到大数相加上来 做大数问题  要返璞归真 回到小学里做加法 把数字读入到字符串数组中  每个位数一 一相加  主要考虑进位问题 如果整数的话 左边用零补齐 小数的话要左右分开补齐零  小数的零要补右边 HDOJ题目在1002 1753 下面给代码  整数相加 #include <stdio.h> #include <string.h> #define SIZE 1000 void convert(char a[],char newa[]) { memset(