Open12

LeetCode特訓: NeetCode

mae616mae616

Arrays & Hashing: [Easy] 217. Contains Duplicate

問題

https://leetcode.com/problems/contains-duplicate/description/

解いたコード

遅い

https://leetcode.com/problems/contains-duplicate/submissions/1453481791

もっと遅い

https://leetcode.com/problems/contains-duplicate/submissions/1453484726

採用

https://leetcode.com/problems/contains-duplicate/submissions/1453491550

/**
 * @param {number[]} nums
 * @return {boolean}
 */
var containsDuplicate = function(nums) {

    set = new Set();
    for(let num of nums){
        if(set.has(num)) return true;
        set.add(num);
    }
    return false;
};
mae616mae616

Arrays & Hashing: [Easy] 242. Valid Anagram

問題

https://leetcode.com/problems/valid-anagram/description/

解いたコード

遅い

https://leetcode.com/problems/valid-anagram/submissions/1453496874/

採用

https://leetcode.com/problems/valid-anagram/submissions/1453505519

/**
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
var isAnagram = function(s, t) {

    if(s.length != t.length) return false;

    // a -> charCode 97, z -> charCode 122 アルファベットは26文字
    const ary = new Array(26).fill(0);

    const start = 'a'.charCodeAt();
    for(let i=0; i<s.length; i++){
        ary[s.charCodeAt(i) - start]++;
        ary[t.charCodeAt(i) - start]--;
    }

    return !ary.some((item) => item !== 0);
};
mae616mae616

Arrays & Hashing: [Easy] 1. Two Sum

問題

https://leetcode.com/problems/two-sum/description/

解いたコード

遅い
https://leetcode.com/problems/two-sum/submissions/1453897954

採用
https://leetcode.com/problems/two-sum/submissions/1453900948

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(nums, target) {

    const map = new Map();
    for(let i=0; i<nums.length; i++){
        const value = target - nums[i];
        if(map.has(value)) return [i, map.get(value)];
        map.set(nums[i], i);
    }
};
mae616mae616

Arrays & Hashing: [Medium] 49. Group Anagrams

問題

https://leetcode.com/problems/group-anagrams/description/

解いたコード

遅い
https://leetcode.com/problems/group-anagrams/submissions/1453906656

採用
https://leetcode.com/problems/group-anagrams/submissions/1453907640

/**
 * @param {string[]} strs
 * @return {string[][]}
 */
var groupAnagrams = function(strs) {

    const map = {};
    for(let str of strs){
        const key = str.split('').sort().join('');

        if(key in map){
            map[key].push(str);
        }else{
            map[key] = [str];
        }
    }
    return Object.values(map);
}
mae616mae616

Arrays & Hashing: [Medium]347. Top K Frequent Elements

問題

https://leetcode.com/problems/top-k-frequent-elements/description/

解いたコード

採用
https://leetcode.com/problems/top-k-frequent-elements/submissions/1454015562

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
var topKFrequent = function(nums, k) {

    const map = {};
    for(let num of nums){
        if(num in map){
            map[num].count++;
        }else{
            map[num] = {key: num, count:1};
        }
    }
    return Object.values(map).sort((a, b)=>a.count - b.count).reverse().slice(0,k).map(item => item.key);
};
mae616mae616

Arrays & Hashing: [Medium] 238. Product of Array Except Self

問題

https://leetcode.com/problems/product-of-array-except-self/description/

解いたコード

採用

https://leetcode.com/problems/product-of-array-except-self/submissions/1454028508

/**
 * @param {number[]} nums
 * @return {number[]}
 */
var productExceptSelf = function(nums) {

    const n = nums.length;
    const result = new Array(n).fill(1);
    let start = 1;
    let end = 1;
    for(let i=0; i<n; i++){
        result[i] *= start;
        result[n-i-1] *= end;

        start *= nums[i];
        end *= nums[n-i-1];       
    }
    return result;
};
mae616mae616

Arrays & Hashing: [Medium] 36. Valid Sudoku

問題

https://leetcode.com/problems/valid-sudoku/description/

解いたコード

採用

https://leetcode.com/problems/valid-sudoku/submissions/1454052478

/**
 * @param {character[][]} board
 * @return {boolean}
 */
var isValidSudoku = function(board) {
    
    const n = board.length;
    const board2 = new Array(n).fill().map(_ => new Set());
    const trout = new Array(n).fill().map(_ => new Set());
    const column = new Array(n).fill().map(_ => new Set());
    for(let i=0; i<n; i++){

        for(let j=0; j<n; j++){
            const current = board[i][j];
            if(current === '.') continue;

            const index = 3 * Math.trunc(i/3) + Math.trunc(j/3);
            if(board2[i].has(current) || column[j].has(current) || trout[index].has(current)) {
                return false;
            }
            board2[i].add(current);
            column[j].add(current);
            trout[index].add(current);
        }
    }
    return true;
};
mae616mae616

Arrays & Hashing: [Medium] 128. Longest Consecutive Sequence

問題

https://leetcode.com/problems/longest-consecutive-sequence/description/

解いたコード

遅い

https://leetcode.com/problems/longest-consecutive-sequence/submissions/1454167876

採用

https://leetcode.com/problems/longest-consecutive-sequence/submissions/1454171948

/**
 * @param {number[]} nums
 * @return {number}
 */
var longestConsecutive = function(nums) {

    let max = 0;
    const nums2 = new Set(nums);
    for(const current of nums2.values()){
        if(nums2.has(current - 1)) continue;

        let i=1;
        while(nums2.has(current + i)){
            i++;
        }
        max = Math.max(max, i);
    }

    return max;
};
mae616mae616

Two Pointers: [Easy] 128. Longest Consecutive Sequence

問題

https://leetcode.com/problems/valid-palindrome/description/

解いたコード

採用

https://leetcode.com/problems/valid-palindrome/submissions/1454194200

/**
 * @param {string} s
 * @return {boolean}
 */
var isPalindrome = function(s) {
  
    let start = 0;
    let end = s.length - 1;
    while(start < end){
        while(start < end && /[^a-zA-Z0-9]/.test(s[start])) start++;
        while(start < end && /[^a-zA-Z0-9]/.test(s[end])) end--;

        if(s[start].toLowerCase() !== s[end].toLowerCase()) return false;
        start++;
        end--;
    }
    return true;
};
mae616mae616

Two Pointers: [Medium] 167. Two Sum II - Input Array Is Sorted

問題

https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/

解いたコード

採用
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/submissions/1454291067

/**
 * @param {number[]} numbers
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(numbers, target) {

    let start = 0;
    let end = numbers.length - 1;
    while(start < end){
        const sum = numbers[start] + numbers[end];

        if(target === sum){
            return [start+1, end+1];
        }else if(target < sum){
            end--;
        }else{ // target > sum
            start++;
        }
    }
};