lintcode:Plus One 加一
题目:
给定一个非负数,表示一个数字数组,在该数的基础上+1,返回一个新的数组。
该数字按照大小进行排列,最大的数在列表的最前面。
样例
给定 [1,2,3] 表示 123, 返回 [1,2,4].
给定 [9,9,9] 表示 999, 返回 [1,0,0,0].
解题:
好像只有这样搞,对应进位的时候,要新定义个数组
Java程序:
public class Solution {
/**
* @param digits a number represented as an array of digits
* @return the result
*/
public int[] plusOne(int[] digits) {
// Write your code here
int len = digits.length;
int carray = 1;
for(int i = len-1;i>=0;i--){
carray +=digits[i];
digits[i] = carray%10;
carray = carray/10;
}
if(carray!=1)
return digits;
else {
int nums[] = new int[len+1];
nums[0] = 1;
for(int i=1;i<len+1;i++)
nums[i] =digits[i-1];
return nums;
}
}
}
总耗时: 11253 ms
Python程序:
class Solution:
# @param {int[]} digits a number represented as an array of digits
# @return {int[]} the result
def plusOne(self, digits):
# Write your code here
carray = 1
for i in range(len(digits)-1,-1,-1):
carray +=digits[i]
digits[i] = carray%10
carray = carray/10 if carray!=1:
return digits
else:
digits = [1] + digits
return digits
总耗时: 413 ms
=================================更新===================================
参考leetcode discuss 中一个很好的方法
abcde + 1
有下面的情况:
1.个位数小于9 ,加一后,只是把个位数加一,其他位数没有变,可以直接返回加一后的数组就是答案
2.个位数等于9,说明需要进位,各位数变为0,,十位数 可能小于9 或者等于9的情况,转向 1、2进行讨论
3.最高位等于9,加一后需要进位,这个1一定是开始的1,慢慢先前加进去的,说明这个数全是9,而最后的结果是1000000,这样的形式
所以只需要新定义一个数组,第一位数是1,其余是0,长度是原始数组长度加一。
public class Solution {
/**
* @param digits a number represented as an array of digits
* @return the result
*/
public int[] plusOne(int[] digits) {
// Write your code here
int n = digits.length;
for(int i=n-1; i>=0; i--) {
if(digits[i] < 9) {
digits[i]++;
return digits;
}
digits[i] = 0;
}
int[] newNumber = new int [n+1];
newNumber[0] = 1;
return newNumber;
}
}
lintcode:Plus One 加一的更多相关文章
- [LintCode] Plus One 加一运算
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
- LintCode之加一
题目描述: 分析:由样例可以知道,当数组的每一个数字都是9时,加一会产生一个最高位的数字1,所以先判断这个数组的每一位是否都是9,如果是,那么新数组的大小是原数组大小加一,否则新数组的大小等于原数组的 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- [LintCode] Mini Twitter 迷你推特
Implement a simple twitter. Support the following method: postTweet(user_id, tweet_text). Post a twe ...
- lintcode: k Sum 解题报告
K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...
- leetcode & lintcode for bug-free
刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...
- LintCode题解之斐波纳契数列
直接使用递归的方法会导致TLE,加个缓存就好了: public class Solution { private Integer[] buff = new Integer[1000]; /* * @p ...
- (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...
- leetcode & lintcode 题解
刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...
随机推荐
- Nginx,LVS,HAProxy,负载均衡之选择
Nginx的优点:性能好,可以负载超过1万的并发.功能多,除了负载均衡,还能作Web服务器,而且可以通过Geo模块来实现流量分配.社区活跃,第三方补丁和模块很多支持gzip proxy缺点:不支持se ...
- SQL允许远程访问
1.打开sqlserver对象资源管理器 右键 方面 常规 服务器配置 RemoteAccessEnabled true RemoteDacEnabled true 2.打开SQL SERVER管理 ...
- odoo8.0+PyCharm4.5开发环境配置
终于把odoo的开发环境配置好,现把相关经验教训做个记录分享: odoo8.0+PyCharm4.5开发环境配置(剑飞花 373500710) 1.最佳兼容搭配包 试过各种组合 GreenOdoo+p ...
- Windows 下 Composer 与 Laravel 4 的安装
1.安装Composer Composer是PHP的依附经管对象之一,官方网站 http://getcomposer.org/ .它支撑多种安装体式格式,对于在win下做开辟的草来说,最便捷的体式格式 ...
- 打包python脚本为exe可执行文件-pyinstaller和cx_freeze示例
本文介绍使用cx_freeze和pyinstaller打包python脚本为exe文件 cx_freeze的使用实例 需要使用到的文件wxapp.py, read_file.py, setup.py ...
- _cpluscplus
_cpluscplus是c++中的定义,而c中没有该定义 1.用来判定代码是c类型还是c++类型 2._cplusplus的类型是"long int",值为199711L int ...
- VB 思维导图总结(三)
续上篇.vb总结之“思维导图”(2) 第十章.绘制图形 第十一章.数据库技术... 第十二章.总结! 相信有了vb的一个基础,应对接下来的学习会顺畅些.加油!
- 百度快收录吧!!!a39fe054b88866bc737dd5fb02f39e41
百度快收录吧!!!a39fe054b88866bc737dd5fb02f39e41 }416oTemocleW{yek
- android button 字母自动大写
<Button android:id="@+id/btnStart" android:layout_width="wrap_content" androi ...
- sqlserver 动态表名 动态字段名 执行 动态sql
动态语句基本语法: 1 :普通SQL语句可以用exec执行 Select * from tableName exec('select * from tableName') exec sp_execut ...