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] ...
随机推荐
- DH密钥交换非对称加密
迪菲-赫尔曼密钥交换(Diffie–Hellman key exchange,简称"D–H") 是一种安全协议. 它可以让双方在完全没有对方任何预先信息的条件下通过不安全信道建立起 ...
- ListView 与ContextMenu的关联管理
<span style="font-family: Arial, Helvetica, sans-serif;">package com.example.listvie ...
- LeetCode之旅(15)-Odd Even Linked List
题目描述: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...
- Hibernate与Mybatis的比较
Hibernate与Mybatis的比较: Hibernate: 标准的.重量级.全自动化的ORM框架 可以写sql(SQLQuery,sql )也可以不写sql(Query,hql) ORM映射主要 ...
- XSS攻击过滤处理
关于XSS攻击 XSS是一种经常出现在web应用中的计算机安全漏洞,它允许恶意web用户将代码植入到提供给其它用户使用的页面中. XSS漏洞的危害 网络钓鱼,包括盗取各类用户账号: 窃取用户cooki ...
- python笔记--1
pip工具常用命令: pip命令示例 说明 pip download SomePackage[==version] 下载扩展库的指定版本,不安装 pip freeze [> requiremen ...
- css选择器应用
.mynav li:not(:last-child) { margin-right: 20px; }
- python---购物车
购物车功能如下: 1. 输入收入多少,购买商品 2. 打印购物清单,根据清单选择商品: 3. 结算,打印购物清单及总金额 # -*- coding:utf-8 -*- # LC goods=[[1,' ...
- currval of sequence "follow_id_seq" is not yet defined in this session
postgresql上使用 select currval('follow_id_seq'); 报错: currval of sequence "follow_id_seq" is ...
- Ocelot中文文档-负载均衡
Ocelot能通过可用的下游服务对每个ReRoute进行负载平衡. 这意味着您可以扩展您的下游服务,并且Ocelot可以有效地使用它们. 可用的负载均衡器的类型是: LeastConnection - ...