三角形のLeetCode 611有効数(有効トライアングル数)

611三角形の有効数
611有効なトライアングル数

タイトル説明
LeetCodeの
LeetCode

LeetCode 611有効なトライアングル数中等

Java実装

import java.util.Arrays;

class Solution {
    public int triangleNumber(int[] nums) {
        int count = 0, len = nums.length;
        Arrays.sort(nums);
        for (int i = len - 1; i > 1; i--) {
            int left = 0, right = i - 1;
            while (left < right) {
                if (nums[left] + nums[right] > nums[i]) {
                    count += right - left;
                    right--;
                } else {
                    left++;
                }
            }
        }
        return count;
    }
}

類似のトピック

参考資料

おすすめ

転載: www.cnblogs.com/hglibin/p/10991063.html