• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

int[]数组转Integer[]、List、Map「结合leetcode第414题 第三大的数、第169题 多数元素 」

武飞扬头像
进阶的小名
帮助1

学新通

1、int[ ] 转 Integer[ ]:

public static void main(String[] args) {
    int[] nums = {1, 2, 3};    
    Integer[] array = Arrays.stream(nums).boxed().toArray(Integer[]::new);    
    System.out.println(Arrays.toString(array));
}

输出:

// [1, 2, 3]

2、两道leetcode题遇到的场景:

众所周知,将普通数组转为List集合,可以通过JDK提供的诸多方法来减轻我们的编码负担,所以接下来小名借用两个leetcode题中的场景来分享下数组转集合的使用方法:

2.1、int[ ] 转 List<Integer> :

List<Integer> ints = Arrays.stream(nums)
.boxed()
.collect(Collectors.toList());

看到开头的「int[ ]转Integer[ ]」可能有的小伙伴并不知道什么情况会用。当然平日开发我们断然不会这样为难自己,一般入参和出参都会很自然的定义为数据类型为包装类的Integer的数组(Integer[])。

但是作为码农的小名刷题时,发现一个有意思的现象:当你想对一个int[ ]数组进行降序排序(注意这里不是给List降序排序)时,「Arrays.sort()」方法只为非包装类的int提供了 升序排序 ,并不提供对其 降序排序 的方法

题目地址: https://leetcode.cn/problems/third-maximum-number/

414. 第三大的数
给你一个非空数组,返回此数组中 第三大的数 。如果不存在,则返回数组中最大的数。
示例 1:
输入:[3, 2, 1]
输出:1
解释:第三大的数是 1 。

示例 2:
输入:[1, 2]
输出:2
解释:第三大的数不存在, 所以返回最大的数 2 。

示例 3:
输入:[2, 2, 3, 1]
输出:1
解释:注意,要求返回第三大的数,是指在所有不同数字中排第三大的数。
此例中存在两个值为 2 的数,它们都排第二。在所有不同数字中排第三大的数为 1 。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/third-maximum-number
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Coding:

        public int thirdMax(int[] nums) {
            // 将int[]降序排序、去重并转为List<Integer>
            List<Integer> ints = Arrays.stream(nums)
            .boxed()
            .distinct()
            .sorted(Collections.reverseOrder())
            .collect(Collectors.toList());
            // 获得最大的数字
            int num = ints.get(0);
            // 初始化计数器
            int count = 1;
            for (int i = 1; i < ints.size(); i  ) {
                // 数组长度小于3时,返回数组中第一个即最大的数字
                if (ints.size() < 3) {
                    return ints.get(0);
                }
                int intnum = ints.get(i);
                // 返回第三大的数字
                if (intnum < num && count <= 2) {
                    num = intnum;
                    count  ;
                }
            }
            return -1;
        }

2.2、int[ ] 转 Map:

Map<Integer,Long> map= Arrays.stream(nums)
.boxed()
.collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));

通过上面「int[ ]转List」 可能有使用过JDK8-lambda表达式 的小伙伴会想看下如果将int[ ]数组转为 Map 这种数据结构后的样子,毕竟Map有很多好用的场景,例如下面这道题的应用。

题目地址:https://leetcode.cn/problems/majority-element/

169. 多数元素
给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。
你可以假设数组是非空的,并且给定的数组总是存在多数元素。

示例 1:
输入:nums = [3,2,3]
输出:3

示例 2:
输入:nums = [2,2,1,1,1,2,2]
输出:2

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/majority-element
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Coding:

    public int majorityElement(int[] nums) {
        // 获取 n/2 的数组长度
        int size = nums.length>>1;
        // 将 数字-出现次数 以Map的Key-Value的结构分组
        Map<Integer,Long> map= Arrays.stream(nums)
        .boxed()
        .collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
        // 遍历数字,获取数字出现次数
        for (Map.Entry<Integer,Long> entry : map.entrySet()) {
            // 返回其中的多数元素
            if (entry.getValue() > size) {
                return entry.getKey();
            }
        }
        return -1;
    }

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgafeca
系列文章
更多 icon
同类精品
更多 icon
继续加载