LeetCode 611. Valid Triangle Number

如果任意两边之和大于第三边,那么是个合法的三角形
所以联想到判断合法IP地址那道题,既然是固定选择3个数,那么可以Brute Force
排序之后将所有的情况判定出来

package array.No611;

import java.util.Arrays;

class Solution {
    public static int triangleNumber(int[] nums) {
        int cnt = 0;
        for(int a = 0; a < nums.length; a++){
            for(int b = a+1; b < nums.length; b++){
                for(int c = b+1; c < nums.length; c++){
                    if(isValid(nums[a],nums[b],nums[c]))
                        cnt++;
                }
            }
        }
        return cnt;
    }

    public static boolean isValid(int a, int b, int c){
        return (a + b > c && a + c > b && b + c > a);
    }

    public static void main(String[] args){
        int[] nums = {2,2,3,4};
        System.out.println(triangleNumber(nums));
    }
}

相关文章:

  • 2021-06-12
  • 2021-09-08
  • 2021-06-19
  • 2021-12-23
  • 2022-02-27
  • 2022-02-20
  • 2021-11-23
猜你喜欢
  • 2021-10-17
  • 2021-06-11
  • 2021-11-08
  • 2021-08-06
  • 2022-12-23
  • 2021-07-02
相关资源
相似解决方案