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 ...
随机推荐
- NULL合并操作符??
参考官方手册: /** * NULL合并操作符 ?? */ // $a, $b, $c都未声明和定义 var_dump($a??$b??$c); // NULL // $a为数组,$b为100,$c为 ...
- empty视为空的条件
/** * empty视为空的条件: * (1)."" (空字符串) * (2).0 (作为整数的0) * (3).0.0 (作为浮点数的0) * (4)."0" ...
- 【记录】ELK之logstash同步mysql数据到Elasticsearch ,配置文件详解
本文出处:https://my.oschina.net/xiaowangqiongyou/blog/1812708#comments 截取部分内容以便学习 input { jdbc { # mysql ...
- 【记录】docker 安装redis
docker拉取镜像 docker pull redis docker 启动redis docker run -dit -p 6379:6379 --name redis redis:latest - ...
- mysql安装与修改密码
数据库基本概念:数据的仓库 数据库服务器-->数据库-->表-->记录-->属性(列,字段) unix下数据库服务安装: apt-get install -y mysql-se ...
- ganglia监控部署
1.ganglia组件 ganglia 相比于falcon和zabbix主要在于集群的状态集中显示,可以很便捷的对比各主机的性能状态. gmond:相当于是agent端,主要用于收集各node的性能状 ...
- python 常用技巧 — 列表(list)
目录: 1. 嵌套列表对应位置元素相加 (add the corresponding elements of nested list) 2. 多个列表对应位置相加(add the correspond ...
- Windows下Maven安装 + eclipse集成
一.什么是maven? Maven是一个项目管理工具,能方便的帮我们下载jar包,告别传统手动导包的方式. 二.maven仓库 maven中有中央仓库,本地仓库,私服三个概念 1.中央仓库是maven ...
- 筆記本 wifi走外网线 網卡走內網
筆記本 wifi走外网线 網卡走內網 ,案列 -------------------------------------------------------- route print ...
- python实现人民币大写转换
问题描述: 银行在打印票据的时候,常常需要将阿拉伯数字表示的人民币金额转换为大写表示,现在请你来完成这样一个程序. 在中文大写方式中,0到10以及100.1000.10000被依次表示为: 零 壹 贰 ...