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 ...
随机推荐
- cmake之if
note if 要 与endif配对使用 语法含义 表达式 含义 if (not expression) 与 expression相反 if (var1 AND var2) var1与var2都为真时 ...
- 【LeetCode】234. Palindrome Linked List 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- Description has only two Sentences(hdu3307)
Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/327 ...
- 深入理解Java虚拟机二:垃圾收集与内存分配
垃圾收集:垃圾收集要完成三件事,包括哪些内存需要回收,什么时候回收及如何回收. 1.需要回收的内存判定:没有引用指向原先分配给某个对象的内存时,则该内存是需要回收的垃圾 Java垃圾收集器在对内存进行 ...
- [zbar]zbar条码扫描器解析示例
// // Created by leoxae on 2020/3/30. // #include "BarCodeRecogntion.h" string BarCode::Ba ...
- [多线程]async异步操作的使用实例及不同策略的对比
#include <iostream> #include <thread> #include <mutex> #include <iostream> / ...
- 【MySQL作业】SELECT 数据查询——美和易思select 选择列表应用习题
点击打开所使用到的数据库>>> 1.查询所有客户的地址和电话号码. SELECT address, phone FROM customer 2.查询所有商品的名称.种类和单价信息. ...
- Ranger-Sqoop2插件安装
Ranger-Sqoop2插件安装,基于Ranger版本1.0.0,支持Sqoop2版本1.99.7. 1.获取安装包 scp root@10.43.159.11:/home/compile/rang ...
- x86-2-保护模式(protect mode)
x86-2-保护模式(protect mode) 引入保护模式的原因: 操作系统负责计算机上的所有软件和硬件的管理,它可以百分百操作计算机的所有内容.但是,操作系统上编写的用户程序却应当有所限制,比如 ...
- 使用 Docker 构建和运行自己的镜像
步骤 首先,从 GitHub 中克隆示例项目: git clone https://github.com/dockersamples/node-bulletin-board cd node-bulle ...