参考了别人的想法,思路简洁,效率高。虽然是在充分理解别人的思路后写出的代码,但是还是发现了自己的不足之处。以后还是要多思考多写。加油吧!

public class Solution {public int majorityElement(int[] num) {int ans = 0, cnt = 0;for(int i=0; i<num.length; i++) {if(cnt == 0) {ans = num[i];cnt ++;} else {if(ans == num[i]) {cnt ++;} else {cnt --;}}}return ans;}
}

转载于:https://www.cnblogs.com/wxisme/p/4376637.html

LeetCode - Majority Element相关推荐

  1. LeetCode Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  2. [LeetCode] Majority Element

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  3. [LeetCode]Majority Element

    题目描述: Given an array of size n, find the majority element. The majority element is the element that ...

  4. [Leetcode] Majority Element 众数

    Majority Element I Given an array of size n, find the majority element. The majority element is the ...

  5. [LeetCode] Majority Element II

    Majority Element II Given an integer array of size n, find all elements that appear more than ⌊ n/3 ...

  6. LeetCode Majority Element II(Moore Voting Algorithm即Majority Voting Algorithm)

     Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algo ...

  7. [leetcode]Majority Element II

    思路:可以使用三向切分的快速排序算法,因为这种算法会把小于某个数,等于某个数,和大于某个数的分开,分成三部分.很容易判断等于某个数的全部数量,对于这种重复值问题,很有效. public class S ...

  8. 【LeetCode】169. Majority Element

    原题链接:https://leetcode.com/problems/majority-element/description/ 要求: Given an array of size n, find ...

  9. [LeetCode] 169. Majority Element 多数元素

    Given an array of size n, find the majority element. The majority element is the element that appear ...

最新文章

  1. 阿里云POLARDB如何助力轻松筹打造5亿用户信赖的大病筹款平台?
  2. 2 获取对象 IDbDataAdapter 用于填充 DataSet 和更新数据源
  3. 安义县农业结构调整-农业大健康·林裕豪:从玉农业谋定基地
  4. xxxx is not found in PojoType<Order, fields = [amount: Integer, product: String, user: Long]>
  5. insert时调用本身字段_MySQL RC级别下并发insert锁超时问题 - 案例验证
  6. makefile 的export问题
  7. python 3.7下载安装scrapy_win10上python3.7安装scrapy1.5.1
  8. Android 系统(92)---诊断原生代码崩溃问题
  9. 安阳市2021年高考成绩查询,2021安阳市地区高考成绩排名查询,安阳市高考各高中成绩喜报榜单...
  10. AWS技术峰会免费报名 | 北上深三地可选,资深技术大牛分享实践干货
  11. React学习笔记 - 组件Props
  12. java删除指定文件后重新建立文件系统_java file 操作之创建、删除文件及文件夹...
  13. python乒乓球比赛规则介绍_乒乓球比赛的简要规则
  14. win10锁屏c语言,Win10秘笈:如何在锁屏打开任意应用程序?
  15. 全网首份pyQt6飞机大战
  16. 主成分分析(PCA)方法步骤以及代码详解
  17. 花开花落又是一度春秋,聚散离别见证一场青春。
  18. 12月份参加工作的年假怎么休_可是年假没有休,请问我可以12月份休吗,12月份就不用上班了,法律有规定吗会?影响我全年的出勤奖金。...
  19. CSS基本知识点总结
  20. python大数据培训班学费

热门文章

  1. 8位可控加减法电路设计_100以内数的认知也加减法详解
  2. 学籍管理系统 c++作业
  3. 114. 二叉树展开为链表 golang
  4. 软件工程学习笔记《目录》
  5. 软件工程---18.面向服务的软件工程
  6. Linux网络编程——tcp并发服务器(多进程)
  7. C++智能指针(二)模拟实现三种智能指针
  8. C语言实现多线程排序
  9. fcntl函数(网络编程会用)
  10. 你真的了解Java系统启动流程吗?mysql不包含多个字符