LeetCode实战练习题目 - Array
实战练习题目 - Array
class Solution {
public:
int maxArea(vector<int>& height) {
int res = 0;
int i = 0;
int j = height.size() - 1;
while (i < j) {
int area = (j - i) * min(height[i], height[j]);
res = max(res, area);
if (height[i] < height[j]) {
i++;
} else {
j--;
}
}
return res;
}
};
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int n = nums.size();
int numZeroes = 0;
for (int i = 0; i < n; i++) {
numZeroes += (nums[i] == 0);
}
vector<int> ans;
for (int i = 0; i < n; i++) {
if (nums[i] != 0) {
ans.push_back(nums[i]);
}
}
while (numZeroes--) {
ans.push_back(0);
}
for (int i = 0; i < n; i++) {
nums[i] = ans[i];
}
}
};
class Solution {
public:
long long GetCni(int n, int i) {
i = (n - i > i)? i : (n - i);
if(i == 0) return 1;
else return GetCni(n, i-1)*(n-i+1)/i;
}
int climbStairs(int n) {
int i = 0;
int Sum = 0;
while(i <= n/2) {
Sum += GetCni(n-i, i);
i++;
}
return Sum;
}
};
class Solution {
public:
vector<vector<int> > threeSum(vector<int>& nums) {
vector<vector<int> > ret;
vector<int > vtemp;
int len = nums.size();
sort(nums.begin(),nums.end());//sort the input
for(int i=0;i<len-2;i++){
if(i ==0 ||(i>0 && nums[i] != nums[i-1])){
int p1 = i+1, p2 = len-1; // set two pointers
while(p1 < p2){
if(nums[p1] + nums[p2] < -nums[i]){
p1++;
}else if(nums[p1] + nums[p2] == -nums[i]){
if(p1 == i+1){
vector<int > vtemp{nums[i], nums[p1], nums[p2]};
ret.push_back(vtemp);
vtemp.clear();
}else if(nums[p1] != nums[p1-1]){
vector<int > vtemp{nums[i], nums[p1], nums[p2]};
ret.push_back(vtemp);
vtemp.clear();
}
p1++,p2--;
}else{
p2--;
}
}
}
}
return ret;
}
};
LeetCode实战练习题目 - Array的更多相关文章
- leetcode - 位运算题目汇总(下)
接上文leetcode - 位运算题目汇总(上),继续来切leetcode中Bit Manipulation下的题目. Bitwise AND of Numbers Range 给出一个范围,[m, ...
- leetcode top 100 题目汇总
首先表达我对leetcode网站的感谢,与高校的OJ系统相比,leetcode上面的题目更贴近工作的需要,而且支持的语言广泛.对于一些比较困难的题目,可以从讨论区中学习别人的思路,这一点很方便. 经过 ...
- LeetCode 941. Valid Mountain Array (有效的山脉数组)
题目标签:Array 题目给了一组int array A,让我们判断它是否是 一个山脉数组. 山脉数组一定要有一个最高值,然后要同时有 山坡和下坡. 想法是,从左边开始依次比较两个数字,int[0] ...
- [LeetCode] 88. Merge Sorted Array 合并有序数组
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: T ...
- [LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [LeetCode] 360. Sort Transformed Array 排序转换后的数组
Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f( ...
- [LeetCode] 384. Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)
[LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- leetcode tree相关题目总结
leetcode tree相关题目小结 所使用的方法不外乎递归,DFS,BFS. 1. 题100 Same Tree Given two binary trees, write a function ...
随机推荐
- 将图片转化为base64编码字符串
pom依赖 <dependency> <groupId>org.ops4j.base</groupId> <artifactId>ops4j-base- ...
- Duilib定时器
转载:https://www.zhaokeli.com/article/8262.html 问题描述 直接使用系统函数定义 参数 http://www.zhaokeli.com/article/18 ...
- css样式和定义的class都没问题,但样式却没生效
今天开发遇到过这样的问题,主要原因是 css 文件格式有问题导致的.有问题的 css 样式的那一行下面的 css 样式不能生效
- 使用mysql服务实现负载均衡
mysql 长连接(tcp协议)模拟负载均衡 1.存在member,member的port与mysql服务port一致为3306 2.vip绑定浮动ip 3.开启mysql服务: #service m ...
- css中class后面跟两个类,这两个类用空格隔开
css中class后面跟两个类,这两个类用空格隔开,那么这两个类对这个元素都起作用,如果产生冲突,那么后面的类将替代前面的类.
- 嵌入式编程中使用 do{...} while(0) 的解释
最近在看esp32的idf,有一些宏定义使用了do while(0)这种看起来好像没啥用的代码.然后我查了一下资料,发现在linux内核代码中经常用到这个东西! 现在就将这个东西整理一下. 为什么在内 ...
- java面试题蚂蚁
hashmap结构:什么对象能做为key hashtable,concurrentHashMap,hashtable比较 String,StringBuilder,StringBuffer 对象的深浅 ...
- js中数值各进制之间的转换
十进制转换为二进制 toString()方法可把一个 Number 对象转换为一个字符串,并返回结果.语法如下: NumberObject.toString(radix); 其中,radix为可选.规 ...
- leetcode295 Find Median from Data Stream
""" Median is the middle value in an ordered integer list. If the size of the list is ...
- VUe for循环if 的使用和函数的使用 (笔记)
结果如图: 代码html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...