题目:

加一

给定一个非负数,表示一个数字数组,在该数的基础上+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 加一的更多相关文章

  1. [LintCode] Plus One 加一运算

    Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...

  2. LintCode之加一

    题目描述: 分析:由样例可以知道,当数组的每一个数字都是9时,加一会产生一个最高位的数字1,所以先判断这个数组的每一位是否都是9,如果是,那么新数组的大小是原数组大小加一,否则新数组的大小等于原数组的 ...

  3. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  4. [LintCode] Mini Twitter 迷你推特

    Implement a simple twitter. Support the following method: postTweet(user_id, tweet_text). Post a twe ...

  5. lintcode: k Sum 解题报告

    K SUM My Submissions http://www.lintcode.com/en/problem/k-sum/ 题目来自九章算法 13% Accepted Given n distinc ...

  6. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  7. LintCode题解之斐波纳契数列

    直接使用递归的方法会导致TLE,加个缓存就好了: public class Solution { private Integer[] buff = new Integer[1000]; /* * @p ...

  8. (二分查找 拓展) 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 ...

  9. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

随机推荐

  1. WindowsMediaPlayer控件批量添加文件至播放列表

    思路: 1.读取批定路径的目录文件. 2.用List存放. 3.循环List列表添加到播放列表. public void VidieoPlay() { //WindowsMediaPlayer1.ui ...

  2. python学习第六天

    一. 模块介绍1. 模块的定义:用一堆代码实现了某个功能的代码集合     包的定义:本质就是一个目录(必须导游一个_init_.py文件),是用来从逻辑上组织模块的.2. 需要多个函数才能完成(函数 ...

  3. selenium+python find_element_by_css_selector方法使用

    1.通过类class获取 比如如下代码 <h1 class="important"> This heading is very important. </h1&g ...

  4. OPatch failed with error code 73

    前几天给一套LINUX下的RAC数据库打补丁升级,有一台机器更新失败了,相关的异常内容如下: Restoring "/u01/app/oracle/11.2.0/db_1" to ...

  5. iOS中的堆(heap)和栈(stack)的理解

    操作系统iOS 中应用程序使用的计算机内存不是统一分配空间,运行代码使用的空间在三个不同的内存区域,分成三个段:“text segment “,“stack segment ”,“heap segme ...

  6. ios系统 处理内存警告

    iPhone下每个app可用的内存是被限制的,如果一个app使用的内存超过20M,则系统会向该app发送Memory Warning消息.收到此消息后,app必须正确处理,否则可能出错或者出现内存泄露 ...

  7. MySQL存储过程、函数和游标

    这里我新建了两个表,一个users和test CREATE TABLE users( username ), pwd ) ); CREATE TABLE test( id INT, username ...

  8. 高效开发Android App的10个建议(转)

    假如要Google Play上做一个最失败的案例,那最好的秘诀就是界面奇慢无比.耗电.耗内存.接下来就会得到用户的消极评论,最后名声也就臭了.即使你的应用设计精良.创意无限也没用. 耗 电或者内存占用 ...

  9. c#之委托总结

    1.委托的声明和编译原理 声明委托: delegate void Translate(string str); 通过反编译之后可得下面代码 private sealed class Translate ...

  10. 20145120 《Java程序设计》第5周学习总结

    20145120 <Java程序设计>第5周学习总结 教材学习内容总结 try和catch语法,如果被try{}的语句出现了catch()的问题就执行catch{}的语句. 错误的对象都继 ...