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. 微信小程度腾讯地图SDK使用方法

    在开发过程中,不少人肯定遇到过要用到地图,那么在小程序里,腾讯也给出了相应的SDK供我们来使用.那么接下来,就介绍下如何使用该SDK实现获取经纬度然后显示当前用户所在地址 首先第一步:下载腾讯地图SD ...

  2. datatables 相关文章

    http://blog.csdn.net/zhu_xiao_yuan/article/details/51252300 datatables参数配置详解  http://blog.csdn.net/j ...

  3. 今天升级netbean出错

    出现:无法初始化 UI netbean 由于ubuntun装的是open jdk 直接删除open jdk就可以 sudo apt-get autoremove open-jdk*

  4. 从客户端检测到有潜在危险的Request.Form 值”错误提示

    http://www.cnblogs.com/UouHt/archive/2008/10/30/1322697.html asp.net开发中,经常遇到“从客户端检测到有潜在危险的Request.Fo ...

  5. 【tyvj】P2065 「Poetize10」封印一击(贪心+线段树/差分)

    http://new.tyvj.cn/p/2065 我就不说我很sb的用线段树来维护值...... 本机自测的时候想了老半天没想出怎么维护点在所有区间被多少区间包含的方法.最后一小时才想出来线段树(果 ...

  6. (转)前端:将网站打造成单页面应用SPA

    前端:将网站打造成单页面应用SPA(一) Coffce 680 6月19日 发布 推荐 6 推荐 收藏 85 收藏,3.1k 浏览 前言 不知你有没有发现,像Github.百度.微博等这些大站,已经不 ...

  7. grid++report中篇

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

  8. 使用Lingo增强JMS

    虽然activemq+jencks的jms轻量级解决方案已经很好地在psa中work了,尤其spring的JmsTemplate使得代码更简单,但是还是存在问题. 问题来自暑期做psa的时候,link ...

  9. sql server数据库行转列及巧用case when、和row_number用法例子

    select 身份证号码, MAX(t.单位编号) 单位编号, MAX(t.姓名) 姓名, MAX(case when t.rows=1 then convert(varchar(max),疾病名称) ...

  10. 【BZOJ4704】旅行 树链剖分+可持久化线段树

    [BZOJ4704]旅行 Description 在Berland,有n个城堡.每个城堡恰好属于一个领主.不同的城堡属于不同的领主.在所有领主中有一个是国王,其他的每个领主都直接隶属于另一位领主,并且 ...