这两个题几乎一样,只是说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. IDEA实用教程(十)—— 配置Maven的全局设置

    使用之前需要提前安装好Maven 第一步 第二步

  2. 基于Java+Selenium的WebUI自动化测试框架(十)-----读取Excel文件(JXL)

    之前,我们使用了读取XML文件的方式来实现页面元素的读取,并做成了基础页面类.下面,我们来进行一些扩展,通过Excel来读取页面元素. Excel的使用,大多数人应该都不陌生.那么Java读取Exce ...

  3. Codeforces C. A Simple Task(状态压缩dp)

    题目描述:  A Simple Task time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  4. Maven02 -学习总结&学习文档 -踩坑123

    maven在开发中的作用 ①自动添加第三方 jar 包 在今天的 JavaEE 开发领域,有大量的第三方框架和工具可以供我们使用.要使用这些 jar 包最简单的方法就是复制粘贴到 WEB-INF/li ...

  5. NET Framework 的泛型

    NET Framework 的泛型 泛型是具有占位符(类型参数)的类.结构.接口和方法,这些占位符是类.结构.接口和方法所存储或使用的一个或多个类型的占位符.泛型集合类可以将类型参数用作它所存储的对象 ...

  6. Python win32gui调用窗口到最前面

    Python win32gui调用窗口到最前面 0要写一个轮询几个重要页面的程序,不停的在大屏上进行刷新,通过pywin32模块下的SetForegroundWindow函数调用时,会出现error: ...

  7. 华硕ASUS U5800GE驱动

    重要的触摸板 微软商店 ASUS Keyboard Hotkeys 设备管理器 人体学输入设备 ASUS Precision Touchpad (ScreenPad) Asus ScreenPad D ...

  8. CentOS 7.5静默安装oracle 11g

    1.安装前环境准备 1.1.配置本地yum源 #因公司内网环境,没有互联网,所以需要配置本地yum源,安装所需依赖包等. #挂载ios镜像centos7.5-1804 [root@oracle ~]# ...

  9. 洛谷 P1439 【模板】最长公共子序列 题解

    每日一题 day40 打卡 Analysis 因为两个序列都是1~n 的全排列,那么两个序列元素互异且相同,也就是说只是位置不同罢了,那么我们通过一个book数组将A序列的数字在B序列中的位置表示出来 ...

  10. 多路选择器,加法器原理及verilog实现

    1.数据选择器是指经过选择,把多个通道的数据传到唯一的公共数据通道上.实现数据选择功能的逻辑电路称为数据选择器,它的作用相当于多个输入的单刀多掷开关.本例程以四选一数据选择器(电平触发)为例. 四选一 ...