LeetCode OJ:Plus One (加1)
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.
这题的题目写的比较简单,就是让你把代表的数加上1就可以,一开始尽然还没看懂题目,真是糗,代码如下:
vector<int> plusOne(vector<int>& digits) {
reverse(digits.begin(), digits.end());
int sz = digits.size();
int flag = ;
for(int i = ; i< sz; ++i){
int val = digits[i] + flag;
digits[i] %= val;
flag = val/;
}
if(flag != )
digits.push_back(flag);
reverse(digits.begin(), digits.end());
return digits;
}
下面是用java写的版本,跟上面还是不太一样的:
public class Solution {
public int[] plusOne(int[] digits) {
int carry = 1;
for(int i = digits.length-1; i >= 0; --i){
int val = digits[i] + carry;
carry = 0;
digits[i] = val%10;
carry = val/10;
}
if(carry > 0){
int [] ret = new int[digits.length + 1];
ret[0] = carry;
System.arraycopy(digits, 0, ret, 1, digits.length);
return ret;
}
return digits;
}
}
LeetCode OJ:Plus One (加1)的更多相关文章
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- LeetCode OJ 297. Serialize and Deserialize Binary Tree
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
- LeetCode OJ 之 Maximal Square (最大的正方形)
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...
- LeetCode OJ:Integer to Roman(转换整数到罗马字符)
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- LeetCode OJ:Serialize and Deserialize Binary Tree(对树序列化以及解序列化)
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- 每日一道 LeetCode (14):数组加一
每天 3 分钟,走上算法的逆袭之路. 前文合集 每日一道 LeetCode 前文合集 代码仓库 GitHub: https://github.com/meteor1993/LeetCode Gitee ...
随机推荐
- phpexcel导出带生成图片完美案例
// 导出exl public function look_down(){ $id = I('get.id'); $m = M ('offer_goods'); $where['offer_id'] ...
- 用Maven构建Mahout项目实现协同过滤userCF--单机版
本文来自:http://blog.fens.me/hadoop-mahout-maven-eclipse/ 前言 基于Hadoop的项目,不管是MapReduce开发,还是Mahout的开发都是在一个 ...
- s5_day8作业
# 1 整理今天装饰器代码(每人手写一份,注意,是手写,交到小组长手里,明天我检查),准备明天默写 # 2 编写日志装饰器,实现功能如:一旦函数f1执行,则将消息2017-07-21 11:12:11 ...
- webservice -- cxf客户端调用axis2服务端
背景: 有个项目, 需要由第三方提供用户信息, 实现用户同步操作, 对方给提供webservice接口(axis2实现)并也使用axis2作主客户端调用我方提供的webservice接口 起初, 由于 ...
- C++白盒测试最佳实践课程,3个免费名额火热申请中,31号前截止申请...
C++白盒测试最佳实践课程,3个免费名额火热申请中,31号前截止申请...http://automationqa.com/forum.php?mod=viewthread&tid=2561&a ...
- springboot的Scheduled定时器不工作
问题情况 使用springboot,使用注解方式启动定时器进行业务调度. 在入口类中加了注解如下: package org.test.xyz; @SpringBootApplication @Enab ...
- Keepalived 服务器状态监测
keepalived简介: keepalived是一个类似于layer3, 4 & 5交换机制的软件,也就是我们平时说的第3层.第4层和第5层交换.Keepalived的作用是检测web服务器 ...
- ubuntu 16.04安装navicat for mysql
下载地址:官网https://www.navicat.com/download 1.下载 navicat120_mysql_en_x64.tar.gz 文件 2.下载后移到/opt/下 3.解压ta ...
- C# 字符串中正则表达式的应用
1.截取字符串中指定内容 {"weatherinfo":{"city":"北京","cityid":"1010 ...
- Zabbix Windos agent 安装
系统:Windos 2008 R2 x64 服务:Zabbix_agents_3.0.4.win 一.安装Zabbix_agents_3.0.4.win 1.下载Zabbix_agents_3.0.4 ...