【LeetCode】66. Plus One 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
[LeetCode]
题目地址:https://leetcode.com/problems/plus-one/
Total Accepted: 99274 Total Submissions: 294302 Difficulty: Easy
题目描述
Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
Example 1:
Input: [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Example 2:
Input: [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
题目大意
给出了一个数组表示的十进制数字,数组的每个位置都是单个数字,现在要把这个十进制数字+1,求结果数组。
解题方法
数九
就是把一个数组表示的整数+1,看起来非常简单的一个题目。但是据说是Google最喜欢的面试题。
想法是从最后一位开始看,如果这位是9的话转为0,不是的话就+1,再往前面看,直到不是9为止。
运算结束之后,如果最高位为0,说明这个数所有的位数都为9,那么需要创建新数组,把最高位置为一。
这个想法的确实是一个加法最基本的想法。说明我们需要从最常见的事物中找到准确表达的算法。
public class Solution {
public int[] plusOne(int[] digits) {
for(int i=digits.length-1; i>=0; i--){
if(digits[i]==9){
digits[i]=0;
continue;
}else{
digits[i]+=1;
break;
}
}
if(digits[0]==0){
int[] answer=new int[digits.length + 1];
answer[0]=1;
return answer;
}else{
return digits;
}
}
}
AC:0ms
采用进位
使用carry表示进位,这样我们把每个位置更新成当前的数字+carry即可,如果大于等于10,那么carry就又是1,否则carry就是0了。这个操作很类似大整数加法。
因为只有+1的操作,所以我在刚开始的时候,就对最后一个元素做了+1运算,这样再次循环判断是不是要进位即可。
class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
N = len(digits)
pos = N - 1
carry = 0
digits[-1] += 1
while pos >= 0:
digits[pos] += carry
if digits[pos] >= 10:
carry = 1
digits[pos] -= 10
else:
carry = 0
pos -= 1
if carry:
digits.insert(0, 1)
return digits
日期
2016 年 05月 8日
2018 年 11 月 21 日 —— 又是一个美好的开始
【LeetCode】66. Plus One 解题报告(Python)的更多相关文章
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
- Leetcode 115 Distinct Subsequences 解题报告
Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solutio ...
- 【LeetCode】3Sum Closest 解题报告
[题目] Given an array S of n integers, find three integers in S such that the sum is closest to a give ...
随机推荐
- Oracle-where exists()、not exists() 、in()、not in()用法以及效率差异
0.exists() 用法: select * from T1 where exists(select 1 from T2 where T1.a=T2.a) 其中 "select 1 fro ...
- 26-Palindrome Number
回文数的判定,不使用额外的空间 Determine whether an integer is a palindrome. Do this without extra space. 思路:将一个整数逆 ...
- kubernetes部署 kube-apiserver服务
kubernetes部署 kube-apiserver 组件 本文档讲解使用 keepalived 和 haproxy 部署一个 3 节点高可用 master 集群的步骤. kube-apiserve ...
- lua5.4 beta中的to-be-closed变量的用法
对应目前最新lua5.4 beta版本:2019-10-09发布 这个功能之前修改过两次语法,当前的语法不出意外将会是最终决定了,目前还没有最新的中文资料,所以我来这里发一下. 先介绍下这个功能: 被 ...
- 添加页面、页面交互、动态添加页面tab
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ViewDictTosPr ...
- 学习java的第五天
一.今日收获 1.java程序设计完全手册第一章节的小总结 2.完成了部分例题验证解答. 二.今日难题 1.java语言与c++语言的不同点. 2.有例题验证不出来 三.明日目标 1.复习java程序 ...
- accessory, accident
accessory 1. belt, scarf, handbag, Penny用rhinestone做的小首饰(Penny Blossom)都是accessory2. With default se ...
- C++之无子数
题目如下: 1 #include <iostream> 2 3 using namespace std; 4 5 6 bool isThisNumhaveChild(int num); 7 ...
- 哪里可以下载支付宝demo或者sdk
http://club.alipay.com/read-htm-tid-9976972.html 这里有所有的demo和sdk包括移动产品的demo.在他的论坛里面呢 真心恶心啊.不放到主页.
- redis入门到精通系列(八):redis的高可用--主从复制详解
(一)主从复制介绍 前面所讲的关于redis的操作都属于单机操作,单机操作虽然操作简单,但是处理能力有限,无法高可用.所谓高可用性,就是指当一台服务器宕机的时候,有备用的服务器能顶替上,在单机操作上这 ...