LeetCode 66. Plus One
来源:互联网 发布:天音淘宝复制 编辑:程序博客网 时间:2024/06/07 23:45
题意
将一个存于数组中组成的数字加一
思路
直接模拟
代码
class Solution {public: vector<int> plusOne(vector<int>& digits) { string num1 = ""; for(int i = 0; i < digits.size(); i++) { num1 += digits[i] + '0'; } num1 = add(num1, "1"); vector<int>ans; for(int i = 0; i < num1.length(); i++){ ans.push_back(num1[i] - '0'); } return ans; }private: string add(string A, string B){ int lenA = A.length(); int lenB = B.length(); string ans = ""; lenA--;lenB--; int pos = 0; while(lenA >= 0 && lenB >= 0){ int sum = A[lenA--] + B[lenB--] - '0' - '0' + pos; ans += sum % 10 + '0'; pos = sum / 10; } while(lenA >= 0){ int sum = A[lenA--]- '0' + pos; ans += sum % 10 + '0'; pos = sum / 10; } while(lenB >= 0){ int sum = B[lenB--]- '0' + pos; ans += sum % 10 + '0'; pos = sum / 10; } if(pos) ans += pos + '0'; reverse(ans.begin(), ans.end()); return ans; }};
阅读全文
0 0
- [LeetCode]66.Plus One
- LeetCode --- 66. Plus One
- [Leetcode] 66. Plus One
- [leetcode] 66.Plus One
- [leetcode] 66. Plus One
- Leetcode-66. Plus One
- 【leetcode】66. Plus One
- 66. Plus One LeetCode
- 【LeetCode】66. Plus One
- [LeetCode]66. Plus One
- 【LeetCode】66. Plus One
- LeetCode 66. Plus One
- LeetCode *** 66. Plus One
- leetcode 66. Plus One
- 【leetcode】66. Plus One
- leetcode 66. Plus One
- [LeetCode]66. Plus One
- Leetcode 66. Plus One
- Spring框架自学笔记
- JNI/NDK开发指南(三)——JNI数据类型及与Java数据类型的映射关系
- dpkg基本操作
- Session变量不能传送到下一页.解决: session.use_trans_sid = 1
- 编译libeXosip以及向代理服务器注册
- LeetCode 66. Plus One
- JNI/NDK开发指南(四)——字符串处理
- EPS图片范围切割
- 用opencv打开苹果手机ip_camera
- redis 发布/订阅模式
- word转pdf时存在的一些问题
- Spring Integration概述
- 简易的万年历程序C语言
- NI/NDK开发指南(五)——访问数组(基本类型数组与对象数组)