今日学习的文章链接和视频链接
242.有效的字母异位词
var isAnagram = function(s, t) {
if(s.length !== t.length) return false
let map = new Map();
for(let char of s){
if(!map.get(char)){
map.set(char,1)
}else{
map.set(char,map.get(char)+1)
}
};
for(let char of t){
if(!map.get(char)){
return false;
}else{
map.set(char,map.get(char)-1)
}
}
return true
};
- 两个数组的交集
var intersection = function(nums1, nums2) {
let set = new Set(nums1);
let arr = []
for(let i = 0 ;i <nums2.length;i++){
if(set.has(nums2[i])){
arr.push(nums2[i])
}
}
return [...new Set(arr)]
};
● 202. 快乐数
var isHappy = function(n) {
let myset = new Map();
n = String(n)
myset.set(n,n)
while(true){
let sum =0;
n = String(n)
for(let i = 0 ;i<n.length;i++){
sum += n[i] * n[i]
}
n = sum
console.log(myset,n)
if(myset.has(n)){
return false
}else{
myset.set(n,n)
if(n=='1'){
return true
}
}
}
// return 4
};
● 1. 两数之和
var twoSum = function(nums, target) {
let map = new Map();
for(let i = 0 ; i< nums.length ; i++){
// console.log(map.get(target-nums[i]))
if(map.get(target-nums[i])>=0){
return [i,map.get(target-nums[i])]
}
map.set(nums[i],i)
};
};
今日收获,记录一下自己的学习时长
标签:map,202,get,nums,随想录,char,set,let,两数 From: https://www.cnblogs.com/lijiacheng/p/17840447.html2h