---
# System prepended metadata

title: 0611. Valid Triangle Number
tags: [Leetcode, Two Pointers, Medium]

---

# 0611. Valid Triangle Number
###### tags: `Leetcode` `Medium` `Two Pointers`
Link: https://leetcode.com/problems/valid-triangle-number/
## 思路 $O(N^2)$ $O(1)$
## Code
```java=
class Solution {
    public int triangleNumber(int[] nums) {
        Arrays.sort(nums);
        int ans = 0;
        for(int i=2; i<nums.length; i++){
            int l=0, r=i-1;
            while(l<r){
                if(nums[l]+nums[r]>nums[i]){
                    ans+=r-l;
                    r--;
                }
                else l++;
            }
        }
        return ans;
    }
}
```