66、Plus One

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.

C++

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

67. Add Binary

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

The input strings are both non-empty and contains only characters 1 or 0.

Example 1:

Input: a = "11", b = "1"
Output: "100"

Example 2:

Input: a = "1010", b = "1011"
Output: "10101"

C++

class Solution {
public:
string addBinary(string a, string b) {
string s = ""; int c = , i = a.size() - , j = b.size() - ;
while(i >= || j >= || c == )
{
c += i >= ? a[i --] - '' : ;
c += j >= ? b[j --] - '' : ;
s = char(c % + '') + s;
c /= ;
} return s;
}
};

leetcode 60-80 easy的更多相关文章

  1. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

  2. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  3. leetcode 26 80 删除已排序数组中重复的数据

    80. Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if dupli ...

  4. LeetCode OJ 80. Remove Duplicates from Sorted Array II

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  5. LeetCode: 60. Permutation Sequence(Medium)

    1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...

  6. 【Leetcode】【Easy】String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  7. [LeetCode] 60. Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  8. leetcode第一题(easy)

    第一题:题目内容 Given an array of integers, return indices of the two numbers such that they add up to a sp ...

  9. Java实现 LeetCode 60 第k个排列

    60. 第k个排列 给出集合 [1,2,3,-,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" &q ...

  10. 【LeetCode】80. 删除有序数组中的重复项 II

    80. 删除有序数组中的重复项 II 知识点:数组:排序:双指针: 题目描述 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现两次 ,返回删除后数组的新长度. 不要使 ...

随机推荐

  1. 版本控制git之二 分支 切换分支 创建分支 合并 删除

      版本控制git之二 分支   有人把 Git 的分支模型称为它的`‘必杀技特性’',也正因为这一特性,使得 Git 从众多版本控制系统中脱颖而出. 为何 Git 的分支模型如此出众呢? Git 处 ...

  2. CentOS6安装docker、docker-compose、docker-enter

    一.安装docker 1.查看CentOS内核版本 uname -r 2.安装Fedora的EPEL源 yum install http://ftp.riken.jp/Linux/fedora/epe ...

  3. python3-常用模块之os

    os模块,os是和操作系统交互的模块 os.getcwd() :获取当前工作目录,即当前python脚本工作的目录路径,如果是命令行模式下,同样表示当前目录下 os.listdir(路径): 列出指定 ...

  4. myeclipse中tomcat内存大小的设置

    刚刚安装了myeclipse9.0,又配置了tomcat7.0,想用ssh框架搭个项目试试tomcat7.0,没想到刚启动项目就会报错,在tomcat6.0中就不会有问题,上网查了那些都不起作用,后来 ...

  5. ORA-01790: 表达式必须具有与对应表达式相同的数据类型

    出现这种错误,要先看一下是不是sql中有用到连接:union,unio all之类的,如果有,需要注意相同名称字段的数据类型一定要相同.

  6. 2019-5-21-C#-命令行如何静默调用-del-删除文件

    title author date CreateTime categories C# 命令行如何静默调用 del 删除文件 lindexi 2019-05-21 11:32:28 +0800 2019 ...

  7. [CQOI2009]叶子的染色【性质+树形Dp】

    Online Judge:Bzoj1304,Luogu P3155 Label:无根树,树形Dp 题目描述 给定一棵\(N\)个节点的无根树,它一共有\(K\)个叶子节点.你可以选择一个度数大于1的节 ...

  8. boost 日期时间计算

    示例代码如下: #include <boost/date_time/gregorian/gregorian.hpp> #include <boost/date_time/posix_ ...

  9. linux php5.4安装phalcon

    PHP版本5.6以上,安装可参考.https://blog.csdn.net/happysdh/article/details/51450654 在phalcon3.0以后就不支持php5.4了. 要 ...

  10. vue-cli 手机上浏览自己的项目

    首先我们需要更改config文件 拿我这个项目举例子,config文件下的index.js内的dev下的host需要更改为自己的电脑IP 其次,重点来了,我们需要更改路径,细节的为什么我还解释不来,简 ...