剑指offer
数组中出现次数超过一半的数字
相似的企业真题
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 64M,其他语言128M
热度指数:680659
本题知识点:
哈希
数组
算法知识视频讲解
校招时部分企业笔试将禁止编程题跳出页面,为提前适应,练习时请使用在线自测,而非本地IDE。
题目描述
数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。如果不存在则输出0。
示例1
输入
复制
[1,2,3,2,2,2,5,4,2]
[1,2,3,2,2,2,5,4,2]
返回值
复制
2
2
上一题
下一题
登录
/
注册
我的提交
编辑器加载中...
public class Solution { public int MoreThanHalfNum_Solution(int [] array) { } }
class Solution { public: int MoreThanHalfNum_Solution(vector
numbers) { } };
# -*- coding:utf-8 -*- class Solution: def MoreThanHalfNum_Solution(self, numbers): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param numbers int整型一维数组 * @return int整型 */ public int MoreThanHalfNum_Solution (List
numbers) { // write code here } }
function MoreThanHalfNum_Solution(numbers) { // write code here } module.exports = { MoreThanHalfNum_Solution : MoreThanHalfNum_Solution };
function MoreThanHalfNum_Solution(numbers) { // write code here }
# -*- coding:utf-8 -*- class Solution: def MoreThanHalfNum_Solution(self, numbers): # write code here
package main /** * * @param numbers int整型一维数组 * @return int整型 */ func MoreThanHalfNum_Solution( numbers []int ) int { // write code here }
/** * * @param numbers int整型一维数组 * @param numbersLen int numbers数组长度 * @return int整型 */ int MoreThanHalfNum_Solution(int* numbers, int numbersLen ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # @param numbers int整型一维数组 # @return int整型 # class Solution def MoreThanHalfNum_Solution(numbers) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param numbers int整型一维数组 * @return int整型 */ def MoreThanHalfNum_Solution(numbers: Array[Int]): Int = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param numbers int整型一维数组 * @return int整型 */ fun MoreThanHalfNum_Solution(numbers: IntArray): Int { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param numbers int整型一维数组 * @return int整型 */ public int MoreThanHalfNum_Solution (int[] numbers) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param numbers int整型一维数组 * @return int整型 */ export function MoreThanHalfNum_Solution(numbers: number[]): number { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param numbers int整型一维数组 * @return int整型 */ func MoreThanHalfNum_Solution ( _ numbers: [Int]) -> Int { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param numbers int整型一维数组 * @return int整型 */ pub fn MoreThanHalfNum_Solution(&self, numbers: Vec
) -> i32 { // write code here } }
[1,2,3,2,2,2,5,4,2]
2