123th LeetCode Weekly Contest Add to Array-Form of Integer
For a non-negative integer X, the array-form of X is an array of its digits in left to right order. For example, if X = 1231, then the array form is [1,2,3,1].
Given the array-form A of a non-negative integer X, return the array-form of the integer X+K.
Example 1:
Input: A = [1,2,0,0], K = 34
Output: [1,2,3,4]
Explanation: 1200 + 34 = 1234
Example 2:
Input: A = [2,7,4], K = 181
Output: [4,5,5]
Explanation: 274 + 181 = 455
Example 3:
Input: A = [2,1,5], K = 806
Output: [1,0,2,1]
Explanation: 215 + 806 = 1021
Example 4:
Input: A = [9,9,9,9,9,9,9,9,9,9], K = 1
Output: [1,0,0,0,0,0,0,0,0,0,0]
Explanation: 9999999999 + 1 = 10000000000
Note:
1 <= A.length <= 100000 <= A[i] <= 90 <= K <= 10000- If
A.length > 1, thenA[0] != 0
这题肯定有简单解法,写大数加法只是更熟练而已
class Solution {
public:
vector<int> addToArrayForm(vector<int>& A, int K) {
int len = A.size();
int num=;
vector<int>Ve;
int a[]={},b[]={},c[]={};
for(int i=len-;i>=;i--){
a[num++]=A[i];
//cout<<A[i]<<endl;
}
int cnt=;
int i;
while(K){
b[cnt++]=K%;
K/=;
}
//cout<<"B"<<endl;
int k=max(len,cnt);
for(i=;i<k;i++)
{
//cout<<a[i]<<" "<<b[i]<<endl;
c[i]=a[i]+b[i];
}
for(i=; i<k; i++){
//cout<<"A1"<<endl;
if(c[i]>=){
c[i+]+=c[i]/;
c[i]%=;
}
//cout<<"A2"<<endl;
}
if(c[k]==) k--;
for(i=k;i>=;i--){
Ve.push_back(c[i]);
}
return Ve;
}
};
123th LeetCode Weekly Contest Add to Array-Form of Integer的更多相关文章
- 123th LeetCode Weekly Contest Broken Calculator
On a broken calculator that has a number showing on its display, we can perform two operations: Doub ...
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- 【LeetCode Weekly Contest 26 Q3】Friend Circles
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...
- 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II
[题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...
随机推荐
- cakephp中sql查询between
$trading_list = $this->Trading->find('all', array('conditions' => array('buy_time BETWEEN ? ...
- [SoapUI] 按照 Test Step Type 获取所有满足条件的 Test Step
获取当前测试用例下所有Groovy Script类型的测试步骤 def testStepList = testRunner.testCase.getTestStepsOfType(com.eviwar ...
- Asp.net相关知识和经验的碎片化记录
1.解决IIS7.0下“HTTP 错误 404.15 - Not Found 请求筛选模块被配置为拒绝包含的查询字符串过长的请求”问题 方案1:在程序的web.config中system.web节点里 ...
- Servlet面试题
Servlet运行在Servlet容器中,其生命周期由容器来管理.Servlet的生命周期通过javax.servlet.Servlet接口中的init().service()和destroy()方法 ...
- javascript的那些事儿你都懂了吗
javascript从开始的验证表单的脚本语言发展到现在能运行在服务器上,其影响力不断的提升.自己作为一个做前端的,编写js是必不可少,从自己学习js的历程来看其实也是比较吃力.要 学好它,还是的花费 ...
- sql 两大类 DDL数据定义语言 和DCL数据控制语言
SQL分为五大类: DDL:数据定义语言 DCL:数据控制语言 DML:数据的操纵语言 DTL:数据事务语言 DQL:数据查询语言. DDL (date definition lang ...
- 14、Semantic-UI之菜单样式
14.1 基础菜单样式 在Semantic-UI中使用class="ui menu". 示例:定义基础菜单样式 <div class="ui menu" ...
- [示例] 使用 TStopwatch 计时
使用 TStopwatch 计时 uses System.Diagnostics; var t1: TStopwatch; begin t1 := TStopwatch.StartNew; // do ...
- Robot Framework资料
https://www.cnblogs.com/pachongshangdexuebi/category/981644.html 虫师 :http://www.cnblogs.com/fnng/ ...
- Luckily general gradient for spherical harmonics is defined
http://web4.cs.ucl.ac.uk/staff/j.kautz/publications/gradientSH_RS04.pdf