题解 | #牛的体重统计#
牛的体重统计
https://www.nowcoder.com/practice/15276ab238c9418d852054673379e7bf
考点知识点:考察合并列表,计算列表的数值的数量,通过数量获取最大值;注意数量相同取最大值;
题目解析:
1、调用python函数,排序去重,获取列表的唯一值
2、利用为唯一值计算原始列表各值的数量
3、遍历数量列表,获取最大数量值,且是最大的数值返回
采用的编程语言:Python
完整的编码代码:如下所示
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param weightsA int整型一维数组 # @param weightsB int整型一维数组 # @return int整型 # class Solution: def findMode(self, weightsA: List[int], weightsB: List[int]) -> int: Weights =(weightsA + weightsB) Weight=list(set(Weights)) Weight.sort() a=[] for i in Weight: a.append(Weights.count(i)) for i in range(len(a)-1,-1,-1): if max(a)==a[i]: return Weight[i]
面试高频TOP202解析 文章被收录于专栏
采用Java,C,Python等方法去解答面试高频TOP202题目,