leetcode 60-80 easy
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
1or0.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的更多相关文章
- LeetCode:60. Permutation Sequence,n全排列的第k个子列
LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- leetcode 26 80 删除已排序数组中重复的数据
80. Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if dupli ...
- LeetCode OJ 80. Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode: 60. Permutation Sequence(Medium)
1. 原题链接 https://leetcode.com/problems/permutation-sequence/description/ 2. 题目要求 给出整数 n和 k ,k代表从1到n的整 ...
- 【Leetcode】【Easy】String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- [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 ...
- leetcode第一题(easy)
第一题:题目内容 Given an array of integers, return indices of the two numbers such that they add up to a sp ...
- Java实现 LeetCode 60 第k个排列
60. 第k个排列 给出集合 [1,2,3,-,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" &q ...
- 【LeetCode】80. 删除有序数组中的重复项 II
80. 删除有序数组中的重复项 II 知识点:数组:排序:双指针: 题目描述 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 最多出现两次 ,返回删除后数组的新长度. 不要使 ...
随机推荐
- JAVA基础_可变参数
自JAVA1.5以来,在JAVA中出现了可变参数一说,其针对的情况是对多个不确定的相同类型的元素进行同一类操作的情形. 可变参数有点类似与重载的概念,但是其中的可变参数是被隐式的转换成数组来进行处理的 ...
- Python2.7版本:定义类时为什么要继承object类?
********此答案摘自知乎,且经过自己实际运行后得出******** 继承 object 类的是新式类,不继承 object 类的是经典类 例子: 新式类: 经典类: B.C 是 A 的子类,D ...
- java 对接芝麻信用 -用芝麻私钥解密错误
最近在做芝麻信用认证对接, 按照官方的文档,配置了,appid ,私钥公钥 ,然后把示例文档拷过来, 结果 总是报错, 芝麻私钥解密 ZMOP.decode_private_key_error, 调了 ...
- 短URL系统、301/302重定向
短 URL 系统是怎么设计的?https://yq.aliyun.com/articles/87600 短网址(short URL)系统的原理及其实现 https://hufangyun.com/20 ...
- css3之2D 转换
浏览器支持 表格中的数字表示支持该属性的第一个浏览器版本号. 紧跟在 -webkit-, -ms- 或 -moz- 前的数字为支持该前缀属性的第一个浏览器版本号. Chrome 和 Safari 要求 ...
- AlexNet结构图详解
- Spring MVC 返回 xml json pdf 数据的配置方法
<!-- Spring MVC 返回 xml 数据的配置方法 --> <bean class="org.springframework.web.servlet.vi ...
- cannot be cast to javax.servlet.Servlet 解决
使用maven创建web项目的时候,通过添加依赖的方式来添加servlet-api,如下 通过maven的命令(tomcat:run)来启动项目,发现访问的时候报错,错误如下: 错误排查: 首先查看s ...
- 全栈之路-微信小程序-SKU开发(分析)
SKU是整个小程序中最难完成的部分了,好好记录一下SKU,主要是想记录一下 从最开始拿到这个业务到最终完成这个功能期间的思考过程,至于代码什么的,记录也好,不记录也行,再看! 一.从思路说起 1.SK ...
- Putty保存设置
使用putty连接服务器每次都要配置编码,有时候忘了配置,提示就出现中文乱码真是坑爹(纯英文提示也行啊),好了闲话少说,步入正题. Putty的设置保存功能隐藏的实在太好了,原来在Connection ...