C语言/数据结构数组题解:数字列表加一(Plus One)——进位处理与数组扩展

问题描述

小A是一位正在学习数字魔法的学徒,他有一个神奇的整数列表。每次施展魔法时,他需要将这个列表表示的数字整体加一,并返回加一后的新数字列表。例如,列表 [1,2,3] 表示数字 123,加一后得到 124,因此应该返回 [1,2,4]。但魔法有时会带来进位问题,比如 [9,9] 加一后变成 [1,0,0],列表长度会增加。小A希望你能帮助他高效地完成这个魔法操作。

要求:

  1. 设计一个算法,将输入的数字列表加一,并返回结果列表。
  2. 算法的时间复杂度应为 O(n),其中 n 是列表的长度。
  3. 尽量减少额外空间的使用,以体现你的算法优化能力。

测试样例

样例1:

输入:digits = [1,2,3] 输出:[1,2,4] 解释:数字 123 加一后是 124,所以返回 1,2,4。

样例2:

输入:digits = [9,9] 输出:[1,0,0] 解释:数字 99 加一后是 100,列表长度增加,返回 1,0,0。

样例3:

输入:digits = [0] 输出:[1] 解释:数字 0 加一后是 1,返回 1。

约束条件

  • 1 ≤ digits.length ≤ 100
  • 0 ≤ digitsi ≤ 9
  • 输入列表不包含任何前导零,除了数字0本身

程序代码

#include <stdio.h>

#include <stdlib.h>

/**

* Note: The returned array must be malloced, assume caller calls free().

*/

int* plusOne(int* digits, int digitsSize, int* returnSize) {

// 从末位开始遍历

for (int i = digitsSize - 1; i >= 0; i--) {

if (digitsi < 9) {

// 不需要进位,直接加1返回

digitsi++;

*returnSize = digitsSize;

// 复制结果(避免修改原数组)

int* result = (int*)malloc(digitsSize * sizeof(int));

for (int j = 0; j < digitsSize; j++) {

resultj = digitsj;

}

return result;

} else {

// 当前位是9,置为0,继续进位

digitsi = 0;

}

}

// 所有位都是9,需要扩展数组

*returnSize = digitsSize + 1;

int* result = (int*)malloc((digitsSize + 1) * sizeof(int));

result0 = 1;

for (int i = 1; i <= digitsSize; i++) {

resulti = 0;

}

return result;

}

int main() {

int digits1\[\] = {1, 2, 3};

int digits2\[\] = {9, 9};

int digits3\[\] = {0};

int returnSize1, returnSize2, returnSize3;

int* result1 = plusOne(digits1, 3, &returnSize1);

int* result2 = plusOne(digits2, 2, &returnSize2);

int* result3 = plusOne(digits3, 1, &returnSize3);

for (int i = 0; i < returnSize1; i++) printf("%d ", result1i);

printf("\n");

for (int i = 0; i < returnSize2; i++) printf("%d ", result2i);

printf("\n");

for (int i = 0; i < returnSize3; i++) printf("%d ", result3i);

printf("\n");

free(result1);

free(result2);

free(result3);

return 0;

}

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* plusOne(int* digits, int digitsSize, int* returnSize) {
    // 从末位开始遍历
    for (int i = digitsSize - 1; i >= 0; i--) {
        if (digits[i] < 9) {
            // 不需要进位,直接加1返回
            digits[i]++;
            *returnSize = digitsSize;
            
            // 复制结果(避免修改原数组)
            int* result = (int*)malloc(digitsSize * sizeof(int));
            for (int j = 0; j < digitsSize; j++) {
                result[j] = digits[j];
            }
            return result;
        } else {
            // 当前位是9,置为0,继续进位
            digits[i] = 0;
        }
    }
    
    // 所有位都是9,需要扩展数组
    *returnSize = digitsSize + 1;
    int* result = (int*)malloc((digitsSize + 1) * sizeof(int));
    result[0] = 1;
    for (int i = 1; i <= digitsSize; i++) {
        result[i] = 0;
    }
    return result;
}

int main() {
    int digits1[] = {1, 2, 3};
    int digits2[] = {9, 9};
    int digits3[] = {0};
    
    int returnSize1, returnSize2, returnSize3;
    
    int* result1 = plusOne(digits1, 3, &returnSize1);
    int* result2 = plusOne(digits2, 2, &returnSize2);
    int* result3 = plusOne(digits3, 1, &returnSize3);
    
    for (int i = 0; i < returnSize1; i++) printf("%d ", result1[i]);
    printf("\n");
    for (int i = 0; i < returnSize2; i++) printf("%d ", result2[i]);
    printf("\n");
    for (int i = 0; i < returnSize3; i++) printf("%d ", result3[i]);
    printf("\n");
    
    free(result1);
    free(result2);
    free(result3);
    
    return 0;
}

运行结果

相关推荐
重生之小比特8 小时前
【算法拾光】递归|反转链表 两两交换链表中的结点
数据结构·链表
Navigator_Z9 小时前
LeetCode //C - 1332. Remove Palindromic Subsequences
c语言·算法·leetcode
Code_Solitude9 小时前
c语言:期末复习(7)
c语言·开发语言·算法
金榜题名萱9 小时前
蓝桥杯 - 旗帜
c语言·数据结构·c++·算法·蓝桥杯
jianqiang.xue9 小时前
GCC 15 还是 ST-ARM-Clang:STM32 的 VS Code 工具链怎么定
c语言·arm开发·stm32·嵌入式硬件·嵌入式·ai编程·代码审查
程序员与背包客_CoderZ10 小时前
Linux C++异步网络编程:c-ares高性能DNS解析编码实战
linux·服务器·c语言·开发语言·网络·c++·网络编程
我是伪码农11 小时前
12.5程序设计
数据结构·算法
Shaoshing11 小时前
LeetCode100 152.乘积最大子数组
数据结构·算法·leetcode
凡人叶枫11 小时前
数据结构分类与基本类型:线性、非线性、连续与分散
数据结构·c++
风落青芜11 小时前
C++模板初阶:函数模板与类模板
开发语言·数据结构·c++·算法