LeetCode Array Easy 66. Plus One
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的更多相关文章
- LeetCode Array Easy 88. Merge Sorted Array
Description Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted ar ...
- LeetCode Array Easy 485. Max Consecutive Ones
Description Given a binary array, find the maximum number of consecutive 1s in this array. Example 1 ...
- 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 ...
- 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 ...
- 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 ...
- LeetCode Array Easy 268. Missing Number
Description Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one th ...
- LeetCode Array Easy 219. Contains Duplicate II
---恢复内容开始--- Description Given an array of integers and an integer k, find out whether there are two ...
- LeetCode Array Easy 217. Contains Duplicate
Description Given an array of integers, find if the array contains any duplicates. Your function sho ...
- LeetCode Array Easy 189. Rotate Array
---恢复内容开始--- Description Given an array, rotate the array to the right by k steps, where k is non-ne ...
随机推荐
- new做了些什么?
new做了些什么? function People(name, age){ this.name = name; this.age = age; }; var xiaoming = new People ...
- iOS 点击按钮截屏
@interface CaptureViewController () @property (nonatomic, strong) UIImageView *backgrounView; //控制器背 ...
- linux100day(day6)--shell脚本简单逻辑
if语句: if条件语句的使用格式: 1.单分支语句 if 条件;then 执行语句 fi 2.双分支语句 if 条件;then 执行语句1 else 执行语句2 fi 3.多分支语句 if 条件;t ...
- Nginx之Keepalived
目录 Nginx之Keepalived 1. Keepalived 高可用基本概述 1.1 什么是高可用 1.2 高可用通常使用什么软件? 1.3 keepalived是如何实现高可用的? 1.4 那 ...
- shell巡检草拟
#!/bin/bash phy_cpu=$(cat /proc/cpuinfo | grep "physical id"|sort | uniq | wc -l) logic_cp ...
- deep features for text spotting 在linux,windows上使用
做文本检测这个方向的同学应该都知道 deep features for text spotting 这篇ECCV14的文章. 用的是Matconvnet这个是深度学习框架来做文本检测,同时他还提供了代 ...
- OpenCV常用基本处理函数(8)图像变换
傅里叶变换 傅里叶变换在实际中有非常明显的物理意义,设f是一个能量有限的模拟信号,则其傅里叶变换就表示f的频谱. 图像的频率是表征图像中灰度变化剧烈程度的指标,是灰度在平面空间上的梯度.如:大面积的沙 ...
- css雪碧图实现数字切换
vue中 css 雪碧图应用及数字切换demo 1. CSS Sprites一般只能使用到固定大小的盒子(box)里,这样才能够遮挡住不应该看到的部分. 2.使用css雪碧图的优点: 利用CSS Sp ...
- 理解Java主函数中的"String[] args"
public class Understand_String_args { public static void main(String[] args) { System.out.printf(&qu ...
- 【leetcode】979. Distribute Coins in Binary Tree
题目如下: Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and th ...