LeetCode——Add Strings

Question

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

Note:

The length of both num1 and num2 is < 5100.

Both num1 and num2 contains only digits 0-9.

Both num1 and num2 does not contain any leading zero.

You must not use any built-in BigInteger library or convert the inputs to integer directly.

解题思路

这道题就是所谓的大整数加法操作,因为超过了计算机所能表示的范围数,所以用字符串表示,然后注意到就是,当两个长度不一样的时候,可以考虑往一个较短的字符串补‘0’,让两个字符串的长度保持一致。

具体实现

class Solution {
public:
// 大整数加法
string addStrings(string num1, string num2) {
string n1(num1.rbegin(), num1.rend());
string n2(num2.rbegin(), num2.rend()); int size1 = n1.length();
int size2 = n2.length();
if (size1 < size2) {
for (int i = 0; i < size2 - size1; i++) {
n1 += '0';
}
}
if (size1 > size2) {
for (int j = 0; j < size1 - size2; j++) {
n2 += '0';
}
} int remainer = 0;
string str;
for (int i = 0; i < n1.length(); i++) {
int sum = n1[i] - 48 + n2[i] - 48 + remainer;
remainer = 0;
if (sum >= 10) {
sum = sum - 10;
remainer = 1;
}
char tmp = sum + 48;
str += tmp;
}
if (remainer == 1)
str += '1'; string str1(str.rbegin(), str.rend());
return str1;
}
};

LeetCode——Add Strings的更多相关文章

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

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

  2. LeetCode Add Strings

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

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

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

  4. 36. leetcode 415. Add Strings

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

  5. 【leetcode】415. Add Strings

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

  6. [LeetCode] Multiply Strings 字符串相乘

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  7. [LeetCode] Add Two Numbers 两个数字相加

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  8. LeetCode_415. Add Strings

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

  9. 447. Add Strings

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

随机推荐

  1. Poj1482

    It's not a Bug, It's a Feature! Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 1428   ...

  2. Scala学习之For、Function、Lazy(4)

    1.for的使用 for的使用在各种编程语言中是最常见的,这里只是聊聊for在Scala中的表现形式,由于Scala语言是完全面向对象的,所以直接导致for的不同呈现,下面举几个例子说明一下 obje ...

  3. Linux中搭建HTTP服务器

    1.配置IP [root@localhost~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 BOOTPROTO=static ...

  4. minikube操作命令

    1. 下载minikube   https://storage.googleapis.com/minikube/releases/v0.16.0/minikube-linux-amd64     ch ...

  5. delphi ----寻找控件,以字符串类型的名称控件

    vari :Integer;beginfor i := 1 to 10 do(FindComponent('Edit'+inttostr(i)) as TEdit).Text := inttostr( ...

  6. table width 决定 td width

    w td width 有无在chrome edge ff 均未影响td实际宽度,td接近等比分配table width. <!doctype html> <html lang=&qu ...

  7. Apache配置HTTPS的过程小记

    一.HTTPS的summery,综述,它的基本原理,扫肓. http://www.codeceo.com/article/https-knowledge.html 读过后,就明白https怎么加密的, ...

  8. Android系统移植与调试之------->如何添加一个adb wifi无线调试的功能【开发者选项】-【Wifi调试】

    首先弄懂怎么设置adb wifi无线调试的功能,如下所示. 1. 手机端开启adb tcp连接端口 :/$setprop service.adb.tcp.port :/$stop adbd :/$st ...

  9. oracle入门(2)—— 使用图形工具navicat for oracle

    [本文介绍] 本文将介绍如何使用图形工具navicat for oracle连接本地数据库 以及远程访问 服务器数据库. [下载地址] http://www.navicat.com.cn/downlo ...

  10. 在Linux下查找文件内容包含某个特定字符串的文件

    如何在Linux下查找文件内容包含某个特定字符串的文件? 我的目录下面有test1和test2两个文件夹,里面都含有很多文件,其中test2里面还包含一个test文件夹 我想请问的是,如何通过查找关键 ...