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. 个人的Linux实用命令

    Linux下的命令有那么多,我不可能也不想去把每一个都记住,列举一些自己工作中很实用的命令,这些命令或许不是很常用,但是有时候却离不了. 1.网络方面 service iptables start/s ...

  2. PHP学习笔记(2)字符串函数

    注意: 1.双引号中可以解析变量 2.单引号中不会解析变量

  3. mongodb数据库安装与卸载

    此处以centos下monggodb3.4版本安装为例,可参考官网安装教程 步骤如下: 1.配置mongodb ym源 vi /etc/yum.repos.d/mongodb-org-3.4.repo ...

  4. 【Mac + Appium + Python3.6学习(五)】之常用的Android自动化测试API总结

    Github测试样例地址:https://github.com/appium-boneyard/sample-code/tree/master/sample-code/examples ①定位text ...

  5. easyui -grid每列绑定tooltip

    /**用法:*/function doCellTip() { $('#dg').datagrid('doCellTip', { 'max-width': '100px' });} /** * 扩展两个 ...

  6. poj 2987(最大权闭合图+割边最少)

    题目链接:http://poj.org/problem?id=2987 思路:标准的最大权闭合图,构图:从源点s向每个正收益点连边,容量为收益:从每个负收益点向汇点t连边,容量为收益的相反数:对于i是 ...

  7. grid++report中篇

    QQ:1187362408 欢迎技术交流和学习 grid++report中篇(grid++report): TODO: 1.grid++report:简单介绍( Grid++Report 是一款高性能 ...

  8. vmware 虚拟机下 ubuntu 与主机共享锐捷

    一直以来.想要学习 linux ,在 vm 虚拟机下安装了 ubuntu 系统. 可是这个系统并不能上网.原因就是 vm 虚拟机的虚拟网卡会和锐捷冲突.锐捷会检測到多网卡,断开网络,所以不得不禁用 v ...

  9. java进阶-多线程学习笔记

    多线程学习笔记 1.什么是线程 操作系统中 打开一个程序就是一个进程 一个进程可以创建多个线程 现在系统中 系统调度的最小单元是线程 2.多线程有什么用? 发挥多核CPU的优势 如果使用多线程 将计算 ...

  10. Android UI开发第三十五篇——AppCompat实现Action Bar

    每一位Android开发者对Action Bar这种设计都不陌生了,毕竟它已经发布了至少两年了.Android团队发布Action Bar设计规范时同时放出了ActionBar的Api来支持这种设计. ...