首页 > 编程语言 >#yyds干货盘点# LeetCode程序员面试金典:多数元素

#yyds干货盘点# LeetCode程序员面试金典:多数元素

时间:2023-05-25 22:34:40浏览次数:70  
标签:yyds nums int 金典 num counts Map LeetCode majorityEntry

1.描述:

给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。

你可以假设数组是非空的,并且给定的数组总是存在多数元素。

 

示例 1:

输入:nums = [3,2,3]

输出:3

示例 2:

输入:nums = [2,2,1,1,1,2,2]

输出:2

2.代码实现:

class Solution {
    private Map<Integer, Integer> countNums(int[] nums) {
        Map<Integer, Integer> counts = new HashMap<Integer, Integer>();
        for (int num : nums) {
            if (!counts.containsKey(num)) {
                counts.put(num, 1);
            } else {
                counts.put(num, counts.get(num) + 1);
            }
        }
        return counts;
    }

    public int majorityElement(int[] nums) {
        Map<Integer, Integer> counts = countNums(nums);

        Map.Entry<Integer, Integer> majorityEntry = null;
        for (Map.Entry<Integer, Integer> entry : counts.entrySet()) {
            if (majorityEntry == null || entry.getValue() > majorityEntry.getValue()) {
                majorityEntry = entry;
            }
        }

        return majorityEntry.getKey();
    }
}

标签:yyds,nums,int,金典,num,counts,Map,LeetCode,majorityEntry
From: https://blog.51cto.com/u_15488507/6351551

相关文章

  • Leetcode2585. 获得分数的方法数
    题解多重背包的模板f[i][j]表示前i种题目得分为j的方案数f[i][j]+=f[i-1][j-kw]再将空间优化为1维classSolution{publicintwaysToReachTarget(inttarget,int[][]types){intn=types.length,MOD=(int)1e9+7,INF=0x3f3f3f3f;int[......
  • 图解LeetCode——24. 两两交换链表中的节点
    一、题目给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。二、示例2.1>示例1:【输入】head=[1,2,3,4]<br>【输出】[2,1,4,3]2.2>示例2:【输入】head=[]<br>【输出】[]2.3>示例......
  • 路径总和 leetcode——递归+回溯
    题目leetcode:113代码与解析这道题可以看做leetcode112和leetcode257合体这道题要遍历整棵树,把所有符合条件的路径添加进去,所以不需要返回值递归三部曲:确定参数和返回值要传入当前节点,和总和,不需要返回值确定终止条件如果当前节点没有叶子结点,并且和等于target.那么添加进r......
  • #yyds干货盘点# LeetCode程序员面试金典:路径总和
    题目:给你二叉树的根节点 root和一个表示目标和的整数 targetSum。判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和 targetSum。如果存在,返回true;否则,返回false。叶子节点是指没有子节点的节点。 示例1:输入:root=[5,4,8,11,null,13,4,......
  • #yyds干货盘点# LeetCode程序员面试金典:Excel表列名称
    1.简述:给你一个整数 columnNumber,返回它在Excel表中相对应的列名称。例如:A->1B->2C->3...Z->26AA->27AB->28... 示例1:输入:columnNumber=1输出:"A"示例2:输入:columnNumber=28输出:"AB"示例3:输入:columnNumber=701输出:"ZY"示例4:输入:colum......
  • 每日一题 力扣 1377 https://leetcode.cn/problems/frog-position-after-t-seconds/
    力扣1377https://leetcode.cn/problems/frog-position-after-t-seconds/这道题目用dp去做,构建邻接矩阵,做的时候需要注意题目条件,如果青蛙跳不动了,这个概率就保持不变了一般跳青蛙,很容易想到dp核心代码如下publicdoublefrogPosition(ipublicdoublefrogPosition(intn,......
  • LeetCode 98. 验证二叉搜索树
    classSolution{public:vector<int>dfs(TreeNode*root)//依次返回是否是二叉搜索树,最大值最小值{vector<int>res{1,root->val,root->val};if(root->left){autol=dfs(root->left);res[1]=max(res......
  • LeetCode 222. 完全二叉树的节点个数
    classSolution{public:intcountNodes(TreeNode*root){if(!root)return0;autol=root->left,r=root->right;intx=1,y=1;//记录左右两边层数while(l)l=l->left,x++;while(r)r=r->right,y++;......
  • 二刷Leetcode-Days07
    动规:/***70.爬楼梯*@paramn*@return*/publicintclimbStairs(intn){if(n<=2){returnn;}int[]dp=newint[n];dp[0]=1;dp[1]=2;for(inti=2;i......
  • Linux 常用命令大全【yyds干货盘点1】
    1. 系统信息arch显示机器的处理器架构(1)uname-m显示机器的处理器架构(2)uname-r显示正在使用的内核版本dmidecode-q显示硬件系统部件-(SMBIOS/DMI)hdparm-i/dev/hda罗列一个磁盘的架构特性hdparm-tT/dev/sda在磁盘上执行测试性读取操作cat/proc/cp......