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 ...
随机推荐
- mysql :将其中两个数据的某一个字段合拼成一句
SELECT xq.*, ts.xu_qiu_id, ts.content FROM wx_xu_qiu xq LEFT JOIN (SELECT xu_qiu_id, GROUP_CONCAT(co ...
- rem与部分手机 字体偏大问题
原因是部分手机自己设置了巨无霸字体.
- MyEclipse和Eclipse中常用的快捷键
##########################快捷键分类速查########################## *******常用类********[Ctrl+O] 显示类中方法和 ...
- 零基础学完Python的7大就业方向,哪个赚钱多?
“ 我想学 Python,但是学完 Python 后都能干啥 ?” “ 现在学 Python,哪个方向最简单?哪个方向最吃香 ?” “ …… ” 相信不少 Python 的初学者,都会遇到上面的这些问 ...
- JPG加入RAR文件原理详解
在水木看到有人上传了一张图片,说如果将其后缀改为rar,解压后会有别的文件,试了一下,果然如此.用十六进制的编辑器看了看,发现的确有理. 先是,文件头部是以JPG格式起始的,如下: ......JFI ...
- The Google File System中文版
译者:alex 摘要 我们设计并实现了Google GFS文件系统,一个面向大规模数据密集型应用的.可伸缩的分布式文件系统.GFS虽然运行在廉价的普遍硬件设备上,但是它依然了提供灾难冗余的能力,为大量 ...
- Android数据库高手秘籍(二):创建表和LitePal的基本用法
原文:http://blog.jobbole.com/77157/ 上一篇文章中我们学习了一些Android数据库相关的基础知识,和几个颇为有用的SQLite命令,都是直接在命令行操作的.但是我们都知 ...
- 「SCOI2010」幸运数字
传送门 Luogu 解题思路 首先构造出所有的幸运数字. 然后考虑一个幸运数字会产生多少贡献. 对于一个数 \(x\),它在区间 \([l,r]\) 内的倍数的个数为 \(\lfloor \frac{ ...
- 5.5 Nginx 负载均衡
ip_hash 语法:ip_hash 默认值:none 使用字段:upstream 这个指令将基于客户端连接的IP地址来分发请求.哈希的关键字是客户端的C类网络地址,这个功能将保证这个客户端请求总是被 ...
- (转)C#的 GC工作原理基础
作为一位C++出身的C#程序员,我最初对垃圾收集(GC)抱有怀疑态度,怀疑它是否能够稳定高效的运作:而到了现在,我自己不得不说我已经逐渐习惯并依赖GC与我的程序“共同奔跑”了,对“delete”这个习 ...