zoukankan      html  css  js  c++  java
  • Leetcode 之 Valid Triangle Number

    611. Valid Triangle Number

    1.Problem

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

    2.Solution

       题目的大意是给定一个元素可能存在重复的一维数组,给出其中能组成合法三角形的组合数。

    3.Code

    //Solution1
    //时间复杂度O(N^3),空间复杂度O(logN)(排序导致)
    //先对nums数组进行排序,如从大小三条边为 a,b,c,只需判断 a + b > c 成立与否 =》三条边能否构成三角形
    class
    Solution { public int triangleNumber(int[] nums) { Arrays.sort(nums); int l = nums.length; int count = 0; for ( int i = 0 ; i < l - 2 ; i++ ) { for ( int j = i + 1 ; j < l - 1 ; j++ ) { for ( int k = j + 1 ; k < l ; k++ ) { if ( nums[i] + nums[j] > nums[k]) { count++; } } } } return count; } }
    //Solution2 二分查找
    //时间复杂度O(N^2 * log(N)),空间复杂度O(log N)
    class
    Solution { public int triangleNumber(int[] nums) { Arrays.sort(nums); int l = nums.length; int count = 0; for ( int i = 0 ; i < l - 2 ; i++ ) { int k = i + 2; for ( int j = i + 1 ; j < l - 1 && nums[i] != 0 ; j++ ) { k = binarySearch(k,l - 1,nums,nums[i] + nums[j]); count += k - j - 1; } } return count; } public int binarySearch( int start , int end , int[] nums , int target ) { while ( start <= end ) { int mid = ( end - start ) / 2 + start; if ( nums[mid] < target ) { start = mid + 1; } else { end = mid - 1; } } return start; } }
    //Solution3,时间复杂度O(N^2) ,空间复杂度O(lgN)
    • ime complexity : O(n2)O(n^2)O(n2​​). Loop of kkk and jjj will be executed O(n2)O(n^2)O(n2​​) times in total, because, we do not reinitialize the value of kkk for a new value of jjj chosen(for the same iii). Thus the complexity will be O(n^2+n^2)=O(n^2).

    • Space complexity : O(logn)O(logn)O(logn). Sorting takes O(logn) space.

    class Solution {
        public int triangleNumber(int[] nums) {
            Arrays.sort(nums);
            int l = nums.length;
            int count = 0;
            for ( int i = 0 ; i < l - 2 ; i++ ) {
                int k = i + 2;
                for ( int j = i + 1 ; j < l - 1 && nums[i] != 0 ; j++ ) {
                    while ( k < l && (nums[i] + nums[j] > nums[k]) ) {
                        k++;
                    }
                    count += k - j - 1;   
                }
            }
            return count;
        }
    }
  • 相关阅读:
    JS this的指向
    JS 中类型鉴别
    eclipse 常用快捷键 及 windows快捷键
    服务器 设置 将 Tomcat 注册 到系统服务 及使用方法
    阿里云服务器 Windows连接不成功 提示“你的凭证不工作” 解决方法
    JavaWeb 后端 <八> 之 JDBC基礎(全)
    Mysql
    JavaEE EL & JSTL 学习笔记
    关于 Eclipse中的Web项目 部署的文件位置 查看jsp源码的部署位置
    JavaEE JSP 学习笔记
  • 原文地址:https://www.cnblogs.com/fangpengchengbupter/p/7411747.html
Copyright © 2011-2022 走看看