码迷,mamicode.com
首页 > 其他好文 > 详细

36. leetcode 415. Add Strings

时间:2017-04-09 22:09:26      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:整数   字符串   处理   etc   分享   .com   images   rar   img   

415. Add Strings

 

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

思路:由于一个字符与一个非负整数等价,所以直接对两个字符串对应字符进行加法运算。诸多细节:首先是向一个string对象指定位置插入字符;然后,为了避免后续处理的麻烦,我直接将两个字符串归一化成一样的长度,用0来补齐;0的ascii为48,在进行字符加法时要十分注意加减48的处理。

技术分享

 

36. leetcode 415. Add Strings

标签:整数   字符串   处理   etc   分享   .com   images   rar   img   

原文地址:http://www.cnblogs.com/vincent93/p/6686627.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!