#Leetcode# 989. Add to Array-Form of Integer
https://leetcode.com/problems/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 <= 10000
0 <= A[i] <= 9
0 <= K <= 10000
- If
A.length > 1
, thenA[0] != 0
代码:
class Solution {
public:
vector<int> addToArrayForm(vector<int>& A, int K) {
int n = A.size();
vector<int> ans;
string words1 = toString(K), words2 = "";
for(int i = 0; i < n; i ++)
words2 += ('0' + A[i]);
string sum = addStrings(words1, words2);
for(int i = 0; i < sum.length(); i ++)
ans.push_back(sum[i] - '0');
return ans;
}
string toString(int x) {
string s = "";
while(x) {
s += (x % 10) + '0';
x /= 10;
}
for(int i = 0; i < s.length() / 2; i ++)
swap(s[i], s[s.length() - i - 1]);
return s;
}
string addStrings(string num1, string num2) {
string c = "";
int len1 = num1.length();
int len2 = num2.length();
int len = max(len1, len2);
for(int i = len1; i < len; i ++)
num1 = "0" + num1;
for(int i = len2; i < len; i ++)
num2 = "0" + num2;
int ok = 0;
for(int i = len - 1; i >= 0; i --) {
char temp = num1[i] + num2[i] - '0' + ok;
if(temp > '9') {
ok = 1;
temp -= 10;
}
else ok = 0;
c = temp + c;
}
if(ok) c = "1" + c;
return c;
}
};
数组模拟大数加法
#Leetcode# 989. Add to Array-Form of Integer的更多相关文章
- 【LeetCode】989. Add to Array-Form of Integer 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数组转整数再转数组 模拟加法 日期 题目地址:htt ...
- 【leetcode】989. 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 o ...
- LC 989. 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. ...
- 【Leetcode_easy】989. Add to Array-Form of Integer
problem 989. Add to Array-Form of Integer 参考 1. Leetcode_easy_989. Add to Array-Form of Integer; 完
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- LeetCode:1. Add Two Numbers
题目: LeetCode:1. Add Two Numbers 描述: Given an array of integers, return indices of the two numbers su ...
- [LeetCode] 415. Add Strings_Easy tag: String
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2 ...
- [LeetCode] 445. Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
随机推荐
- Linux 小知识翻译 - 「NTP」
这周聊聊「NTP」. 上次,聊了「时区」,也就是时间相关的话题. NTP是「Network Time Protocol」的简称,是为了将网络中计算机的时钟同步到正确时间的协议. PC内部的时钟是相当不 ...
- Linux 小知识翻译 - 「GCC」
这次聊聊「GCC」. GCC是「GNU Compiler Collection」的简称,由C.C++.FORTRAN.Java等语言的编译器以及这些语言的库所组成. GCC不仅包含编译器本身,还包含了 ...
- CSS多行文本垂直居中
今天需要将文本垂直居中,就是一行是垂直居中,多行也是垂直居中. 效果如下 实现代码(同事提供) <!DOCTYPE html> <html> <head lang=&qu ...
- 微服务---Eureka注册中心(服务治理)
在上一篇的初识SpringCloud微服务中,我们简单讲解到服务的提供者与消费者,当服务多了之后,会存在依赖与管理之间混乱的问题,以及需要对外暴露自己的地址,为了解决此等问题,我们学习Eureka注册 ...
- 【工具大道】ssh登录Linux服务器,并显示图形化界面
本文地址 点击关注微信公众号 "程序员的文娱情怀" 分享提纲: 1. 概述 2. mac版实现ssh登录,显示图形化 1. 概述 平时ssh登录到Linux服务器都是在命令行下进行 ...
- JavaScript的对象详解
JavaScript对象的概述 什么是对象,代表现实中的某个事物, 是该事物在编程中的抽象,多个数据的集合体(封装体),用于保存多个数据的容器 为什么要用对象,便于对多个数据进行统一管理 对象属于一种 ...
- 使用Eclipse打jar包 包含依赖jar包
1.在项目根目录新建MANIFEST.MF文件 //版本号 Manifest-Version: 1.0 //依赖jar包路径 多个用空格隔开 Class-Path: lib/commons-loggi ...
- UVA12107-Digit Puzzle(迭代加深搜索)
Problem UVA12107-Digit Puzzle Accept:85 Submit:612 Time Limit: 3000 mSec Problem Description Inpu ...
- [tool] google搜索的正确使用姿势(待补全)
第一,也是非常重要的前提,请一定要能FQ.如果这条没有解决,没有往下的必要 现在我假设你已经能FQ了,个人推荐使用搜索引擎的顺序: Google>微软bing国际搜索>百度 (百度总能给你 ...
- 磁盘性能评价指标—IOPS和吞吐量
转:http://blog.csdn.net/hanchengxi/article/details/19089589 一.磁盘 I/O 的概念 I/O 的概念,从字义来理解就是输入输出.操作系统从上层 ...