leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)
这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式。
611. Valid Triangle Number
判断这个数组能组成三角形的个数,利用两边之和大于第三边
https://www.cnblogs.com/grandyang/p/7053730.html
暴力方法是找所有3个数的组合,看满足条件的,时间复杂度是O(n3)。
此方法时间复杂度为O(n2)。先排序,然后left是第0个,right是i-1,进行比较。如果left+right大于了当前遍历的值,那么left往上移动到right中经过的值都能满足,因为left相对于那些数反而是最小的,既然小的都能满足,大的也能满足。
class Solution {
public:
int triangleNumber(vector<int>& nums) {
if(nums.size() <= )
return ;
int res = ;
sort(nums.begin(),nums.end());
for(int i = ;i < nums.size();i++){
int left = ,right = i-;
while(left < right){
if(nums[left] + nums[right] > nums[i]){
res += right - left;
right--;
}
else
left++;
}
}
return res;
}
};
259. 3Sum Smaller
http://www.cnblogs.com/grandyang/p/5235086.html
这个题是寻找3个数之和小于目标值,暴力的方法也是将所有3个数的可能性遍历寻找满足条件的个数。
这个题的O(n2)的方法与611. Valid Triangle Number几乎一样,只是说因为这个题寻找的是小于目标值,所以满足条件后,应该是right往下走,因为right往下的才更小才能满足条件。
注意:这里选择是left是0,right是i-1,也就是说我每次作为基本递归的是最大的那个数
class Solution {
public:
/**
* @param nums: an array of n integers
* @param target: a target
* @return: the number of index triplets satisfy the condition nums[i] + nums[j] + nums[k] < target
*/
int threeSumSmaller(vector<int> &nums, int target) {
// Write your code here
if(nums.size() <= )
return ;
sort(nums.begin(),nums.end());
int res = ;
for(int i = ;i < nums.size();i++){
int left = ,right = i-;
while(left < right){
if(nums[left] + nums[right] + nums[i] < target){
res += right - left;
left++;
}
else
right--;
}
}
return res;
}
};
leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)的更多相关文章
- LeetCode 611. Valid Triangle Number有效三角形的个数 (C++)
题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...
- Leetcode 之 Valid Triangle Number
611. Valid Triangle Number 1.Problem Given an array consists of non-negative integers, your task is ...
- 【LeetCode】611. Valid Triangle Number 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/valid-tri ...
- **611. Valid Triangle Number three pointer O(n^3) -> square(binary search larget number smaller than target)
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
- 【leetcode】Valid Triangle Number
题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...
- 611. Valid Triangle Number
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
- 611. Valid Triangle Number三角形计数
[抄题]: 给定一个整数数组,在该数组中,寻找三个数,分别代表三角形三条边的长度,问,可以寻找到多少组这样的三个数来组成三角形? [暴力解法]: 全部都用for循环 时间分析: 空间分析: [思维问题 ...
- LeetCode 611. 有效三角形的个数(Valid Triangle Number)
611. 有效三角形的个数 611. Valid Triangle Number 题目描述 LeetCode LeetCode LeetCode611. Valid Triangle Number中等 ...
- [LeetCode] Valid Triangle Number 合法的三角形个数
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
随机推荐
- 使用SikuliX定位Object(flash)元素
先说一下背景,这个是我们测试的系统上的一个上传文件的地方,但是用传统的selenium方法很难定位的到.具体的样子是下面这样的. 使用id等属性定位做点击操作好像不能直接操作.无奈之下,只好从网上找找 ...
- No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.lang.String, java.lang.String>]
java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...
- SVM: 使用kernels(核函数)的整个SVM算法过程
将所有的样本都选做landmarks 一种方法是将所有的training data都做为landmarks,这样就会有m个landmarks(m个trainnign data),这样features就 ...
- (java)selenium webdriver学习--通过id、name定位,输入内容,搜索,关闭操作、通过tagname查找元素
selenium webdriver学习--通过id.name定位,输入内容,搜索,关闭操作:通过tagname查找元素 打开谷歌浏览器,输入不同的网站,搜索框的定位含有不同元素(有时为id,有时为n ...
- Attention Model详解
要是关注深度学习在自然语言处理方面的研究进展,我相信你一定听说过Attention Model(后文有时会简称AM模型)这个词.AM模型应该说是过去一年来NLP领域中的重要进展之一,在很多场景被证明有 ...
- 向指定URL发送GET和POST请求
package com.sinosoft.ap.wj.common.util; import java.io.BufferedReader;import java.io.IOException;imp ...
- greenplum 下载地址
一.推荐使用下面下载地址 https://network.pivotal.io/products/pivotal-gpdb#/releases/158026/file_groups/1083 二.官网 ...
- mpvue搭建小程序框架
http://mpvue.com/mpvue/ 美团开源了mpvue 由于mpvue框架是完全基于Vue框架的(重写了其runtime和compiler) 运行时框架 runtime 和代码编译器 c ...
- shell编程题(四)
编译当前目录下的所有.c文件 #!/bin/bash ] ;] 输入参数个数 echo "Please follow up file.c!" echo "eg: ./ma ...
- 深度Linux /etc/profile 环境变量生效问题
/etc/profile 环境变量生效问题 设置了环境变量后 ,使用source /etc/profile生效后,每次关闭终端后,都需要重新输入source /etc/profile命令使环境变量生效 ...