leetcode 066 Plus One(难易度:Easy)
2015-10-02 12:34
274 查看
Plus One
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.
代码:
/** * Return an array of size *returnSize. * Note: The returned array must be malloced, assume caller calls free(). */ int* plusOne(int* digits, int digitsSize, int* returnSize) { int i; int carry = 1; int *new_digits = NULL; for (i = digitsSize - 1; i >= 0; i--) { int a = digits[i] + carry; digits[i] = a % 10; carry = a / 10; if (carry == 0) break; } if (carry == 1) { digitsSize ++; new_digits = (int*)malloc(digitsSize * sizeof(int)); new_digits[0] = 1; for (i = digitsSize - 2; i >= 0; i--) { new_digits[i + 1] = digits[i]; } //digits = new_digits; //free(digits); *returnSize = digitsSize; return new_digits; } else { *returnSize = digitsSize; return digits; } }原题地址:https://leetcode.com/problems/plus-one/
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解
- LeetCode [Day 5] Longest Palindromic Substring 题解
- LeetCode [Day 6] ZigZag Conversion 题解