为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode611. 有效三角形的个数 | Valid Triangle Number

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10470002.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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

给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数。

示例 1:

输入: [2,2,3,4]
输出: 3
解释:
有效的组合是: 
2,3,4 (使用第一个 2)
2,3,4 (使用第二个 2)
2,2,3

注意:

  1. 数组长度不超过1000。
  2. 数组里整数的范围为 [0, 1000]。

Runtime: 36 ms
Memory Usage: 19.4 MB
 1 class Solution {
 2     func triangleNumber(_ nums: [Int]) -> Int {
 3         var nums = nums
 4         var res:Int = 0
 5         var n:Int  = nums.count
 6         nums.sort()
 7         for i in stride(from:n - 1,through:2,by:-1)
 8         {
 9             var left:Int = 0
10             var right:Int = i - 1
11             while (left < right)
12             {
13                 if nums[left] + nums[right] > nums[i]
14                 {
15                     res += (right - left)
16                     right -= 1
17                 }
18                 else
19                 {
20                     left += 1
21                 }
22             }
23         }
24         return res
25     }
26 }

76ms

 1 class Solution {
 2     func triangleNumber(_ nums: [Int]) -> Int {
 3         var result = 0
 4         guard nums.count >= 3 else {
 5             return 0
 6         }
 7         var nums = nums.sorted()
 8         for i in 0...nums.count - 2 {
 9             let longest = nums[i]
10             var fast = i + 2
11             var slow = i + 1
12             while fast < nums.count {
13                 while slow < fast && nums[i] + nums[slow] <= nums[fast] {
14                     slow += 1
15                 }
16                 result += fast - slow
17                 fast += 1
18             }
19         }
20         return result
21     }
22 }

124ms

 1 class Solution {
 2     func triangleNumber(_ nums: [Int]) -> Int {
 3         guard nums.count >= 3 else { return 0 }
 4         let sortedNums = nums.sorted {
 5             return $0 < $1
 6         }
 7         var count = 0
 8         let last = sortedNums.count - 1
 9         print(sortedNums)
10         for i in 0..<last {
11             if sortedNums[i] == 0 {
12                 continue
13             }
14             var k = i + 1
15             for j in i+1...last {
16                 if sortedNums[j] == 0 {
17                     continue
18                 }
19                 let sum = sortedNums[i] + sortedNums[j]
20                 while k < sortedNums.count && sortedNums[k] < sum {
21                     k += 1
22                 }
23                 count += (k - j - 1)
24             }
25         }
26         return count
27     }
28 }

128ms

 1 class Solution {
 2     func triangleNumber(_ nums: [Int]) -> Int {
 3         let n = nums.sorted()
 4         var ans = 0
 5         for i in 0..<n.count {
 6             var k = i + 1
 7             for j in i + 1..<n.count {
 8                 while k + 1 < n.count && n[k + 1] < n[i] + n[j] {
 9                     k += 1
10                 }
11                 if n[k] < n[i] + n[j] {
12                     ans += max(0,k - j)
13                 }
14             }
15         }
16         return ans
17     }
18 }

232ms

 1 class Solution {
 2     // return the last index of a biggest number <= target
 3     func binarySearch(_ nums: [Int], _ target: Int, _ start: Int, _ end: Int) -> Int {
 4         var i = start, j = end
 5         while i < j {
 6             let m = (i + j) / 2
 7             if nums[m] > target { j = m }
 8             else { i = m + 1 }
 9         }
10         return i-1
11     }
12     
13    
14     func triangleNumber(_ nums: [Int]) -> Int {
15         guard nums.count > 2 else { return 0 }
16         let nums = nums.sorted()
17         var res = 0
18         for i in 0..<(nums.count - 2) {
19             for j in i+1..<(nums.count - 1) {
20                 res += binarySearch(nums, nums[i] + nums[j] - 1, j+1, nums.count) - j
21             }
22         }
23         return res
24     }
25 }

248ms

 1 class Solution {
 2     func triangleNumber(_ nums: [Int]) -> Int {
 3         if nums.count < 3 { return 0 }
 4         var res = 0
 5         let nums = nums.sorted()
 6         for i in 1..<nums.count - 1 {
 7             var k = nums.count - 1
 8             for j in (0..<i).reversed() {
 9                 let s = nums[j] + nums[i]
10                 while k > i && nums[k] >= s { k -= 1 }
11                 res += k - i
12             }
13         }
14         return res
15     }
16 }

1180ms、18948kb

 1 class Solution {
 2     func triangleNumber(_ nums: [Int]) -> Int {
 3     if nums.count < 3 {
 4       return 0
 5     }
 6     var count = 0
 7     let nums_sorted = nums.sorted()
 8     let maxNum = nums_sorted.last!
 9     for i in 0..<nums_sorted.count {
10      
11       for j in i+1..<nums_sorted.count {
12         let side1 = nums_sorted[i]
13         let side2 = nums_sorted[j]
14         var numOfValidThridNums = 0
15         for k in j+1..<nums_sorted.count {    
16           if nums_sorted[k] < side1 + side2 {
17             numOfValidThridNums = numOfValidThridNums + 1
18           } else {
19             break;
20           }
21         }
22         count = count + numOfValidThridNums
23       }
24     }
25     return count
26   }
27 }

1584ms 

 1 class Solution {
 2     func triangleNumber(_ nums: [Int]) -> Int {
 3         
 4         var n = 0
 5         let nums = nums.sorted()
 6         for i in 0..<nums.count {
 7             for j in i + 1..<nums.count {
 8                 for k in j + 1..<nums.count {
 9                     if nums[k] < nums[i] + nums[j] {
10                         n += 1
11                     } else {
12                         break
13                     }
14                 }
15             }
16         }
17         return n
18     }
19 }

2332ms

 1 class Solution {
 2     func triangleNumber(_ nums: [Int]) -> Int {
 3         let ct = nums.count
 4         if ct < 3 {
 5             return 0
 6         }
 7         var nums = nums.sorted { $0 > $1 }
 8         var ans = 0
 9         for i in 0..<ct-2 {
10             for j in (i+1)..<ct-1 {
11                 for k in (j+1)..<ct {
12                     if vailidTriangle(nums[i], nums[j], nums[k]) {
13                         ans += 1
14                     }else{
15                         break
16                     }
17                 }
18             }
19         }
20         return ans
21     }
22 
23     func vailidTriangle(_ a: Int, _ b: Int, _ c: Int) -> Bool {
24         return (b+c>a)
25     }
26 }

 

posted @ 2019-03-04 12:38  为敢技术  阅读(308)  评论(0编辑  收藏  举报