zoukankan      html  css  js  c++  java
  • leetcode 315: 计算右侧小于当前元素的个数

    package com.example.lettcode.dailyexercises;
    
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    
    /**
     * @Class CountSmaller
     * @Description 315 计算右侧小于当前元素的个数
     * 给定一个整数数组 nums,按要求返回一个新数组 counts。
     * 数组 counts 有该性质: counts[i] 的值是 nums[i] 右侧小于 num
     * s[i] 的元素的数量。
     * 示例:
     * 输入: [5,2,6,1]
     * 输出: [2,1,1,0]
     * 解释:
     * 5 的右侧有 2 个更小的元素 (2 和 1).
     * 2 的右侧仅有 1 个更小的元素 (1).
     * 6 的右侧有 1 个更小的元素 (1).
     * 1 的右侧有 0 个更小的元素.
     * @Author
     * @Date 2020/7/11
     **/
    public class CountSmaller {
        /**
         * 解法1: 逐个元素统计在其右侧并小于该元素的个数,时间复杂度O(n^2)超时
         */
     /*   public static List<Integer> countSmaller(int[] nums) {
            if (nums == null || nums.length == 0) return new ArrayList<>();
    
            List<Integer> integerList = new ArrayList<>();
            int len = nums.length;
            for (int i = 0; i < len; i++) {
                int count = 0;
                for (int j = i + 1; j < len; j++) {
                    if (nums[j] < nums[i]) count++;
                }
                integerList.add(count);
            }
            return integerList;
        }*/
    
        /**
         * 解法2:利用排序
         * 从右往左进行插入排序, 根据插入的位置计算右边小于该元素的个数
         */
        public static List<Integer> countSmaller(int[] nums) {
            if (nums == null || nums.length == 0) return new ArrayList<>();
            List<Integer> integerList = new LinkedList<>();
    
            int len = nums.length;
            // 反向插入排序
            for (int i = len - 2; i >= 0; i--) {
                int j = i + 1;
                int temp = nums[i];
                // 因为计算的是右边小于该元素的个数,所以插入后的数据是非递增的
                // 如果是计算右边大于该元素的数,插入后的数据这应该是非递减的
                while (j < len && nums[j] >= temp) {
                    nums[j - 1] = nums[j];
                    j++;
                }
                // j位置的元素是小于temp的,待插入位置是j-1;
                nums[j - 1] = temp;
                // len-j表示计数
                // addFirst 是因为i是逆序的,需要再逆序回原来的顺序
                ((LinkedList<Integer>) integerList).addFirst(len - j);
            }
            // 需要添加最后一个元素
            integerList.add(0);
    
            return integerList;
        }
    
        public static void main(String[] args) {
            int[] nums = new int[]{5, 2, 6, 1};
            List<Integer> integerList = new ArrayList<>();
            integerList = countSmaller(nums);
            System.out.println("CountSmaller demo01 result:");
            for (Integer integer : integerList) {
                System.out.print("," + integer);
            }
            System.out.println("");
        }
    }
    
  • 相关阅读:
    centos python sh recode command not find的解决办法
    Webmail邮件攻防实战技术总结[转]
    关于c#字符串三种逆转方法及性能比较的另外看法
    随机生成10个含有1万个qq邮箱的文件(Python)
    C#对IE使用Proxy(代理)
    sohu邮箱的联系人获取
    Python 的二进制文件读写需要注意的地方
    hotmail解码遇到的&#解码问题
    利用cookie收取Hotmail信件
    CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\web\4b49f661\23a749fc\App_Web_default.aspx.cdcab7d2.zii77dc.dll
  • 原文地址:https://www.cnblogs.com/fyusac/p/13282979.html
Copyright © 2011-2022 走看看