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. 机器学习算法与Python实践之(五)k均值聚类(k-means)

    机器学习算法与Python实践这个系列主要是参考<机器学习实战>这本书.因为自己想学习Python,然后也想对一些机器学习算法加深下了解,所以就想通过Python来实现几个比较常用的机器学 ...

  2. 关于masm中PTR伪指令的一点思考

    在masm中,PTR伪指令只能修饰内存变量类型,因为任何寄存器的大小都是已知的且不能改变的(如果PTR修饰的是寄存器,则它修饰的是寄存器本身而不是其指向的内容)所以不能用PTR改变寄存器的大小,例如: ...

  3. 杭电ACM 1001题

    import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=ne ...

  4. 知物由学|游戏开发者如何从容应对Unity手游风险?

    本文由  网易云发布. "知物由学"是网易云易盾打造的一个品牌栏目,词语出自汉·王充<论衡·实知>.人,能力有高下之分,学习才知道事物的道理,而后才有智慧,不去求问就不 ...

  5. Core Animation简介

    一.Core Animation简介 * Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代 ...

  6. TCP TIME WAIT

     根据TCP协议定义的3次握手断开连接规定,发起socket主动关闭的一方 socket将进入TIME_WAIT状态,TIME_WAIT状态将持续2个MSL(Max Segment Lifetime) ...

  7. 小议 HashMap

    大家都知道,在Java里对对象的操作是基于引用的.而当我们需要对一组对象操作的时候,就需要有接收这一组引用的容器.平时我们最常用的就是数组.在Java里可以定义一个对象数组来完成许多操作.可是,数组长 ...

  8. JAVA 创建对象4种方法

    java创建对象的几种方式 博客分类: java   (1) 用new语句创建对象,这是最常见的创建对象的方法.(2) 运用反射手段,调用java.lang.Class或者java.lang.refl ...

  9. 使用 JMeter 进行压力测试

    一.前言 压力测试是每一个Web应用程序上线之前都需要做的一个测试,他可以帮助我们发现系统中的瓶颈问题,减少发布到生产环境后出问题的几率:预估系统的承载能力,使我们能根据其做出一些应对措施.所以压力测 ...

  10. JS(原生js和jq方式)获取元素属性(自定义属性),删除属性(自定义属性)

    JS(原生js和jq方式)获取元素属性(自定义属性),删除属性(自定义属性) 以下内容: 一.获取元素的属性 二.设置元素的属性 三.删除元素的属性 一.获取元素的属性 1-原生JS 获取属性 .ge ...