leetcode 66 Plus One
生活随笔
收集整理的這篇文章主要介紹了
leetcode 66 Plus One
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給定一個數組,表示整數的各個位數,現要將其加上1,考慮進位。
vector<int> plusOne(vector<int>& digits) {int size = digits.size();bool carry = true;for (int i = size - 1; i >= 0; --i) {if (digits[i] == 9 && carry)digits[i] = 0;else{digits[i]++;return digits;}}if (digits[0] == 0) {vector<int> ret(size + 1, 0);ret[0] = 1;return ret;} }思考:
其實也有最后用insert的,其實insert代價也是O(n),需要后移。
也想過判定全是9可以放在for前面,考慮正常情況下全9出現較少,放后面效率好點,概率,:-)
轉載于:https://www.cnblogs.com/willaty/p/8331855.html
總結
以上是生活随笔為你收集整理的leetcode 66 Plus One的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转】 ConstraintLayout
- 下一篇: 第6章 循环