611. 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].
找出满足组成三角形的三条边的个数
思路:组成三角形条件是两条较短的边的和大于第三条边
1 public int triangleNumber(int[] nums) {
2 Arrays.sort(nums);
3 int count = 0;
4 for (int i = nums.length - 1; i >= 2; i--) {
5 int left = 0, right = i - 1;
6 while (left < right) {
7 if (nums[left] + nums[right] > nums[i]) { //最短的两条边之和大于最长的边nums[i]
8 count += right - left; //left后面的数字,都可以结合right构成三角形
9 right--;
10 } else {
11 left++;
12 }
13 }
14 }
15 return count;
16 }
611. Valid Triangle Number的更多相关文章
- leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)
这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...
- **611. Valid Triangle Number three pointer O(n^3) -> square(binary search larget number smaller than target)
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
- 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】611. Valid Triangle Number 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/valid-tri ...
- 611. Valid Triangle Number三角形计数
[抄题]: 给定一个整数数组,在该数组中,寻找三个数,分别代表三角形三条边的长度,问,可以寻找到多少组这样的三个数来组成三角形? [暴力解法]: 全部都用for循环 时间分析: 空间分析: [思维问题 ...
- 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] Valid Triangle Number 合法的三角形个数
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
- [Swift]LeetCode611. 有效三角形的个数 | Valid Triangle Number
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
随机推荐
- word里搜狗输入法出不来,可以按ctrl+空格键
word里搜狗输入法出不来,可以按ctrl+空格键
- 查找局域网中未知设备的IP
先运行net view,然后再运行arp -a 设备启动前后对比IP列表
- React-Router(一)
React-Router基础知识 import React from "react"; import { BrowserRouter as Router, Switch, Rout ...
- c++之折半查找(二分查找)递归与非递归的实现
递归实现 template<typename T> int binary_search2(const T arr[], const int left, const int right, c ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】1014. Capacity To Ship Packages Within D Days 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 1080 - Binary Simulation
1080 - Binary Simulation PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 64 ...
- 5G的到来
通信改变未来,从古至今信息的传输和获取从来就没有缺少过,之所以谁能取得胜利就是谁掌握的资源多,其中信息资源尤为重要,只要获取到更多的信息你就能提前做出应对策略.因此未来一定是信息的未来,作为信息传输的 ...
- 第二十个知识点:Merkle-Damgaard hash函数如何构造
第二十个知识点:Merkle-Damgaard hash函数如何构造 这里讲的是MD变换,MD变换的全称为Merkle-Damgaard变换.我们平时接触的hash函数都是先构造出一个防碰撞的压缩函数 ...
- 第三十个知识点:大致简述密钥协商中的BR安全定义。
第三十个知识点:大致简述密钥协商中的BR安全定义. 在两方之间建密钥共享是一件密码学中古老的问题.就算只考虑定义也比标准加密困难的多.尽管古典的Diffie-Hellman协议在1976年思路解决了这 ...