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. Linux打包命令 - tar

    上一篇文章谈到的命令大多仅能针对单一文件来进行压缩,虽然 gzip 与 bzip2 也能够针对目录来进行压缩, 不过,这两个命令对目录的压缩指的是『将目录内的所有文件 "分别" 进 ...

  2. Understanding the Objective-C Runtime

    Wednesday, January 20, 2010 Understanding the Objective-C Runtime The Objective-C Runtime is one of ...

  3. JVM学习--(二)内存模型、可见性、指令重排序

    我们将根据JVM的内存模型探索java当中变量的可见性以及不同的java指令在并发时可能发生的指令重排序的情况. 内存模型 首先我们思考一下一个java线程要向另外一个线程进行通信,应该怎么做,我们再 ...

  4. Spring Boot 2.0.1 入门教程

    简介 Spring Boot是Spring提供的一套基础配置环境,可以用来快速开发生产环境级别的产品.尤其适合开发微服务架构,省去了不少配置麻烦.比如用到Spring MVC时,只需把spring-b ...

  5. 使用jdk8 stream 统计单词数

    在我的SpringBoot2.0不容错过的新特性 WebFlux响应式编程里面,有同学问如何使用stream统计单词数.这是个好例子,也很典型,在这里补上. 下面的例子实现了从一个文本文件读取(英文) ...

  6. [ASP.NET MVC4高级编程] 学习记录(一)

    理论: 先有GUI在发展,当用户按下某个键,某个进程会监听到这个动作,这个进程就是控制器.这就是MVC模式. 后来有了事件驱动编程,响应动作的是按钮本身,而不是控制器. 再后来webForm中,事件驱 ...

  7. Day16 Django

    学Django之前,先看下http基础,老师的网页地址: web框架 - Yuan先生 - 博客园 http://www.cnblogs.com/yuanchenqi/articles/7690561 ...

  8. (转)go rabbitmq实践

    转载自:http://www.cnblogs.com/shi-meng/p/4800080.html 1:驱动 本来打算自己写一个驱动的,后来发现github上面已经有了,那我就直接拿现成的了, 驱动 ...

  9. Linux部署集群.NET网站

    一.Linux下面安装需要软件 我们这里需要安装的软件有: 1) Mono 3.2.8 : C#跨平台编译器,能使.Net运行与Linux下,目前.net 4.0可以完美运行在该平台下 2) ngin ...

  10. 大型三甲医院医疗体检信息管理系统源码 PEIS 体检科软件 CS

    详情请点击查看 开发环境 :VS2008 + C# + SQL2000 功能介绍: 1:设置:操作员设置   系统功能设置    用户组权限设置  公告打印设置  数据字典设置  临床类型设置  体检 ...