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;
}

运行结果

相关推荐
Escalating_xu1 小时前
【C语言常见概念】从第一行代码到字符串结束标志:一次打通编译、main、ASCII、转义字符与注释
c语言·c++
Navigator_Z1 小时前
LeetCode //C - 1250. Check If It Is a Good Array
c语言·算法·leetcode
SuperByteMaster3 小时前
freertos 任务创建方式
c语言
susplus4 小时前
【传感器DS18B20】51单片机上实现温度采集
c语言·51单片机·ds18b20·温度采集
xxwxx__4 小时前
C++ STL set 与 map 全套详解:关联式容器、红黑树底层、代码坑点、刷题实战
数据结构·c++
程曦曦5 小时前
MySQL 生产库误删 98 张表后的时间点恢复实战:从 binlog 解析到资金对账
linux·数据结构·其他·算法·ubuntu·运维开发
郝学胜-神的一滴5 小时前
Effective Python 条款 13:善用星号解包,告别下标切片拆分的坑
服务器·开发语言·数据结构·python·程序人生·pycharm
Logic1015 小时前
C语言/数据结构位运算题解:异或XOR找出流水线上的“独特零件编号“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
山下梅子酒2255 小时前
洛谷-入门-B2043
c语言