这两个题几乎一样,只是说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)的更多相关文章

  1. LeetCode 611. Valid Triangle Number有效三角形的个数 (C++)

    题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...

  2. Leetcode 之 Valid Triangle Number

    611. Valid Triangle Number 1.Problem Given an array consists of non-negative integers, your task is ...

  3. 【LeetCode】611. Valid Triangle Number 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/valid-tri ...

  4. **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 ...

  5. 【leetcode】Valid Triangle Number

    题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...

  6. 611. Valid Triangle Number

    Given an array consists of non-negative integers, your task is to count the number of triplets chose ...

  7. 611. Valid Triangle Number三角形计数

    [抄题]: 给定一个整数数组,在该数组中,寻找三个数,分别代表三角形三条边的长度,问,可以寻找到多少组这样的三个数来组成三角形? [暴力解法]: 全部都用for循环 时间分析: 空间分析: [思维问题 ...

  8. LeetCode 611. 有效三角形的个数(Valid Triangle Number)

    611. 有效三角形的个数 611. Valid Triangle Number 题目描述 LeetCode LeetCode LeetCode611. Valid Triangle Number中等 ...

  9. [LeetCode] Valid Triangle Number 合法的三角形个数

    Given an array consists of non-negative integers, your task is to count the number of triplets chose ...

随机推荐

  1. Spark-2.3.2 Java SparkSQL的自定义HBase数据源

    由于SparkSQL不支持HBase的数据源(HBase-1.1.2),网上有很多是采用Hortonworks的SHC,而SparkSQL操作HBase自定义数据源大多数都是基于Scala实现,我就自 ...

  2. brief 程序注释

    /** * @brief Acceleration (g's) in body frame. * Embedded MPL defines gravity as positive accelerati ...

  3. 五.Protobuf3 枚举

    Protobuf3 枚举 定义消息类型时,您可能希望它的一个字段有一个预定义的值列表.例如,假设您希望为每个SearchRequest添加一个corpus字段,其中语料库可以是UNIVERSAL.WE ...

  4. Idea和eclipse安装activiti插件

    eclipse安装:help>install new software>add             有外网状态下 输入  :http://www.activiti.org/design ...

  5. tensorflow LSTM+CTC使用详解

    最近用tensorflow写了个OCR的程序,在实现的过程中,发现自己还是跳了不少坑,在这里做一个记录,便于以后回忆.主要的内容有lstm+ctc具体的输入输出,以及TF中的CTC和百度开源的warp ...

  6. GBDT(梯度提升树) 原理小结

    在之前博客中,我们对Boosting家族的Adaboost算法做了总结,本文就对Boosting家族中另一个重要的算法梯度提升树(Gradient Boosting Decison Tree, 以下简 ...

  7. BlockingCollection<T> 类实现 列队操作

    官方文档 为实现 IProducerConsumerCollection<T> 的线程安全集合提供阻塞和限制功能. 通过 BlockingCollection<T> 实现列队调 ...

  8. web场景的监控

    在一台可以连外网的主机上创建web场景, 添加步骤,zabbix可以步骤化web页面,例如,登陆操作,正常操作是要先验证用户获取token,step1就可以是先访问token页面,获取到返回的toke ...

  9. Luogu P1903 BZOJ 2120 数颜色 带修改的莫队

    https://www.luogu.org/problemnew/show/P1903 之前切过这道题,复习莫队再切一遍,不过我之前写的是主席树和树状数组,也不知道我当时怎么想的…… 这个题卡常我没写 ...

  10. javascript练习题

    function Vertex(city, x) { this.name = city; this.num = x; } var node0 = new Vertex("邯郸", ...