leetcode #66 in cpp

原创 2016年05月31日 01:48:14

Solution: 

1.start from the tail of the input, and keep adding 1 until we reach the head of the input or the digit + 1 is less than 10. 

2.check if the head >= 10. If so insert an extra 1 to the input.

Code:

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int i = digits.size() - 1;
        int carry = 1;
        while(carry && i >=0){
            digits[i]+=carry;
            if(digits[i] >=10){
                carry = 1;
                digits[i]%=10;
            }else carry = 0;
            i--;
        }
        if(carry){
            digits.insert(digits.begin(), 1);
        }
        return digits;
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode #32 in cpp

Given a string containing just the characters '(' and ')', find the length of the longest valid (we...

leetcode #114 in cpp

Given a binary tree, flatten it to a linked list in-place. For example, Given 1 ...

leetcode #160 in cpp

Write a program to find the node at which the intersection of two singly linked lists begins. F...

Leetcode 285. Inorder Successor in BST (Medium) (cpp)

Leetcode 285. Inorder Successor in BST (Medium) (cpp)

leetcode #98 in cpp

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined...

leetcode #37 in cpp

The question is to solve a Sudoku. Solution: We scan through the Sudoku. Whenever we meet a '.', w...

leetcode #77 in cpp

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exa...

leetcode #139 in cpp

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa...

leetcode #18 in cpp

The question is similar to 3sum. And how to solve it is thus very similar to the solution of 3sum.  ...

leetcode #44 in cpp

Solution:  We use DP to solve this problem.  Initialize bool dp[pattern length + 1][ s length + 1]...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)