0611.cpp (460B)
1 class Solution { 2 public: 3 int triangleNumber(vector<int> &nums) const { 4 const int n = size(nums); 5 int res = 0; 6 7 sort(begin(nums), end(nums)); 8 for (int k = 2; k < n; k++) { 9 int i = 0, j = k - 1; 10 while (i < j) { 11 if (nums[i] + nums[j] <= nums[k]) 12 i++; 13 else 14 res += j - i, j--; 15 } 16 } 17 18 return res; 19 } 20 };