989 数组形式的整数加法

2023-11-30 18:38
文章标签 数组 形式 整数 加法 989

本文主要是介绍989 数组形式的整数加法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

//对于非负整数 X 而言,X 的数组形式是每位数字按从左到右的顺序形成的数组。例如,如果 X = 1231,那么其数组形式为 [1,2,3,1]。 
//
// 给定非负整数 X 的数组形式 A,返回整数 X+K 的数组形式。 
//
// 
//
// 
// 
//
// 示例 1: 
//
// 输入:A = [1,2,0,0], K = 34
//输出:[1,2,3,4]
//解释:1200 + 34 = 1234
// 
//
// 示例 2: 
//
// 输入:A = [2,7,4], K = 181
//输出:[4,5,5]
//解释:274 + 181 = 455
// 
//
// 示例 3: 
//
// 输入:A = [2,1,5], K = 806
//输出:[1,0,2,1]
//解释:215 + 806 = 1021
// 
//
// 示例 4: 
//
// 输入:A = [9,9,9,9,9,9,9,9,9,9], K = 1
//输出:[1,0,0,0,0,0,0,0,0,0,0]
//解释:9999999999 + 1 = 10000000000
// 
//
// 
//
// 提示: 
//
// 
// 1 <= A.length <= 10000 
// 0 <= A[i] <= 9 
// 0 <= K <= 10000 
// 如果 A.length > 1,那么 A[0] != 0 
// 
// Related Topics 数组 
// 👍 85 👎 0

思路:数字都是从低位到高位计算,一般涉及到用数组或者字符串存储数字的一定要考虑是否溢出的问题!!

大方向是:读取K从低位到高位去和数组对应位置进行计算。

 

        解答成功:
        执行耗时:6 ms,击败了30.76% 的Java用户
        内存消耗:39.9 MB,击败了49.22% 的Java用户


//leetcode submit region begin(Prohibit modification and deletion)
class Solution {public List<Integer> addToArrayForm(int[] A, int K) {//数字运算一定要考虑溢出问题!!!List<Integer> list = new ArrayList<>();int res=0;int cur=0;int i=A.length-1;int add=0;//进位//处理数字while(i>=0||K>0){//相同位置计算if(i<0){cur=K%10+add;}else{cur=K%10+A[i]+add;}list.add(cur%10);add=cur/10;i--;K=K/10;}//处理最高位的进位if(add>0){list.add(add);}Collections.reverse(list);return list;}
}

 

这篇关于989 数组形式的整数加法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/438104

相关文章

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

PTA求一批整数中出现最多的个位数字

作者 徐镜春 单位 浙江大学 给定一批整数,分析每个整数的每一位数字,求出现次数最多的个位数字。例如给定3个整数1234、2345、3456,其中出现最多次数的数字是3和4,均出现了3次。 输入格式: 输入在第1行中给出正整数N(≤1000),在第二行中给出N个不超过整型范围的非负整数,数字间以空格分隔。 输出格式: 在一行中按格式“M: n1 n2 ...”输出,其中M是最大次数,n

uva 10069 DP + 大数加法

代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <cl

整数Hash散列总结

方法:    step1  :线性探测  step2 散列   当 h(k)位置已经存储有元素的时候,依次探查(h(k)+i) mod S, i=1,2,3…,直到找到空的存储单元为止。其中,S为 数组长度。 HDU 1496   a*x1^2+b*x2^2+c*x3^2+d*x4^2=0 。 x在 [-100,100] 解的个数  const int MaxN = 3000

C语言:柔性数组

数组定义 柔性数组 err int arr[0] = {0}; // ERROR 柔性数组 // 常见struct Test{int len;char arr[1024];} // 柔性数组struct Test{int len;char arr[0];}struct Test *t;t = malloc(sizeof(Test) + 11);strcpy(t->arr,

C 语言基础之数组

文章目录 什么是数组数组变量的声明多维数组 什么是数组 数组,顾名思义,就是一组数。 假如班上有 30 个同学,让你编程统计每个人的分数,求最高分、最低分、平均分等。如果不知道数组,你只能这样写代码: int ZhangSan_score = 95;int LiSi_score = 90;......int LiuDong_score = 100;int Zhou

计算数组的斜率,偏移,R2

模拟Excel中的R2的计算。         public bool fnCheckRear_R2(List<double[]> lRear, int iMinRear, int iMaxRear, ref double dR2)         {             bool bResult = true;             int n = 0;             dou

C# double[] 和Matlab数组MWArray[]转换

C# double[] 转换成MWArray[], 直接赋值就行             MWNumericArray[] ma = new MWNumericArray[4];             double[] dT = new double[] { 0 };             double[] dT1 = new double[] { 0,2 };

PHP7扩展开发之数组处理

前言 这次,我们将演示如何在PHP扩展中如何对数组进行处理。要实现的PHP代码如下: <?phpfunction array_concat ($arr, $prefix) {foreach($arr as $key => $val) {if (isset($prefix[$key]) && is_string($val) && is_string($prefix[$key])) {$arr[