169. Majority Element

/**
* @param {number[]} nums
* @return {number}
*/
var majorityElement = function(nums) {
var hash = {};
var y=-1,z;
//注意这里的方括号,利用变量访问对象属性时要用方括号
for(var i=0;i<=nums.length-1;i++){
if(hash[nums[i]]){
hash[nums[i]]++;
}else{
hash[nums[i]]=1;
}
}
for(var x in hash){
if(y<hash[x]){
y=hash[x]
z=x;
}
}
return Number(z);
};

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


217. Contains Duplicate

/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function(nums) {
// if(nums==[]){
// return false;
// }
var vain = [];
for(var i=0;i<nums.length;i++){
if(vain.indexOf(nums[i])==-1){
vain.push(nums[i]);
}
}
if(vain.join("")==nums.join("")){
return false;
}
return true;
};

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


350. Intersection of Two Arrays II

/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[]}
*/
var intersect = function(nums1, nums2) {
var small = nums1.length<=nums2.length?nums1:nums2;
var big = nums1.length<=nums2.length?nums2:nums1;
var newarr = [];
var i, dict = {};
for(i = 0; i < big.length; i++){
if(!dict[big[i]]){
dict[big[i]] = 1;
}else{
dict[big[i]]++;
}
} for(i = 0; i < small.length; i++){
if(!dict[small[i]] || dict[small[i]] === 0){ }else{
dict[small[i]]--;
newarr.push(small[i]);
}
} return newarr;
};

这题又用到哈希表,效率很高,大约超过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. Android高效率编码-细节,控件,架包,功能,工具,开源汇总,你想要的这里都有

    Android高效率编码-细节,控件,架包,功能,工具,开源汇总 其实写博客的初衷也并不是说什么分享技术,毕竟咱还只是个小程序员,最大的目的就是对自我的知识积累,以后万一编码的时候断片了,也可以翻出来 ...

  2. PS 图像特效-非线性滤波器

    利用非线性滤波器,使图像的色彩凝块,形成一种近似融化的特效. clc; clear all; addpath('E:\PhotoShop Algortihm\Image Processing\PS A ...

  3. HDFS APPEND性能测试

    hbase在写入数据之前会先写hlog,hlog目前是sequencefile格式,采用append的方式往里追加数据.之前团队的同学测试关闭hlog会一定程序上提升写hbase的稳定性.而在我之前的 ...

  4. C# 压缩PDF图片

    文档中包含图片的话,会使得整个文档比较大,占用存储空间且不利于快速.高效的传输文件.针对一些包含大量高质图片的PDF文档,若是对图片进行压缩,可以有效减少文档的占用空间.并且,在文档传输过程中也可以减 ...

  5. Table对象代表一个HTML表格,在文档中<table>标签每出现一次,一个table对象就会被创建。

    1.对象集合 cells[] 返回包含表格中所有单元格的一个数组 rows[] 返回包含表格中所有行的一个数组 tBodies[] 返回包含表格中所有tbody的一个数组(主包含ty和td) 2.对象 ...

  6. java多线程的理解

    java多线程的理解   线程的5种状态:新建,就绪,运行,阻塞,死亡. Thread.sleep:线程  运行状态 转为  阻塞状态,(其它线程启动运行) Thread.yield:   线程 运行 ...

  7. web3.js

      安装 别按照官网上面 npm install web3 下载,我已经吃过一次亏了 npm initnpm install ethereum/web3.js --save   web3.isConn ...

  8. 从has no method 'tmpl'谈起

    最近做一个相对比较功能专业化的应用系统,其中今天Leader提出的功能修改需求有点smart table的意思,其中有个界面修改由于用Dom操作太麻烦了,于是想用用很久之前在学习jQuery API中 ...

  9. CDN公共资源

    SAE: http://lib.sinaapp.com/ Google: https://developers.google.com/speed/libraries/devguide?hl=zh-CN ...

  10. 【js-xlsx和file-saver插件】前端html的table导出数据到excel的表格合并显示boder

    最近在做项目,需要从页面的表格中导出excel,一般导出excel有两种方法:一.习惯上是建模版从后台服务程序中导出:二.根据页面table中导出:综合考虑其中利弊选择二.根据页面table中导出ex ...