给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。

注意:

  1. num1 和num2 的长度都小于 5100.
  2. num1 和num2 都只包含数字 0-9.
  3. num1 和num2 都不包含任何前导零。
  4. 你不能使用任何內建 BigInteger 库, 也不能直接将输入的字符串转换为整数形式。
class Solution {
public:
string addStrings(string num1, string num2) {
int len1 = num1.size();
int len2 = num2.size();
string str = "";
int i, j;
int z = 0;
for(i = len1 - 1, j = len2 - 1; i >= 0 && j >= 0; i--, j--)
{
int x = num1[i] - '0';
int y = num2[j] - '0';
int temp = (x + y + z) % 10;
z = (x + y + z) / 10;
str = (char)('0' + temp) + str;
} if(i >= 0)
{
for(; i >= 0; i--)
{
int x = num1[i] - '0';
int temp = (x + z) % 10;
z = (x + z) / 10;
str = (char)('0' + temp) + str;
}
}
else if(j >= 0)
{
for(; j >= 0; j--)
{
int y = num2[j] - '0';
int temp = (y + z) % 10;
z = (y + z) / 10;
str = (char)('0' + temp) + str;
}
} if(z > 0)
{
str = (char)('0' + z) + str;
}
return str; }
};

Leetcode415Add 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] 415. Add Strings 字符串相加

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

  3. 415 Add Strings 字符串相加

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

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

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

  5. String字符串相加的原理

    因为String是非常常用的类, jvm对其进行了优化, jdk7之前jvm维护了很多的字符串常量在方法去的常量池中, jdk后常量池迁移到了堆中 方法区是一个运行时JVM管理的内存区域,是一个线程共 ...

  6. T-SQL字符串相加之后被截断的那点事

    本文出处:http://www.cnblogs.com/wy123/p/6217772.html 字符串自身相加, 虽然赋值给了varchar(max)类型的变量,在某些特殊情况下仍然会被“截断”,这 ...

  7. C语言关于利用sscanf实现字符串相加减

    #include<stdio.h>#include<string.h>void main(){ int a; int b; char str1[10] = "9999 ...

  8. 【转】String字符串相加的问题

    String字符串相加的问题 前几天同事跟我说我之前写的代码中在操作字符串时候,使用字符串相加的方式而不是使用StringBuffer或者StringBuilder导致内存开销很大.这个问题一直在困扰 ...

  9. String 字符串相加比较

    String 字符串相加 对比 public static void main(String[] args) { String a = "helloword"; final Str ...

随机推荐

  1. 2019-8-31-dotnet-特性-DynamicallyInvokable-是用来做什么的

    title author date CreateTime categories dotnet 特性 DynamicallyInvokable 是用来做什么的 lindexi 2019-08-31 16 ...

  2. drupal-note2 drush运行make文件

    进入durpal项目的根目录中执行 drush make build-openpublic.make /path/to/webroot 参考: Managing Drush make files fo ...

  3. springboot中activeMQ消息队列的引入与使用(发送短信)

    1.引入pom依赖 <!--activemq--><dependency> <groupId>org.springframework.boot</groupI ...

  4. leetcode-154-寻找旋转排序数组中的最小值

    题目描述: 方法一: class Solution: def findMin(self, nums: List[int]) -> int: left, right = 0, len(nums) ...

  5. 【JZOJ3236】矮人排队

    description 在七山七海之外的一个小村庄,白雪公主与N个矮人住在一起,所有时间都花在吃和玩League of Legend游戏.白雪公主决心终结这样的生活,所以为他们举办了体育课. 在每节课 ...

  6. CSS工程化

    CSS工程化 引言: 你在编写CSS代码时,是否遇到过这样的问题: 代码没有层次结构,难以看出嵌套关系 .site-footer .footer-container .footer-menu { di ...

  7. 菜鸟nginx源码剖析数据结构篇(六) 哈希表 ngx_hash_t(上)[转]

    菜鸟nginx源码剖析数据结构篇(六) 哈希表 ngx_hash_t(上) Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.c ...

  8. 杂项-公司:Axway

    ylbtech-杂项-公司:Axway Axway 公司是法国Sopra 集团从事应用系统集成(EAI/B2Bi)软件及相关咨询服务业务的全资子公司.Axway公司成立于1980年,总部位于美国凤凰城 ...

  9. PAT甲级——A1004 Counting Leaves

    A family hierarchy is usually presented by a pedigree tree. Your job is to count those family member ...

  10. 12_PCA之探究用户对物品类别的喜好细分降维

    案例: 探究:用户对物品类别的喜好细分降维. 背景:把用户分成几个类别,分类的依据是用户购买了哪些物品. 先看商品products.csv数据,有product_id,product_name,ais ...