leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit 308a71e413c0fabf54727efaf5befc77d72fb362
parent d3c844ce2aef16dbc9fe26fce617f68a1cb1a2a4
Author: Jack Sangdahl <jack@pngu.org>
Date:   Wed, 31 Dec 2025 21:35:25 -0700

66. plus one

Diffstat:
Aplus_one.c | 49+++++++++++++++++++++++++++++++++++++++++++++++++
Aplus_one.py | 30++++++++++++++++++++++++++++++
2 files changed, 79 insertions(+), 0 deletions(-)

diff --git a/plus_one.c b/plus_one.c @@ -0,0 +1,49 @@ +// 66. Plus One +#include "leetcode.h" + +/* + * you are given a large integer represented as an integer array 'digits', where + * each 'digits[i]' is the i'th digit of thee integer. the digits are ordered + * from most significant to least signifcant in left to right order. the large + * does not contain any leading zeros. increment the large integer by one and + * return the resulting array of digits. + */ + +int *plusOne(int *digits, int digitsSize, int *returnSize) { + for (int i = digitsSize - 1; i >= 0; i--) { + if (digits[i] < 9) { + digits[i]++; + *returnSize = digitsSize; + return digits; + } + digits[i] = 0; + } + int *ans = (int *)malloc((digitsSize + 1) * sizeof(int)); + ans[0] = 1; + for (int i = 1; i <= digitsSize; i++) + ans[i] = 0; + *returnSize = digitsSize + 1; + return ans; +} + +int main() { + int d1[] = {1, 2, 3}, d2[] = {4, 3, 2, 1}, d3[] = {9}; + int rs1, *po1 = plusOne(d1, ARRAY_SIZE(d1), &rs1); + int rs2, *po2 = plusOne(d2, ARRAY_SIZE(d2), &rs2); + int rs3, *po3 = plusOne(d3, ARRAY_SIZE(d3), &rs3); + for (int i = 0; i < rs1; i++) + printf("%d ", po1[i]); // expect: 1 2 4 + printf("\n"); + for (int i = 0; i < rs2; i++) + printf("%d ", po2[i]); // expect: 4 3 2 2 + printf("\n"); + for (int i = 0; i < rs3; i++) + printf("%d ", po3[i]); // expect: 1 0 + printf("\n"); + if (po1) + free(po1); + if (po2) + free(po2); + if (po3) + free(po3); +} diff --git a/plus_one.py b/plus_one.py @@ -0,0 +1,30 @@ +# 66. Plus One + +""" +you are given a large integer represented as an integer array 'digits', where +each 'digits[i]' is the i'th digit of thee integer. the digits are ordered +from most significant to least signifcant in left to right order. the large +does not contain any leading zeros. increment the large integer by one and +return the resulting array of digits. +""" + + +class Solution(object): + def plusOne(self, digits): + """ + :type digits: List[int] + :rtype: List[int] + """ + for i in range(len(digits) - 1, -1, -1): + if digits[i] < 9: + digits[i] += 1 + return digits + digits[i] = 0 + return [1] + digits + + +if __name__ == "__main__": + obj = Solution() + print(obj.plusOne(digits=[1, 2, 3])) + print(obj.plusOne(digits=[4, 3, 2, 1])) + print(obj.plusOne(digits=[9]))