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] ...
随机推荐
- 摄像头ov2685中关于sensor id 设置的相关的寄存器地址
OV2685 : CHIP_ID address : 0x300A default : 0x26 address : 0x300B default : 0x85 address : 0x3 ...
- twisted高并发库transport函数处理数据包的些许问题
还是在学校时间比较多, 能够把时间更多的花在学习上, 尽管工作对人的提升更大, 但是总是没什么时间学习, 而且工作的气氛总是很紧凑, 忙碌, 少了些许激情吧.适应就好了.延续着之前对twisted高并 ...
- 遍历输出图片加hover
1. $(".icon a>div").hover(function () { var slls = $(this).attr("class"); sll ...
- linux上安装redis的踩坑过程2
昨天在linux上安装redis后马上发现了其它问题,服务器很卡,cpu使用率上升,top命令查看下,原来有恶意程序在挖矿,此程序入侵了很多redis服务器,马上用kill杀掉它 然后开始一些安全策略 ...
- complex figure
1/z ----direct by MATLAB exp(z) by QT logZ by QT 1/z 用QT画的 -----2018-03-17--------- ...
- php判断图片是否存在的几种方法
在我们日常的开发中,经常需要用到判断图片是否存在,存在则显示,不存在则显示默认图片,那么我们用到的判断有哪些呢?今天我们就来看下几个常用的方法: 1.getimagesize()函数 getimage ...
- spring中配置quartz调用两次及项目日志log4j不能每天生成日志解决方法
在quartz中配置了一个方法运行时会连续调用两次,是因为加载两次,只需在tomcat的server.xml中修改配置 <Host name="www.xx.cn" appB ...
- centos下网络的配置
1.网络模式要进行使用NAT,要连网的话,要配置好设置:网络要进行一下共享到虚拟机 进入vi /etc/sysconfig/network-scripts/ifcfg-eth0 把里面的onboo ...
- 压力测试工具ab - Apache HTTP server benchmarking tool
搞互联网开发,压力测试必不可少.压力测试的工具很多,我用过ab和JMeter,今天主要讲ab的用法. 1.ab是什么 ab is a tool for benchmarking your Apache ...
- android 开发常见问题
指定版本 就OK了 路径: android/app/build.gradle compile ("com.facebook.react:react-native:填你自己的RN版本" ...