交换数字
/** * @param {number[]} numbers * @return {number[]} */ var swapNumbers = function(numbers) { numbers.push(numbers.splice(0,1)) return numbers }; // 二 var swapNumbers = function(numbers) { numbers[0] =numbers[0]^numbers[1]; numbers[1] =numbers[0]^numbers[1]; numbers[0]=numbers[0]^numbers[1]; return numbers; };
单词频率’
标签:pre,return,19,金典,---,item,book,numbers,var From: https://www.cnblogs.com/dgqp/p/17365760.html思路:
reduce
+哈希/** * @param {string[]} book */ var WordsFrequency = function(book) { this.book = book.reduce((pre,item)=>{ pre[item] = !pre[item]? 1 : pre[item] + 1 return pre },{}) }; /** * @param {string} word * @return {number} */ WordsFrequency.prototype.get = function(word) { return this.book[word] || 0 }; /** * Your WordsFrequency object will be instantiated and called as such: * var obj = new WordsFrequency(book) * var param_1 = obj.get(word) */