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.

Solution 1 : 十进制加法

 class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
int carry = ;
for(int i = digits.size()-; i >= ; i --)
{
int sum = digits[i]+carry;
carry = sum / ;
digits[i] = sum % ;
if(carry == )
break;
}
if(carry == )
digits.insert(digits.begin(), );
return digits;
}
};

Solution 2 :

 class Solution {
public:
vector<int> plusOne(vector<int> &digits) {
for(int i = digits.size()-; i >= ; i --)
{
if(digits[i] <= ){
digits[i] += ;
return digits;
}else{//
if(i != )
digits[i] = ;
else
{
digits[] = ;
digits.push_back();
return digits;
}
}
}
}
};

67- Add Binary

Given two binary strings, return their sum (also a binary string).

For example,
a = "11"
b = "1"
Return "100".

Solution:二进制加法,和为2进1,和为3进1留1;

 1 class Solution {
2 public:
3 string addBinary(string a, string b) {
4 int sizea=a.size(),sizeb=b.size();
5 if(sizea<sizeb)return addBinary(b,a);
6 int n=sizea-sizeb;
7 string helper(n,'0');
8 b = helper + b;
9 int carry=0;
10 for(int i=sizea-1;i>=0;i--){
11 int sum=(a[i]-'0')+(b[i]-'0')+carry;
12 if(sum==0);
13 else if(sum==1){
14 a[i]='1';
15 carry=0;
16 }else if(sum==2){
17 a[i]='0';
18 carry=1;
19 }else if(sum==3){
20 a[i]='1';
21 carry=1;
22 }
23 }
24 if(carry==1)a='1'+a;
25 return a;
26 }
27 };

【LeetCode】66 & 67- Plus One & Add Binary的更多相关文章

  1. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  2. 【LeetCode】241. Different Ways to Add Parentheses

    Different Ways to Add Parentheses Given a string of numbers and operators, return all possible resul ...

  3. 【LeetCode】66. 加一

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

  4. 【LeetCode】66. Plus One 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数九 采用进位 日期 [LeetCode] 题目地址 ...

  5. 【LeetCode】241. Different Ways to Add Parentheses 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归构建所有表达式 方法二:分而治之 日期 ...

  6. 【LeetCode】66. Plus One

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

  7. 【LeetCode】66. Plus One (2 solutions)

    Plus One Given a non-negative number represented as an array of digits, plus one to the number. The ...

  8. 【LEETCODE】66、字符串分类,hard级别,题目:32,72,76

    package y2019.Algorithm.str.hard; import java.util.Stack; /** * @ProjectName: cutter-point * @Packag ...

  9. 【leetcode】637. Average of Levels in Binary Tree

    原题 Given a non-empty binary tree, return the average value of the nodes on each level in the form of ...

随机推荐

  1. Android EditText获取光标位置并插入字符删除字符

    1.获取光标位置 int index = editText.getSelectionStart(); 2.在光标处插入字符 int index = editText.getSelectionStart ...

  2. 网页爬虫的设计与实现(Java版)

    网页爬虫的设计与实现(Java版)     最近为了练手而且对网页爬虫也挺感兴趣,决定自己写一个网页爬虫程序. 首先看看爬虫都应该有哪些功能. 内容来自(http://www.ibm.com/deve ...

  3. Oracle在linux下的开机自启动(详细)转

    linux下系统开机oracle自启动(方法一) ---加载为服务自启动.停止一.dbstart   及   dbshut 1. 修改Oracle系统配置文件:/etc/oratab,只有这样,Ora ...

  4. github简单使用教程(转)

    github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开.对于一般人来说公共仓库就已经足够了,而且我们也没多少代码来管理,O(∩_∩)O ...

  5. javascript library

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  6. 深入理解Java内存模型(六)——final

    与前面介绍的锁和volatile相比较,对final域的读和写更像是普通的变量访问.对于final域,编译器和处理器要遵守两个重排序规则: 在构造函数内对一个final域的写入,与随后把这个被构造对象 ...

  7. 安全删除mysql binlog日志

    命令行下执行 show binary logs; purge binary logs to 'mysql-bin.000070';

  8. 2010山东省第一届ACM程序设计竞赛

    休眠了2月了 要振作起来了!!... http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2155 因 ...

  9. UVa 11090 Going in Cycle!!【Bellman_Ford】

    题意:给出n个点m条边的加权有向图,求平均值最小的回路 自己想的是用DFS找环(真是too young),在比较找到各个环的平均权值,可是代码实现不了,觉得又不太对 后来看书= =好巧妙的办法, 使用 ...

  10. HDU 1015 Safecracker

    解题思路:这题相当诡异,样例没过,交了,A了,呵呵,因为理论上是可以通过的,所以 我交了一发,然后就神奇的过了.首先要看懂题目. #include<cstdio> #include< ...