169. Majority Element

  1. /**
  2. * @param {number[]} nums
  3. * @return {number}
  4. */
  5. var majorityElement = function(nums) {
  6. var hash = {};
  7. var y=-1,z;
  8. //注意这里的方括号,利用变量访问对象属性时要用方括号
  9. for(var i=0;i<=nums.length-1;i++){
  10. if(hash[nums[i]]){
  11. hash[nums[i]]++;
  12. }else{
  13. hash[nums[i]]=1;
  14. }
  15. }
  16. for(var x in hash){
  17. if(y<hash[x]){
  18. y=hash[x]
  19. z=x;
  20. }
  21. }
  22. return Number(z);
  23. };

利用了从上一题那里学到的哈希表。


217. Contains Duplicate

  1. /**
  2. * @param {number[]} nums
  3. * @return {boolean}
  4. */
  5. var containsDuplicate = function(nums) {
  6. // if(nums==[]){
  7. // return false;
  8. // }
  9. var vain = [];
  10. for(var i=0;i<nums.length;i++){
  11. if(vain.indexOf(nums[i])==-1){
  12. vain.push(nums[i]);
  13. }
  14. }
  15. if(vain.join("")==nums.join("")){
  16. return false;
  17. }
  18. return true;
  19. };

睡前再刷一题,这题我用了数组去重。。反正效率很低,大约超过4%的人。。但是这种情况下依旧要注意当数组a和数组b作比较时,即使a=[1],b=[1],a==b的布尔运算结果却是false!


350. Intersection of Two Arrays II

  1. /**
  2. * @param {number[]} nums1
  3. * @param {number[]} nums2
  4. * @return {number[]}
  5. */
  6. var intersect = function(nums1, nums2) {
  7. var small = nums1.length<=nums2.length?nums1:nums2;
  8. var big = nums1.length<=nums2.length?nums2:nums1;
  9. var newarr = [];
  10. var i, dict = {};
  11. for(i = 0; i < big.length; i++){
  12. if(!dict[big[i]]){
  13. dict[big[i]] = 1;
  14. }else{
  15. dict[big[i]]++;
  16. }
  17. }
  18.  
  19. for(i = 0; i < small.length; i++){
  20. if(!dict[small[i]] || dict[small[i]] === 0){
  21.  
  22. }else{
  23. dict[small[i]]--;
  24. newarr.push(small[i]);
  25. }
  26. }
  27.  
  28. return newarr;
  29. };

这题又用到哈希表,效率很高,大约超过96%的人。

LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II的更多相关文章

  1. [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II

    这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...

  2. 26. leetcode 350. Intersection of Two Arrays II

    350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...

  3. 【leetcode❤python】169. Majority Element

    #Method 1import math class Solution(object):    def majorityElement(self, nums):        numsDic={}   ...

  4. [LeetCode&Python] Problem 169. Majority Element

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

  5. 【一天一道LeetCode】#350. Intersection of Two Arrays II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  6. [LeetCode] 350. Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  7. [LeetCode] 350. Intersection of Two Arrays II 两个数组相交II

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  8. LeetCode 350. Intersection of Two Arrays II (两个数组的相交之二)

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

  9. LeetCode 350. Intersection of Two Arrays II

    Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...

随机推荐

  1. 摄像头ov2685中关于sensor id 设置的相关的寄存器地址

    OV2685 : CHIP_ID address : 0x300A    default : 0x26 address : 0x300B    default : 0x85 address : 0x3 ...

  2. twisted高并发库transport函数处理数据包的些许问题

    还是在学校时间比较多, 能够把时间更多的花在学习上, 尽管工作对人的提升更大, 但是总是没什么时间学习, 而且工作的气氛总是很紧凑, 忙碌, 少了些许激情吧.适应就好了.延续着之前对twisted高并 ...

  3. 遍历输出图片加hover

    1. $(".icon a>div").hover(function () { var slls = $(this).attr("class"); sll ...

  4. linux上安装redis的踩坑过程2

    昨天在linux上安装redis后马上发现了其它问题,服务器很卡,cpu使用率上升,top命令查看下,原来有恶意程序在挖矿,此程序入侵了很多redis服务器,马上用kill杀掉它 然后开始一些安全策略 ...

  5. complex figure

    1/z   ----direct by MATLAB exp(z)    by QT logZ       by  QT 1/z      用QT画的 -----2018-03-17--------- ...

  6. php判断图片是否存在的几种方法

    在我们日常的开发中,经常需要用到判断图片是否存在,存在则显示,不存在则显示默认图片,那么我们用到的判断有哪些呢?今天我们就来看下几个常用的方法: 1.getimagesize()函数 getimage ...

  7. spring中配置quartz调用两次及项目日志log4j不能每天生成日志解决方法

    在quartz中配置了一个方法运行时会连续调用两次,是因为加载两次,只需在tomcat的server.xml中修改配置 <Host name="www.xx.cn" appB ...

  8. centos下网络的配置

    1.网络模式要进行使用NAT,要连网的话,要配置好设置:网络要进行一下共享到虚拟机 进入vi /etc/sysconfig/network-scripts/ifcfg-eth0   把里面的onboo ...

  9. 压力测试工具ab - Apache HTTP server benchmarking tool

    搞互联网开发,压力测试必不可少.压力测试的工具很多,我用过ab和JMeter,今天主要讲ab的用法. 1.ab是什么 ab is a tool for benchmarking your Apache ...

  10. android 开发常见问题

    指定版本 就OK了 路径: android/app/build.gradle compile ("com.facebook.react:react-native:填你自己的RN版本" ...