[leetcode]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:

  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].

题意:

给定数组,可由数组中的数字组成多少个valid三角形

解本题的背景知识: 【Math fact】The sum of two sides of a triangle must be greater than the third one

Solution1: Two Pointers (similar as 3 Sum problem)

1. sort array

2. lock pointer k at the trail (must be largest side)

[2,  2,  3,  4]

^k

3. set pointer left at 0, right at k-1

[2,  2,  3,  4]

^k

^left            ^right

Lock pointer k and pointer right

check if nums[left] + nums[right] > nums[k]

(1)YES.  Then we can say  (nums[left + 1] / nums[left + 2]....) + nums[right] > nums[k]

So combination count: right - left

Then lock pointer k and pointer right -1 to get next combination count

(2) No.   Then left ++

code

 1 /*
 2 Time: O(n^2)
 3 Space: O(1)
 4 */
 5
 6 class Solution {
 7     public int triangleNumber(int[] nums) {
 8         if(nums == null || nums.length == 0) return 0;
 9         Arrays.sort(nums);
10         int result = 0;
11          for (int k = nums.length - 1; k > 0 ; k--) {
12             int left = 0;
13             int right = k - 1 ;
14             while (left < right) {
15                 if (nums[left] + nums[right] > nums[k]) {
16                     result += (right - left);
17                     right--;
18                 }
19                 else {
20                     left++;
21                 }
22             }
23         }
24         return result;
25     }
26 }

原文地址:https://www.cnblogs.com/liuliu5151/p/10807456.html

时间: 2024-08-29 05:25:19

[leetcode]611. Valid Triangle Number有效三角数的相关文章

【leetcode】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 c

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 combi

611. Valid Triangle Number三角形计数

[抄题]: 给定一个整数数组,在该数组中,寻找三个数,分别代表三角形三条边的长度,问,可以寻找到多少组这样的三个数来组成三角形? [暴力解法]: 全部都用for循环 时间分析: 空间分析: [思维问题]: 可以用两层循环:for循环中嵌套while,用过但是没意识 [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: [一刷]: [二刷]: [三刷]: [四刷]: [五刷]: [五分钟肉眼debug的结果]: [总结]:

[LeetCode] 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 combi

LeetCode题解之Valid Triangle Number

1.题目描述 2.问题分析 暴力计算 3.代码 1 int triangleNumber(vector<int>& nums) { 2 int res =0; 3 if( nums.size() < 3) 4 return res; 5 6 for( int i = 0; i < nums.size() -2; i++){ 7 for( int j = i+1; j < nums.size()-1; j++){ 8 for( int k = j +1; k <

leetcode -day13 Valid Palindrome &amp; Triangle &amp; Pascal&#39;s Triangle I II

1.  Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note:

LeetCode --- 65. Valid Number

题目链接:Valid Number Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statemen

LeetCode:Valid Number - 判断字符串中内容是否为数字

1.题目名称 Valid Number(判断字符串中内容是否为数字) 2.题目地址 https://leetcode.com/problems/valid-number/ 3.题目内容 英文:Validate if a given string is numeric. 中文:给出一个字符串,检查这个字符串中内容是否是一个数字 例如:"0"." 0.1"."2e10"是数字,"abc"."1 a"不是数字 4

LeetCode:Valid Parentheses - 合理的括号搭配

1.题目名称 Valid Parentheses(合理的括号搭配) 2.题目地址 https://leetcode.com/problems/valid-parentheses/ 3.题目内容 英文:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the