package plusOne66;
/*
Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
*/
public class Solution { public static int[] plusOne(int[] digits) {
int index=digits.length-1;
digits[index]=digits[index]+1;
while(digits[index]>=10&&index>0){
digits[index]=digits[index]%10;
index--;
digits[index]=digits[index]+1;
}
if (digits[0]>9)
{
digits[0]=digits[0]%10;
int[] result=new int[digits.length+1];
result[0]=1;
for (int i=0;i<digits.length;i++){
result[i+1]=digits[i];
}
return result;
}
return digits;
}
public static void main(String[] args) {
// TODO Auto-generated method stub int[] digits={9,9,9,9};
//System.out.println(plusOne(digits).toString());
} }

LeetCode----66. Plus One(Java)的更多相关文章

  1. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  2. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  3. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  4. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  5. 前端与算法 leetcode 66. 加一

    目录 # 前端与算法 leetcode 66. 加一 题目描述 概要 提示 解析 解法一 解法二 算法 # 前端与算法 leetcode 66. 加一 题目描述 给定一个由整数组成的非空数组所表示的非 ...

  6. Java实现 LeetCode 66 加一

    66. 加一 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示 ...

  7. Java [Leetcode 66]Plus One

    题目描述: Given a non-negative number represented as an array of digits, plus one to the number. The dig ...

  8. [LeetCode]66.加一(Java)

    原题地址: plus-one 题目描述: 给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 ...

  9. leetcode 刷题记录(java)-持续更新

    最新更新时间 11:22:29 8. String to Integer (atoi) public static int myAtoi(String str) { // 1字符串非空判断 " ...

  10. LeetCode 66. Plus One(加1)

    Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. Yo ...

随机推荐

  1. Lamp下安全配置随笔

    Apache方面: 1.apache有两个指令可以输出服务器的细节,即ServerSignature和ServerTokens. 当这两个指令一起使用时,会输出apache的版本号,php的版本号,i ...

  2. JAVA 线程同步异步简单实例

    package test; public class testThread { public static void main(String[] args) { Example example = n ...

  3. .net 文件下载【转】

    方式一:TransmitFile实现下载.将指定的文件直接写入 HTTP 响应输出流,而不在内存中缓冲该文件.     protected void Button1_Click(object send ...

  4. BootStrap 模态框基本用法

    <!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 模态框(Modal)插件方法</title ...

  5. 计算alert弹出框的次数

    (function() { var oldAlert = window.alert, count = 0; window.alert = function(a) { count++; oldAlert ...

  6. 5_STL设计理念_迭代器

    他山之石,可以攻玉. http://blog.csdn.net/jxh_123/article/details/30793397?utm_source=tuicool&utm_medium=r ...

  7. PHP 标准库 SPL 之数据结构栈(SplStack)简单实践

    PHP 5.3.0 版本及以上的堆栈描述可以使用标准库 SPL 中的 SplStack class,SplStack 类继承双链表 ( SplDoublyLinkedList ) 实现栈. 代码: & ...

  8. ios-获取通讯录 姓名和电话

    #import "ViewController.h" #import <ContactsUI/ContactsUI.h> @interface ViewControll ...

  9. json.parse 与 json.stringfy

    转自 :http://blog.csdn.net/wangxiaohu__/article/details/7254598 parse用于从一个字符串中解析出json对象,如 var str = '{ ...

  10. PHP->利用GD库新建图像

    1.确认php中GD库是否开启 在PHP配置文件php.ini中查找extension=php_gd2.dll,去掉前边的(分号) ';' 即可,一般php是默认开启的 2.绘画步骤 创建一个画布(画 ...