Java [Leetcode 66]Plus One
题目描述:
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 int[] plusOne(int[] digits) {
int carry = 0;
if(digits == null || digits.length == 0)
return null;
else
digits[digits.length - 1] += 1;
for(int i = digits.length - 1; i >= 0; i--){
int cur = carry + digits[i];
carry = cur / 10;
digits[i] = cur % 10;
}
if(carry != 0){
int[] newDigits = new int[digits.length + 1];
System.arraycopy(digits, 0, newDigits, 1, digits.length);
newDigits[0] = carry;
return newDigits;
} else {
return digits;
}
}
}
Java [Leetcode 66]Plus One的更多相关文章
- 前端与算法 leetcode 66. 加一
目录 # 前端与算法 leetcode 66. 加一 题目描述 概要 提示 解析 解法一 解法二 算法 # 前端与算法 leetcode 66. 加一 题目描述 给定一个由整数组成的非空数组所表示的非 ...
- Java实现 LeetCode 66 加一
66. 加一 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示 ...
- [LeetCode]66.加一(Java)
原题地址: plus-one 题目描述: 给定一个由 整数 组成的 非空 数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字. 你可以假设除了整数 ...
- Java [leetcode 1] Two Sum
小二终于开通博客了,真是高兴.最近在看Java,所以就拿leetcode练练手了.以后我会将自己解体过程中的思路写下来,分享给大家,其中有我自己原创的部分,也有参考别人的代码加入自己理解的部分,希望大 ...
- LeetCode 66. Plus One(加1)
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. Yo ...
- LeetCode—66、88、118、119、121 Array(Easy)
66. Plus One Given a non-negative integer represented as a non-empty array of digits, plus one to th ...
- 2017-3-7 leetcode 66 119 121
今天纠结了一整天============================================================== leetcode66 https://leetcode.c ...
- [LeetCode] 66. Plus One 加一
Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The ...
- leetcode 66
66. Plus One Given a non-negative number represented as an array of digits, plus one to the number. ...
随机推荐
- text-align:justify实现文本两端对齐布局,兼容IE
要想更好的理解 css, 尤其是 IE 下对 css 的渲染,haslayout 是一个非常有必要彻底弄清楚的概念.大多 IE下的显示错误,就是源于 haslayout. 什么是 haslayout ...
- 【转】Tarjan&LCA题集
转自:http://blog.csdn.net/shahdza/article/details/7779356 [HDU][强连通]:1269 迷宫城堡 判断是否是一个强连通★2767Proving ...
- Unity3D脚本中文系列教程(八)
◆ static var matrix : Matrix4x4 描述:设置用于渲染所有gizmos的矩阵. 类方法 ◆ Static function DrawCube(center:Vector3, ...
- UVA 11424 GCD - Extreme (I) (欧拉函数+筛法)
题目:给出n,求gcd(1,2)+gcd(1,3)+gcd(2,3)+gcd(1,4)+gcd(2,4)+gcd(3,4)+...+gcd(1,n)+gcd(2,n)+...+gcd(n-1,n) 此 ...
- 1034-IBM技术俱乐部主席竞选
描述 今天IBM技术俱乐部举行主席竞选,你的任务是统计谁是得票最多的候选人. 输入 输入数据包含多组测试案例. 每组测试案例由N(0<N<1000)开头,N表示投票总数,后续N行每行包含一 ...
- POJ 1125 Stockbroker Grapevine(floyd)
http://poj.org/problem?id=1125 题意 : 就是说想要在股票经纪人中传播谣言,先告诉一个人,然后让他传播给其他所有的经纪人,需要输出的是从谁开始传播需要的时间最短,输出这个 ...
- 一天,python搞个分析NGINX日志的脚本
准备给ZABBIX用的. 统计接口访问字次,平均响应时间,4XX,5XX次数 以后可以再改进.. #!/usr/bin/env python # coding: utf-8 ############# ...
- hdu 1524 A Chess Game 博弈论
SG函数!! 代码如下: #include<stdio.h> #include<cstring> #define I(x) scanf("%d",& ...
- hdu 1309 Loansome Car Buyer
纯粹的阅读理解题………… ;}
- java:构造函数
class Dog { Dog(){ } } 构造函数没有返回值定义,构造函数名必须和类名相同,如果类里面没有构造函数,编译器会帮你加一个构造函数. 使用this调用构造函数 class Dog { ...