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. C#简单的文件依赖缓存的使用

    一,FileCache.aspx页面 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind=& ...

  2. Hibernate4教程六:性能提升和二级缓存

    抓取策略(fetching strategy)是指:当应用程序需要在(Hibernate实体对象图的)关联关系间进行导航的时候,Hibernate如何获取关联对象的策略.抓取策略可以在O/R映射的元数 ...

  3. js高级编程思想

    js惰性思想: 能够执行一次就搞定绝对不会执行第二次 function createXHR(){ var xhr=null, falg=false, ary=[ function(){ return ...

  4. [FILE API]利用Javascript上传图片

    1.HTML结构 <label for="file-input">File:</label> <input type="file" ...

  5. 九、结构模式之装饰(Decorator)模式

    装饰模式又叫包装模式,装饰模式以客户端透明的方式扩展对象的功能,是继承关系的一个替代方案.装饰模式可以在不使用创造更多的子类的情况下,将对象的功能加以扩展. 装饰模式结构图如下: 其包含的角色就分为: ...

  6. Netflix:我们为什么要将GraphQL引入前端架构?

    作者|Artem Shtatnov译者|无明 在这篇文章中,我们将分享 Netflix 在这些应用程序的前端架构中引入 GraphQL 所积累的经验. 在内部,我们把用于管理广告创建和组装的主要应用程 ...

  7. C语言——enum

    #include<stdio.h> enum Season { spring, summer=100, fall=96, winter }; typedef enum { Monday, ...

  8. jQuery判断checkbox是否选中的4种方法

    方法一: ).checked) { // do something } 方法二: if($('#checkbox-id').is(':checked')) { // do something } 方法 ...

  9. atexit函数学习

    函数名: atexit 头文件:#include<stdlib.h> 功 能: 注册终止函数(即main执行结束后调用的函数) 用 法: void atexit(void (*func)( ...

  10. spring+websocket的整合实例--可使用

    spring+websocket的整合实例----借鉴如下链接--此贴用于笔记 https://blog.csdn.net/qq_35515521/article/details/78610847