Description

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 single digit.

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

Example 1:

Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Example 2:

Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

  题目理解:输入一个数组,数组中的每个元素都不大于10,每一个元素相当于一个正整数每一位的数,输出这个正整数加一之后的数,用数组表示

  分析:1,如果当前为不为9则直接ji当前位加1即可

     2,如果为9 则当前位置为0,继续1的操作。

我的解决方法

public class Solution {
public int[] PlusOne(int[] digits) {
int[] result = null;
int i = digits.Length - ;
while (i >= && digits[i] + == )
{
digits[i] = ;
i--;
}
if (i >= )
{
digits[i] = digits[i] + ;
return digits;
}
result = new int[digits.Length +]; result[] = ;
return result; }
}

288ms

另一种更好的解决方案(264ms)

public class Solution {
public int[] PlusOne(int[] digits) { int n = digits.Length; for(int i= n-; i>=;i--){
if(digits[i]<){
digits[i]++;
return digits;
}
digits[i]= ;
} int[] answer = new int[n+];
answer[] = ;
return answer;
}
}

LeetCode Array Easy 66. Plus One的更多相关文章

  1. LeetCode Array Easy 88. Merge Sorted Array

    Description Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted ar ...

  2. LeetCode Array Easy 485. Max Consecutive Ones

    Description Given a binary array, find the maximum number of consecutive 1s in this array. Example 1 ...

  3. LeetCode Array Easy 448. Find All Numbers Disappeared in an Array

    Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear ...

  4. LeetCode Array Easy 414. Third Maximum Number

    Description Given a non-empty array of integers, return the third maximum number in this array. If i ...

  5. LeetCode Array Easy 283. Move Zeroes

    Description Given an array nums, write a function to move all 0's to the end of it while maintaining ...

  6. LeetCode Array Easy 268. Missing Number

    Description Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one th ...

  7. LeetCode Array Easy 219. Contains Duplicate II

    ---恢复内容开始--- Description Given an array of integers and an integer k, find out whether there are two ...

  8. LeetCode Array Easy 217. Contains Duplicate

    Description Given an array of integers, find if the array contains any duplicates. Your function sho ...

  9. LeetCode Array Easy 189. Rotate Array

    ---恢复内容开始--- Description Given an array, rotate the array to the right by k steps, where k is non-ne ...

随机推荐

  1. docker--container的port映射

    使用nginx为例 先运行nginx [root@localhost ~]# docker run --name web -d nginx Unable to find image 'nginx:la ...

  2. 使用VS 2019发布.net core程序并部署到IIS的最新教程

    不管你是使用.net core开发的是web api还是网站类的程序,如果你是部署到IIS,那么下面的内容都适合于你,不会将.net core程序部署到IIS的朋友,可以看看这篇手把手教你部署.net ...

  3. Redis 设置权限密码,以及如何开启关闭设置

    linux redis 设置密码:   在服务器上,这里以linux服务器为例,为redis配置密码. 1.第一种方式 (当前这种linux配置redis密码的方法是一种临时的,如果redis重启之后 ...

  4. PNG文件格式

    PNG文件的组成 一个PNG文件可以看作是由多个数据块(chunk)部分组成,如同积木一样,一个数据块就是一个小积木,不同类型的积木组合搭建成了我们的PNG图像. PNG图像至少由文件署名域和三个关键 ...

  5. 【QT学习】数独游戏

    前几天刷leetcode刷到一题,讲sudokuSolver,写完感觉很有意思,遂想做一个数独游戏,百度了一下如何自动生成题库,参考某位大神安卓下的实现思路,自己做了一套文字版的数独游戏,后来想乘机会 ...

  6. 重学JavaScript - 映射与集合

    作者:狐狸家的鱼 GitHub:surRimn 整理自MDN文档 带键的集合 映射 Map对象 一个Map对象在迭代时会根据对象中元素的插入顺序来进行 — 一个 for...of 循环在每次迭代后会返 ...

  7. TCP/IP的分层管理_01

    1.TCP/IP协议族里最重要的一点就是分层.TCP/IP协议族按层次分别分为以下4层:         应用层,传输层,网络层和数据链路层.           应用层:决定了向用户提供应用服务时通 ...

  8. 【和孩子一起学编程】 python笔记--第三天

    第十章 游戏时间:Skier 首先安装pygame,直接在cmd命令控制框里键入pip install pygame就可以了 代码: import pygame, sys, random skier_ ...

  9. selenuim模块的使用 解析库

    selenium: 是自动化测试工具,我们可以用它来进行爬虫. 可以驱动浏览器,执行自定义好的任务. 可以执行js代码 执行速度慢,效率低. 一般用于做登录的认证 基本选择器: find_elemen ...

  10. nucleus plus学习总结(后续)

    前言:     刚刚抽筋点了保存发布,结果要审核,那就分开写个续好了. 内容: signal     信号是异步通知task的一种机制,HISR是不可以接收信号的,但是可以发送信号.     TCB中 ...