- 浏览: 18897 次
- 性别:
- 来自: 北京
文章分类
最新评论
题目描述
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
解题思路
本题是将一个十位数按位存储到数组中,然后加一后将结果数组输出。主要的问题在如果有进位的情况需要特殊处理。
自己的代码
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
解题思路
本题是将一个十位数按位存储到数组中,然后加一后将结果数组输出。主要的问题在如果有进位的情况需要特殊处理。
自己的代码
package leetcode; import java.util.Arrays; public class PlusOne { public int[] plusOne(int[] digits) { boolean addOne = false; for(int i = digits.length - 1; i >= 0; i--){ int result = digits[i] + 1; if(result != 10) { digits[i] = result; break; } else{ digits[i] = 0; if(i == 0) addOne = true; } } if(!addOne) return digits; else{ int[] otherDigits = new int[digits.length+1]; otherDigits[0] = 1; for(int i = 0; i < digits.length; i++) otherDigits[i+1] = digits[i]; return otherDigits; } } public static void main(String[] args) { //int[] digits = {1, 2, 2}; //int[] digits = {9}; //int[] digits = {9, 9}; int[] digits = {0}; PlusOne po = new PlusOne(); System.out.println(Arrays.toString(po.plusOne(digits))); } }
发表评论
-
Java中String与StringBuffer的区别
2014-10-29 21:07 295String和StringBuffer的区别,网上资料可以说 ... -
String to Integer (atoi)
2014-10-29 17:13 395题目描述 Implement atoi to convert ... -
Implement strStr()
2014-10-28 15:17 279题目描述 Implement strStr(). Retu ... -
Valid Palindrome
2014-10-23 10:32 415题目描述 Given a string, determine ... -
ZigZag Conversion
2014-10-22 19:51 338题目描述 The string "PAYPALIS ... -
Add Binary
2014-10-22 19:43 298题目描述 Given two binary strings, ... -
Longest Common Prefix
2014-10-22 19:44 324题目描述 Write a function to find t ... -
Count and Say
2014-10-22 19:44 344题目描述 The count-and-say sequence ... -
Valid Sudoku
2014-10-21 10:22 352题目描述 Determine if a Sudoku is v ... -
Valid Parentheses
2014-10-21 09:41 319题目描述 Given a string containing ... -
Palindrome Number
2014-10-21 09:41 341题目描述 Determine whether an integ ... -
Length of Last Word
2014-10-21 09:41 353题目描述 Given a string s consists ... -
Minimum Depth of Binary Tree
2014-10-21 09:41 305题目描述 Given a binary tree, find ... -
Remove Nth Node From End of List
2014-10-20 16:36 252题目描述 Given a linked list, remov ... -
Path Sum
2014-10-20 15:37 290题目描述 Given a binary tree and a ... -
Binary Tree Level Order Traversal II
2014-10-20 11:17 229题目描述 Given a binary tree, retur ... -
Binary Tree Level Order Traversal
2014-10-20 11:03 289题目描述 Given a binary tree, retur ... -
Pascal's Triangle II
2014-10-20 10:07 252题目描述 Given an index k, return t ... -
Pascal's Triangle
2014-10-19 12:24 312题目描述 Given numRows, generate th ... -
Merge Sorted Array
2014-10-18 10:45 392题目描述 Given two sorted integer a ...
相关推荐
plusOne代码
一个月挑战C++,里面的内容都很基础,是新手学习的很好的资料
OnePlus One Lux 插件。 OnePlus One 的 Lux 插件。
OnePlus One Bootlogo工具WIP OnePlus One Bootlogo工具
适用于Oneplus One的Ubuntu Touch ###如何安装: : ###如何建立: : ###什么在起作用? ###论坛: :
easyroot-opo Easy root for OnePlus one,适用于 linux 和 mac Start-new.sh = 用于新的引导加载程序(将擦除所有数据!)。 Start.sh = 用于旧的引导加载程序。 Start-new:-打开引导加载程序-Flash 新恢复(TWRP...
PlusOne Scraper从Google个人资料页面生成用户的PlusOne(+1)的RSS feed。 设计为自托管。 使用PHP和PHP Simple HTML DOM Paser构建。 代码也来自sgthayes。
OnePlusTouchFix 每次屏幕打开时,尝试重置(并希望修复)Oneplus One上的触摸。 这是通过运行命令来完成的 cat /sys/class/input/input0/baseline_test 每次获得意图ACTION_SCREEN_ON时,以root身份登录。
用于一加手机USB调试,刷机及第三方recovery可以使用OnePlus_USB_Drivers_Setup.exe
quicksupport和add-on(oneplus版) oneplus7pro 亲测可用,有问题或需要其他版本请私我
OnePlus 5 新品发布会,一加手机静态展示,一加手机性能介绍、工艺介绍、极简高大上设计,一加手机OnePlus 5 新品发布会ppt模板。
#README表示自述文件! 许可协议和免责声明版权所有(C)2014-2015 vvn [vvn @ notworth.it] This program is FREE software: you can redistribute it and/or modifyit as you wish. Copying and distribution of ...
One +的设备树版权所有2014,CyanogenMod项目
fer2013 plus 针对FER2013的图像,进行了10类投票(多了Contempt,unknown 和 非人脸三类),提供的是一种标签方式。采用最大投票方式,去除了一些不确定的图像。文档中含三个文件,Voted_labels.csv(自行下载)中是...
server.add('plusOne', function(value) { console.log(value 1); }); server.listen(9456); var kamote = require('kamote'); var client = new kamote.Client(); client.plusOne(100, ...
fastize-jit-prisma-findOne 12.4 5.77兆字节 fastify-prisma-findOne 13.4 6.24兆字节 fastify-prisma-jit-cache-select 2725.4 656兆字节 固定Prisma Jit选择 50 12兆字节 固定棱镜最小值 47.6 12.7兆字节...
OnePlus One(培根)的设备树 版权所有2014-2016,The CyanogenMod Project版权所有2017-2018,The LineageOS Project OnePlus One(代号为“培根” )是OnePlus的旗舰智能手机。 它于2014年6月发布。 基本的 规格...
适配所有的手机型号,操作简单,右击‘我的电脑’--管理--设备管理器--便携设备,然后双击,根据引导操作就行了
Yolk 是是构建异步用户界面的 JavaScript 库,... const count = plusOne.merge(minusOne).scan((x, y) => x y, 0).startWith(0) return ( id="plus" id="minus" 标签:Yolk
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...