LeetCode【66】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.

题目比较简单,直接上代码:

vector<int> plusOne(vector<int>& digits) {
        int cflag=1;;
        for(int i =digits.size()-1;i>=0 && cflag>0;i--)
        {
            if(digits[i]+cflag >= 10)
            {
                digits[i]=0;
                cflag=1;
            }
            else
            {
                digits[i]=digits[i]+cflag;
                cflag=0;
            }
        }
        if(cflag==1)
            digits.insert(digits.begin(),cflag);
        return digits;
    }
时间: 2024-10-20 05:19:20

LeetCode【66】Plus One的相关文章

LeetCode【9】. Palindrome Number --java的实现

Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra spa

LeetCode【5】. Longest Palindromic Substring --java实现

Longest Palindromic Substring 一.题目如下:        Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 题目要求给定字符串的最大对称子字符串,如"aaabccbac

LeetCode【2】. Add Two Numbers--java实现

第二道题 Add Two Numbers 如下:         You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.     Inpu

LeetCode【3】.Longest Substring Without Repeating Characters--算法图解及java实现

第三道题Longest Substring Without Repeating Characters如下:     Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", whic

LeetCode【4】. Median of Two Sorted Arrays --java的不同方法实现

Median of Two Sorted Arrays 这道题确实有点难,想挺久,看别人答案也是不能一下子就明白.题目难度为Hard,原题如下: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 给定两个已

LeetCode【20】Valid Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

LeetCode【1】Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that

LeetCode【2】Add two numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

LeetCode【8】. String to Integer (atoi) --java实现

String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended f