611. Valid Triangle Number

1.Problem

Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.

Example 1:

Input: [2,2,3,4]
Output: 3
Explanation:
Valid combinations are:
2,3,4 (using the first 2)
2,3,4 (using the second 2)
2,2,3

Note:

    1. The length of the given array won't exceed 1000.
    2. The integers in the given array are in the range of [0, 1000].

2.Solution

题目的大意是给定一个元素可能存在重复的一维数组,给出其中能组成合法三角形的组合数。

3.Code

//Solution1
//时间复杂度O(N^3),空间复杂度O(logN)(排序导致)
//先对nums数组进行排序,如从大小三条边为 a,b,c,只需判断 a + b > c 成立与否 =》三条边能否构成三角形
class Solution {
public int triangleNumber(int[] nums) {
Arrays.sort(nums);
int l = nums.length;
int count = 0;
for ( int i = 0 ; i < l - 2 ; i++ ) {
for ( int j = i + 1 ; j < l - 1 ; j++ ) {
for ( int k = j + 1 ; k < l ; k++ ) {
if ( nums[i] + nums[j] > nums[k]) {
count++;
}
}
}
}
return count;
} }
//Solution2 二分查找
//时间复杂度O(N^2 * log(N)),空间复杂度O(log N)
class Solution {
public int triangleNumber(int[] nums) {
Arrays.sort(nums);
int l = nums.length;
int count = 0;
for ( int i = 0 ; i < l - 2 ; i++ ) {
int k = i + 2;
for ( int j = i + 1 ; j < l - 1 && nums[i] != 0 ; j++ ) {
k = binarySearch(k,l - 1,nums,nums[i] + nums[j]);
count += k - j - 1;
}
}
return count;
} public int binarySearch( int start , int end , int[] nums , int target ) {
while ( start <= end ) {
int mid = ( end - start ) / 2 + start;
if ( nums[mid] < target ) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return start;
} }
//Solution3,时间复杂度O(N^2) ,空间复杂度O(lgN)
  • ime complexity : O(n2)O(n^2)O(n​2​​). Loop of kkk and jjj will be executed O(n2)O(n^2)O(n​2​​) times in total, because, we do not reinitialize the value of kkk for a new value of jjj chosen(for the same iii). Thus the complexity will be O(n^2+n^2)=O(n^2).

  • Space complexity : O(logn)O(logn)O(logn). Sorting takes O(logn) space.

class Solution {
public int triangleNumber(int[] nums) {
Arrays.sort(nums);
int l = nums.length;
int count = 0;
for ( int i = 0 ; i < l - 2 ; i++ ) {
int k = i + 2;
for ( int j = i + 1 ; j < l - 1 && nums[i] != 0 ; j++ ) {
while ( k < l && (nums[i] + nums[j] > nums[k]) ) {
k++;
}
count += k - j - 1;
}
}
return count;
}
}

Leetcode 之 Valid Triangle Number的更多相关文章

  1. leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)

    这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...

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

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

  3. 【leetcode】Valid Triangle Number

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

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

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

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

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

  6. LeetCode Valid Triangle Number

    原题链接在这里:https://leetcode.com/problems/valid-triangle-number/description/ 题目: Given an array consists ...

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

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

  8. LeetCode题解之Valid Triangle Number

    1.题目描述 2.问题分析 暴力计算 3.代码 int triangleNumber(vector<int>& nums) { ; ) return res; ; i < n ...

  9. [Swift]LeetCode611. 有效三角形的个数 | Valid Triangle Number

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

随机推荐

  1. Window 窗口类

    窗口类 WNDCLASS 总结 总结为下面的几个问题: . 什么是窗口类 . 窗口类的三种类型 . 窗口类各字段含义 . 窗口类的注册和注销 . 如何使用窗口类,子类化.超类化是什么 下面分别描述: ...

  2. 资源文件properties的存放路径

    参考这篇博客:http://lavasoft.blog.51cto.com/62575/184605 目前看来,大多数的做法是这样的: 比如a.properties需要被com.xxx.yyy这个包中 ...

  3. VMware虚拟机实用经验总结十一条

    转:http://article.pchome.net/content-948404.html 1.VMware虚拟机实用经验之支持的Guest OS: VMWare支持如下Guest OS:MS-D ...

  4. linux--jdk安装与配置

    此处以centos下jdkjdk1.8.0_161安装(此处为rpm安装事例)为例 1.java官网下载页找到对应版本的jdk下载链接地址 2.下载对应版本的rmp包到服务器,执行如下指令: wget ...

  5. hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙

    /** 题目:hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4106 ...

  6. hdu3667 Transportation 费用与流量平方成正比的最小流 拆边法+最小费用最大流

    /** 题目:hdu3667 Transportation 拆边法+最小费用最大流 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 题意:n个城市由 ...

  7. awk数组处理字符串合并

    需求: 有一文本文件 lessons.txt 内容如下,请使用 awk 处理该文本,并输出内容如 result.txt lessons.txt: 634751 预排 568688 预排 386760 ...

  8. dva学习---effects异步中通过select获取当前的state

    根据 在组件中dispatch一个action的例子中,如果要在effects中对于param数据和当前的state数据进行再出处理,这里怎么获取state呢?采用select,如下:       e ...

  9. C# mvc中动态压缩文件发送给前端

    前言 帮朋友解决一个C#中发送压缩文件的的问题,因为感觉解释起来更麻烦,就直接用几分钟时间写了个小Demo.本着"走过路过"不错过的原则,也给记录一下. 1.前端代码 非常简单的一 ...

  10. 怎么用ChemDraw 15.1 Pro绘制彩色结构

    ChemOffice 15是最新的ChemDraw化学工具套件,合理的使用这套软件可以大幅度的提高研究人员的工作效率.也有一些化学老师使用这套化学绘图软件教学,其可以绘制彩色结构有效增强教案说服力并吸 ...