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 ...
随机推荐
- 学习笔记(12)- chatopera的语义理解系统
参考https://github.com/chatopera/clause-py-demo 前提:安装完毕 Docker.Docker Compose 和 Python3.x git clone ht ...
- c数据结构链式存储-静态链表
#include "string.h" #include "ctype.h" #include "stdio.h" #include &qu ...
- 112、Java中String类之字符串文本拆分为指定的个数
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- SSM(Spring-SpringMvc-Mybatis)练习
1.总结 https://pan.baidu.com/s/1kXlCf4r 密码:hv6v 2.代码 https://pan.baidu.com/s/1pNgKph5 密码:6rcm 3.资料 h ...
- 洛谷 P6046 [CTSC2000]快乐的蜜月
先讲解一下如何处理这道题的毒瘤输入.\(m\) 和 \(d\) 之间的"/"和" TO "都可以用 getchar() 强行吃掉,日期的转换可以用公式 \(s_ ...
- Codeforces 598D:Igor In the Museum
D. Igor In the Museum time limit per test 1 second memory limit per test 256 megabytes input standar ...
- python 开启http服务并下载文件
Python <= 2.3python -c "import SimpleHTTPServer as s; s.test();" 8000 Python >= 2.4p ...
- Referenced file contains errors
Referenced file contains errors (file:/D:/TONG/tong/eclipse/config_/xsd/spring-context-4.2.xsd). For ...
- [经验] HTML页面中子盒子溢出了怎么办
背景: 在查询数据记录的一个页面上使用bootstar 框架的分页插件作为显示数据表格的工具. 问题: 当查询的范围在一个较短的时间内时, 子元素是乖乖的呆在父元素内部的, 但是一但将查询的时间范围扩 ...
- GeoDa绘制疫情地图
刚学习GeoDa,菜鸟,目前还不能在地图上显示省市名称,求教. 看到丁香医生发布的疫情地图,我也尝试做一下,不过我的shp文件上只有中国大陆的31个省市. 数据来源于丁香医生,截至时间为 2020.1 ...