这两个题几乎一样,只是说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. 微信小程序~基础组件

    (1)视图容器 名称 功能说明 movable-view 可移动的视图容器,在页面中可以拖拽滑动 cover-image 覆盖在原生组件之上的图片视图 cover-view 覆盖在原生组件之上的文本视 ...

  2. java(Hello World) 常量 变量和注意事项

    一.java的入门程序 java语言的简单介绍 关于java语言的特点:(1) 简单性和c++相比,java没有头文件.指针.运算符重载等,java语言相当于是一个比较纯净版的c++.(2) 面对对象 ...

  3. 抖音热门BGM爬虫下载

    下午无聊在某网上刷了会儿抖音,发现有些音乐还是挺好听的,可以用来做手机铃声,于是想办法从某网上把歌曲爬下来 附上代码: #!/usr/bin/env python # -*- coding: utf- ...

  4. 原生js获取display属性注意事项

    原生js获取diaplay属性需要在标签上写行间样式style='display:none/block;' <div style="display:block;">&l ...

  5. .net框架 - Enum枚举

    概要 在C#或C++,java等一些计算机编程语言中,枚举类型是一种基本数据类型而不是构造数据类型. 在C语言等计算机编程语言中,它是一种构造数据类型. 它用于声明一组命名的常数,当一个变量有几种可能 ...

  6. 结构型模式(六) 享元模式(Flyweight)

    一.动机(Motivate) 在软件系统中,采用纯粹对象方案的问题在于大量细粒度的对象会很快充斥在系统中,从而带来很高的运行时代价--主要指内存需求方面的代价.如何在避免大量细粒度对象问题的同时,让外 ...

  7. STAF Trust Level 4 required for FS copy request

    C#中使用STAF从本机传输文件到远程的电脑,出现如下错误: 解决方法: 修改service.ini文件,添加信任IP段,并将trust level 设置为5,修改后文件内容如下 trace enab ...

  8. Map的常用实现类及Entry的用法

    public static void main(String[] args) {    //map  键值对  json格式根据你的键名来获取对应的值  //特点 :无序.以键值对的形式添加元素,键不 ...

  9. Base64原理解析与使用

    一.Base64编码由来 为什么会有Base64编码呢?因为有些网络传送渠道并不支持所有的字节,例如传统的邮件只支持可见字符的传送,像ASCII码的控制字符就 不能通过邮件传送.这样用途就受到了很大的 ...

  10. C语言malloc的用法及详解

    #include <stdio.h> #include <stdlib.h> void freem(int* p){ #include <stdio.h> #inc ...