题目:

  Given a non-negative integer represented as anon-empty array of digits, plus one to the integer.

      You may assume the integer do not contain any leading zero, except the number 0 itself.

      The digits are stored such that the most significant digit is at the head of the list.

  将一个数字的每个位上的数字分别存到一个一维向量中,最高位在最开头,需要给这个数加1,即在末尾数字加1,如果末尾数字是9,就会有进位问题,如果前一位数字仍然是9,则需继续向前进位。

思路:

   我的思路,判断最后一位数是否小于9,若小于9则加1处理,否则前一位加1,后一位为0。

LeetCode编程练习 - Plus One学习心得


    虽然后程序部分实现了,但是,当我输入的数字为[9]时,加一输出为[1],而不是[1,0],检查程序后发现对数组的长度并没做更改,若加1更改后需要重新定义一个新的数组,用来装载长度发生变化后的数。

    与解决方案相比,在表达方式上有些不同。
    更改后的代码以及解决方案:
LeetCode编程练习 - Plus One学习心得   LeetCode编程练习 - Plus One学习心得
   


相关文章:

  • 2021-10-03
  • 2021-04-06
  • 2021-12-11
  • 2021-06-26
  • 2021-04-24
  • 2021-06-02
  • 2021-09-27
  • 2021-10-13
猜你喜欢
  • 2021-05-13
  • 2021-06-05
  • 2021-11-12
  • 2021-06-25
  • 2021-08-04
  • 2021-11-16
  • 2021-06-04
相关资源
相似解决方案