611. Valid Triangle Number
来源:互联网 发布:中国十大禁菜 知乎 编辑:程序博客网 时间:2024/06/07 14:15
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
My solution:
class Solution {public: int triangleNumber(vector<int>& nums) { int pile[2000],count=0;//pile初始化空间要大一些,1005不行 memset(pile,0,sizeof(pile)); int x,y,tmp; for(int i=0; i<nums.size(); i++) { tmp=nums[i]; pile[tmp]++; } for(int a=0; a<=1000; a++) { for(int b=a; b<=1000; b++) { if(pile[a]!=0 && pile[b]!=0) { for(int c=max(b-a+1,b); c<a+b; c++) { if(pile[c]!=0) { if(a==b && a!=c && pile[a]>=2) count+=pile[a]*(pile[a]-1)*pile[c]/2; else if(a!=b && b==c && pile[b]>=2) count+=pile[b]*(pile[b]-1)*pile[a]/2; else if(a==b && a==c && pile[a]>=3) count+=pile[a]*(pile[a]-1)*(pile[a]-2)/6; else if((b-a)*(b-c)*(a-c)!=0) count+=pile[a]*pile[b]*pile[c]; } } } } } return count; }};
clean solution:Pointers O(N2)
class Solution {public: int triangleNumber(vector<int>& a) { int res = 0; sort(a.begin(), a.end()); reverse(a.begin(), a.end()); // a is decreasing for (int i = 0; i + 2 < a.size(); i++) { for (int j = i + 1, k = a.size() - 1; j < k; j++) { while (j < k && a[j] + a[k] <= a[i]) { k--; } res += k - j; } } return res; }};
O(N2):
class Solution {public: int triangleNumber(vector<int>& nums) { vector<int> snums(nums); sort(snums.begin(), snums.end()); int count = 0; for ( int n = nums.size(), k = n - 1; k > 1; --k ) { int i = 0, j = k - 1; while ( i < j ) { // any value x between i...j will satisfy snums[x] + snums[j] > snums[k] // and because snums[k] > snums[j] > snums[x] >= 0, they will always satisfy // snums[k] + snums[x] > snums[j] and snums[k] + snums[j] > snums[x] if ( snums[i] + snums[j] > snums[k] ) count += --j - i + 1; else ++i; } } return count; }};// 243 / 243 test cases passed.// Status: Accepted// Runtime: 59 ms
基本上好的题解就是,先用sort()排序,然后固定k为三边最大遍历一次,嵌套i从最小值开始遍历,i<j<k
,从k-1开始找符合条件的第一个j(也是最大的那个),找到第一个j之和,j到i之间的数都是符合要求的。
i<j<k
,三角形三边成立的条件(i+k>j, j+k>i
都是显然成立的,所以只需要判断i+j<k
即可)
而使用while可以减少一个循环,因为j是从大到小递减, snums[i] + snums[j] > snums[k],如果不满足这个条件,说明i不够大,所以i++,如果满足条件,就要找有没有更小的j也满足,所以j–。(这里很妙,需要理解)
阅读全文
0 0
- 611. Valid Triangle Number
- 611. Valid Triangle Number
- 611. Valid Triangle Number
- 611. Valid Triangle Number
- 611. Valid Triangle Number
- 611. Valid Triangle Number
- 611. Valid Triangle Number
- 611. Valid Triangle Number
- 611. Valid Triangle Number
- 611. Valid Triangle Number
- [LeetCode]611. Valid Triangle Number
- [LeetCode] 611. Valid Triangle Number
- [leetcode]611. Valid Triangle Number
- leetcode 611. Valid Triangle Number
- [leetcode] 611. Valid Triangle Number
- Leetcode:611. Valid Triangle Number
- leetcode 611. Valid Triangle Number
- 【LeetCode】611.Valid Triangle Number解题报告
- java语言之面向对象
- 使用多路复用套接字 IO 提升性能
- ElasticSearch学习笔记(一)ElasticSearch安装
- Iterator遍历有多级子集合的集合
- 虚拟机virtualbox中安装了centos,docker,nginx后出现的问题 403
- 611. Valid Triangle Number
- PAT 1027打印沙漏
- 有了Docker的程序猿们就能开启“上帝视角”?
- Spring Cloud Stream 多通道创建
- 脚本语言和其他一些编程语言的区别
- Mysql 常用基本命令(2)
- 反汇编一个简单的C
- nginx的安装与基础配置
- HDU 1028 Ignatius and the Princess III