LeetCode 第66题,加一

题目概述

  • 题目:力扣:66.加一
  • 难易:简单
  • 内容:

    给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。

    最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。

    你可以假设除了整数 0 之外,这个整数不会以零开头。

    示例 1:

    输入: [1,2,3]
    输出: [1,2,4]
    解释: 输入数组表示数字 123。

    示例 2:

    输入: [4,3,2,1]
    输出: [4,3,2,2]
    解释: 输入数组表示数字 4321。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/plus-one

第一次思路

使用while循环将数组变为一个整数,然后计算的num有几位数,再倒着循环,将整数变为数组。

Code

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
       int num=0;
       int i=0;
       int a;
       for(int i(0);i<digits.size();i++){
           num=num*10+digits[i];
       }
       num++;
       while(num/10!=0){
        i++;
       }
       do{
          a= num%10;
          digits[i]=a;
          i--;
          num=num/10;
       }while(i==0);
       return digits;
    }
};

测试 Submit

分析

运行时间过长

改进

重新整理思路:
一共分为三种情况:
①.数组中最后一位不是9 ,加一后不会进位,如:[1,2,3],加一后仍然是三位
②.数组中最后有9,但第一位不是9,加一后仍然不会改变数组的元素数,如[1,9,9,9]
③.数组中全书9,加一后会增加一个元素,如[9,9,9]
解决方式:
①.来时只要倒序循环找到9就变为0,直到最后一个连续的9为止,在前一位元素加一即可
②.最后一种情况要把第一个元素变为0,最后一位加上一个0;

改进Code

class Solution {
public:
    vector<int> plusOne(vector<int>& digits) {
        int i=digits.size()-1;
       for(i;i>=0;i--){
           if(digits[i]==9)
                digits[i]=0;
            else {
                digits[i]++;
                break;
            };
       }
       if(digits[0]==0)
       {
            digits[0]=1;
            digits.push_back(0);
       }
       return digits;
    }
};

改进Submit

收获总结

push_back是编程语言里面的一个函数名。如c++中的vector头文件里面就有这个push_back函数,在vector类中作用为在vector尾部加入一个数据。

原文地址:https://www.cnblogs.com/HanLongfeng/p/12077883.html

时间: 2024-07-29 08:36:49

LeetCode 第66题,加一的相关文章

leetcode第66题 (array)

题目: 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. 即用数组代表一个数,给这个数加1后,继续用数组表示. 比较简单,注意进位就可以了,尤其是要注意最高项的进位,容易忽视. 1 vector<int

LeetCode第[66]题(Java):Plus One

题目:数组加一 难度:Easy 题目内容: Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a singl

LeetCode 第三题,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", which the length is 3. For "bbbbb" the longest s

leetcode第9题-Palindrom Number

这是leetcode的第九题,相对来说比较简单,目的很简单,就是判断一个int型的数是不是回文数.但是有几点需要考虑: 负数应该没有回文数,要加判断!要注意额外的空间申请问题.判断是否是回文数势必要对一个数进行反转,反转的时候就要考虑溢出的问题.实现的代码如下: #include<stdio.h> bool isPalindrom(int x) { if(x<0) return false; else { int tmp=x; int sum=0; while(tmp) { sum=su

LeetCode 第 263 题 (Ugly Number)

LeetCode 第 263 题 (Ugly Number) Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another pr

LeetCode第五题,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. 题意解析: 最长回文子串.就是给定一个字符串S,找出其中的最长回文子串,并返回该子串. 解法: 第一种方法显然是循环暴力枚举,复杂度为O(

LeetCode 第 73 题 (Set Matrix Zeroes)

LeetCode 第 73 题 (Set Matrix Zeroes) Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple impro

leetcode中第一题twosum问题解答算法的可行性证明

leetcode中第一题twosum问题解答算法的可行性证明 一.引入 关于leetcode中第一题twosum问题,网上已有不少高人做出过解答,并提出了切实可行的算法实现.我在解答该题时参考了博客http://www.zixue7.com/article-9576-1.html的解答.为让读者更直观地阅读和理解本文,先简要摘录以上博客的内容如下: 题目还原 Two Sum Given an array of integers, find two numbers such that they a

Leetcode第五题_Longest Palindromic Substring

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. Leetcode第5题,题目大概意思是给一个字符串,从中找出最长的回文串,所谓回文串,就是