LeetCode Javascript实现 169. Majority Element 217. Contains Duplicate(两个对象比较是否相等时,如果都指向同一个对象,a==b才是true)350. Intersection of Two Arrays II
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的更多相关文章
- [LeetCode] 349 Intersection of Two Arrays && 350 Intersection of Two Arrays II
这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/in ...
- 26. leetcode 350. Intersection of Two Arrays II
350. Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. ...
- 【leetcode❤python】169. Majority Element
#Method 1import math class Solution(object): def majorityElement(self, nums): numsDic={} ...
- [LeetCode&Python] Problem 169. Majority Element
Given an array of size n, find the majority element. The majority element is the element that appear ...
- 【一天一道LeetCode】#350. Intersection of Two Arrays II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- [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 ...
- [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 ...
- LeetCode 350. Intersection of Two Arrays II (两个数组的相交之二)
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
- LeetCode 350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1] ...
随机推荐
- LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...
- hbase thrift 访问队列
public class CallQueue implements BlockingQueue<Runnable> { private static Log LOG = LogFact ...
- SharePoint 部件通过EditorPart自定义属性面板
需求:编写一个新闻展示的WebPart,要求可以分类,类别是从WebService中获取的字符串,要求可以在属性中勾选分类,显示该分类的信息,分类可能会增加.我要做的就是动态生成属性中的新闻类别,至于 ...
- ubuntu14.04下安装rubinius测试原生线程
因为CRuby(MRI)本身不支持原生多线程,所以想试一下其他ruby解释器实现对原生多线程的支持.于是安装rubinius折腾一下:) 在rubinius官网下载2.4.1源代码,然后驾轻就熟首先b ...
- Linux的pwd命令详解
在Linux层次结构中,用户可以在被授权的任意目录下利用mkdir命令创建新目录,也可以利用cd命令从一个目录转换到另一个目录.然而,没有提示符来告知用户目前处于哪一个目录中.想要知道当前所处的目录, ...
- 常常搞不清楚SQLServer中的sp_columns来看一看
The sp_columns catalog stored procedure is equivalent to SQLColumns in ODBC. The results returned ar ...
- Spring消息之STOMP
一.STOMP 简介 直接使用WebSocket(或SockJS)就很类似于使用TCP套接字来编写Web应用.因为没有高层级的线路协议(wire protocol),因此就需要我们定义应用之间所发送消 ...
- linux常用命令合集(未完)
(1)新建 新建文件夹:mkdir 文件夹名 新建文件:touch 文件路径/文件名 删除: rm –f filename删除文件 rm –rf filename删除文件夹 (2)重命名 1将一个名为 ...
- python flask中的代码约定
在Python社区中有许多关于代码风格的约定.如果你写过一段时间Python了,那么也许对此已经有些了解. 我会简单介绍一下,同时给你一些URL链接,从中你可以找到关于这个话题的详细信息. 让我们提出 ...
- mysql安装与配置(以mysql-5.7.10-winx64为例)
一.在官网上下载相应的mysql安装包,本人下载的是:mysql-5.7.10-winx64 (Windows (x86, 64-bit), ZIP Archive) 附下载地址:http://dev ...