LeetCode Valid Triangle Number
原题链接在这里:https://leetcode.com/problems/valid-triangle-number/
题目:
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:
- The length of the given array won't exceed 1000.
- The integers in the given array are in the range of [0, 1000].
题解:
也是Two Pointers, 构成三角要求三条边成a + b > c. 设nums[i] 为c, 前面的数中选出nums[l]为a, nums[r]为b.
若是nums[l] + nums[r] > c则符合要求,若继续向右移动l, 有r-l种组合都包括num[r]. 所以res += r-l. r左移一位, 之后可能还有符合条件的组合.
若是nums[l] + nums[r] <= c, 则向右移动l.
Time Complexity: O(n^2). sort 用时O(nlogn), 对于每一个c, Two Points用时O(n). n = nums.length.
Space: O(1).
AC Java:
class Solution {
public int triangleNumber(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
}
int res = 0;
Arrays.sort(nums);
for(int i = 2; i<nums.length; i++){
int l = 0;
int r = i-1;
while(l<r){
if(nums[l] + nums[r] > nums[i]){
res += r-l;
r--;
}else{
l++;
}
}
}
return res;
}
}
类似3Sum Smaller.
LeetCode Valid Triangle Number的更多相关文章
- [LeetCode] Valid Triangle Number 合法的三角形个数
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
- LeetCode 611. 有效三角形的个数(Valid Triangle Number)
611. 有效三角形的个数 611. Valid Triangle Number 题目描述 LeetCode LeetCode LeetCode611. Valid Triangle Number中等 ...
- Leetcode 之 Valid Triangle Number
611. Valid Triangle Number 1.Problem Given an array consists of non-negative integers, your task is ...
- leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)
这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...
- 【LeetCode】611. Valid Triangle Number 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/valid-tri ...
- LeetCode 611. Valid Triangle Number有效三角形的个数 (C++)
题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...
- 【leetcode】Valid Triangle Number
题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...
- LeetCode题解之Valid Triangle Number
1.题目描述 2.问题分析 暴力计算 3.代码 int triangleNumber(vector<int>& nums) { ; ) return res; ; i < n ...
- [Swift]LeetCode611. 有效三角形的个数 | Valid Triangle Number
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
随机推荐
- JavaScript传递的是引用的副本
看例子 var test1 = function (args) { args.name = "lcc2"; args = { name: "lcc3" }; } ...
- Boot-Repair&usb_repair
https://help.ubuntu.com/community/Boot-Repair https://askubuntu.com/questions/500647/unable-to-mount ...
- MySQLdump增量备份、完全备份与恢复
在数据库表丢失或损坏的情况下,备份你的数据库是很重要的.如果发生系统崩溃,你肯定想能够将你的表尽可能丢失最少的数据恢复到崩溃发生时的状态.场景:每周日执行一次完全备份,每天下午1点执行MySQLdum ...
- (补充一)CountDownLatch
引言: 在学习单例模式时候,用到了锁synchronized的概念,在多线程中又用到了CountDownLatch的概念 CountDownLatch是一个同步工具类,它允许一个或多个线程一直等待, ...
- 【bzoj3298】[USACO 2011Open]cow checkers(博弈论)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=3298 博弈论经典结论题,我也没什么好说的.matrix67大佬比我想得深入的多:捡石子 ...
- 织梦导航 currentstyle 点击li添加class类 样式
<!--导航开始--> <div class="global_nav_wrap"> <ul class="nav nav-pills&quo ...
- 怎样发布一个工程到自己的GitHub
本人小白一枚: 第一次通过命令行的形式将自己的工程发布到自己的GitHub上去,还是请教了大神之后才完成的.通过总结经验,小白也掌握了一些关于往代码托管平台上发布工程的方式,其实只要用心学,里面的东西 ...
- Go语言 channel 管道 阻塞 死锁 经典问题
建议阅读:14.2协程间的信道 问题:为什么代码1会报死锁的错误,而代码2不会报错? 代码1: package main import ( "fmt" ) func main() ...
- Dib to Bitmap doesn't work in WPF
一.Dib to Bitmap doesn't work in WPF 代码如下: protected byte[] BitmapFromDIB(IntPtr pDIB, IntPtr pPix) { ...
- python Tkinter图形用户编程简单学习(一)
Events(事件) Events are given as strings, using a special event syntax:事件以字符串的方式给出,使用特殊的事件语法:<modif ...