一、力扣01两数之和
<script type="text/javascript">
var twoSum = function(nums, target) {
let map = new Map();
for( let i=0;i<nums.length;i++){
num = target - nums[i];
if( map.has(num) ){
return [map.get(num),i];
}
map.set(nums[i],i);
}
};
</script>
二、力扣217存在重复元素
<script type="text/javascript">
var containsDuplicate = function(nums) {
//视频教程:https://www.xuexiluxian.cn/course/detail/8429c57c800544f6b49fe73f9aae21de
// let map = new Map();
// for(const x of nums ){
// if( map.has(x) ){
// return true;
// }
// map.set(x,1);
// }
// return false;
let set = new Set();
for( const x of nums ){
if( set.has(x) ){
return true;
}
set.add(x);
}
return false;
};
</script>
三、力扣349两个数组的交集
<script type="text/javascript">
//视频教程:https://www.xuexiluxian.cn/course/detail/8429c57c800544f6b49fe73f9aae21de
var intersection = function(nums1, nums2) {
let set = new Set(nums2);
return [...new Set(nums1)].filter((val)=>set.has(val));
};
</script>
四、力扣1207独一无二的出现次数
<script type="text/javascript">
function fun( s ){
let maxNum = 0;
let maxStr = '';
let map = new Map();
for( let item of s ){
map.set( item , (map.get(item) || 0 ) + 1 )
}
for(let [key,value] of map){
if( value > maxNum ){
maxStr = key;
maxNum = value;
}
}
return [maxStr , maxNum];
}
console.log( fun('aaabbbbccccccc') );
</script>
五、力扣03无重复字符的最长子串
<script type="text/javascript">
var lengthOfLongestSubstring = function(s) {
const map = new Map();
let l = 0;
let num = 0;
for( let i =0;i<s.length;i++){
if( map.has(s[i]) && map.get(s[i]) >= l ){
l = map.get(s[i]) + 1;
}
num = Math.max( num , i+1-l );
map.set(s[i],i);
}
return num;
};
</script>
|