题目:给你两个字符串 word1 和 word2 。请你从 word1 开始,通过交替添加字母来合并字符串。如果一个字符串比另一个字符串长,就将多出来的字母追加到合并后字符串的末尾。返回 合并后的字符串 。
输入:word1 = "abc", word2 = "pqr" 输出:"apbqcr" 解释:字符串合并情况如下所示: word1: a b c word2: p q r 合并后: a p b q c r输入:word1 = "ab", word2 = "pqrs" 输出:"apbqrs" 解释:注意,word2 比 word1 长,"rs" 需要追加到合并后字符串的末尾。 word1: a b word2: p q r s 合并后: a p b q r s
mergeAlternately(word1, word2) { let arr = []; if (word1.length >= word2.length) { Array.from(word1).forEach((x, i) => { arr.push(x); Array.from(word2)[i] && arr.push(Array.from(word2)[i]) }) } else { Array.from(word2).forEach((x,i) => { Array.from(word1)[i] && arr.push(Array.from(word1)[i]) arr.push(x) }) } return arr.join("") } },
AIMerge(word1, word2) { let merge = ''; let minLenght = Math.min(word1.length, word2.length); for(let i=0; i<minLenght; i++) { merge += word1[i] + word2[i] } word1.length > word2.length ? merge += word1.substring(minLenght) : merge += word2.substring(minLenght) return merge }
题目:给你一个整数数组 nums
和一个整数 k
。每一步操作中,你需要从数组中选出和为 k
的两个整数,并将它们移出数组。返回你可以对数组执行的最大操作数;
maxOperations(nums, k) { let count = 0; const map = new Map(); for (let num of nums) { if(map.get(k-num)){ count++; map.set(k-num,map.get(k-num)-1); }else{ map.set(num,(map.get(num)||0)+1); } } return count; },
标签:map,arr,nums,leetCode1768,word1,1679,&&,word2,字符串 From: https://www.cnblogs.com/wangxinyubokeyuan/p/17475353.html