题目:

加一

给定一个非负数,表示一个数字数组,在该数的基础上+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. sql中更新数据库用到declare @a in

    declare @a in update TB_Class set @a=1,name='李小龙' where ID=1 这样就可以像更新哪个就更新哪个了 例如ibatisnet中需要更新的时候: & ...

  2. 转:Java HashMap实现详解

    Java HashMap实现详解 转:http://beyond99.blog.51cto.com/1469451/429789 1.    HashMap概述:    HashMap是基于哈希表的M ...

  3. jquery.tmpl.min.js--前端实现模版--数据绑定--详解

    动态请求数据来更新页面是现在非常常用的方法,比如博客评论的分页动态加载,微博的滚动加载和定时请求加载等. 这些情况下,动态请求返回的数据一般不是已拼好的 HTML 就是 JSON 或 XML,总之不在 ...

  4. 利用dropbox备份vps数据

    在VPS的数据最好定时备份,免得服务器出了什么问题,数据就全丢了.我使用dropbox定时同步wordpress文件夹和数据库信息. 首先下载dropbox ? 1 wget -O dropbox.t ...

  5. WordPress 主题开发 - (三) 开发工具 待翻译

    Before we get started building any WordPress Theme, we’re going to need to get our development tools ...

  6. 一个PHP加密脚本,达到一定免杀效果

    <?php /**************************************** *author: L.N. *blog : [url]http://lanu.sinaapp.co ...

  7. python学习第四天第一部分

    1.字典的特性:无序.去重.查询速度快.比list占用内存多. 2.字典查询速度快的原因:因为他是哈希类型的. 3.什么是(hash)哈希? hash把任意长度的二进制映射为较短的固定长度的二进制,这 ...

  8. 爱重启的windows,伤不起

    .

  9. 【html5】这些新类型 能提高生产力

    <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...

  10. java并发编程(一)

    多个线程访问同一个变量时,可能会出现问题.这里我用两个线程同时访问一个int count变量,让他们同时+1.同时让线程睡眠1秒,每个线程执行10次,最后应该输出20才对,因为count++并不是原子 ...