LeetCode刷题 Day06

101 阅读3分钟

LeetCode刷题 Day06

242. Valid Anagram

Given two strings s and t, return true if t is an anagram of s , and false otherwise.

An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

Example 1:

Input: s = "anagram", t = "nagaram"
Output: true

Example 2:

Input: s = "rat", t = "car"
Output: false

思路:

  • 用哈希计算第一个字符串每个字母出现的频次
  • 将第二个字符串中出现的每个字母代入哈希 做减法,如果出现未定义或负值情况则return false

代码:

/**
 * @param {string} s
 * @param {string} t
 * @return {boolean}
 */
var isAnagram = function(s, t) {
    if (s.length !== t.length) return false;
    
    let cache = {};
    
    for (let i = 0; i < s.length; i++) {
        cache[s[i]] = cache[s[i]] ? ++cache[s[i]] : 1;
    }
    
    for (let i = 0; i < t.length; i++) {
        if (!cache[t[i]]) return false;
        cache[t[i]]--;
        if (cache[t[i]] < 0) return false;
    }
    
    return true;
};

时间复杂度: O(n) 空间复杂度: O(n)


349. Intersection of Two Arrays

Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]
Explanation: [4,9] is also accepted.

思路:

  • 大致和上一个题一样,都是通过hash计算数字出现频次。但是因为结果是无重复,首先需要将数组转化为set去重
  • 通过一轮加法一轮减法的运算,将频次为0的放入最终结果
/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number[]}
 */
var intersection = function(nums1, nums2) {
    const numSet1 = new Set(nums1);
    const numSet2 = new Set(nums2);
    
    const cache = {};
    const result = [];
    
    for (let num of numSet1) {
        cache[num] = cache[num] ? ++cache[num] : 1;
    }
    
    for (let num of numSet2) {
        cache[num]--;
        if (cache[num] === 0) result.push(num);
    }
    
    return result;
};

时间复杂度: O(n) 空间复杂度: O(n)


202. Happy Number

Write an algorithm to determine if a number n is happy.

happy number is a number defined by the following process:

  • Starting with any positive integer, replace the number by the sum of the squares of its digits.
  • Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
  • Those numbers for which this process ends in 1 are happy.

Return true if n is a happy number, and false if not.

Example 1:

Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1

Example 2:

Input: n = 2
Output: false

思路:

  • 使用hash来判断是否有重复数值出现

代码:

/**
 * @param {number} n
 * @return {boolean}
 */
var isHappy = function(n) {
    let res = n;
    const cache = {};
    
    while (res !== 1) {
        let temp = res;
        res = 0;
        while (temp) {
            res += Math.pow(temp % 10, 2);
            temp = Math.floor(temp / 10);
        }
        if (cache[res]) return false;
        cache[res] = true;
    }
    
    return true;
};

1. Two Sum

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]

思路:

  • 借助hash存储target - nums[i]的index, 如果loop中hash[target - nums[i]] 不为空,说明遇到正确的数值, return [hash[target - nums[i]], i]

代码:

    /**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(nums, target) {
    const cache = {};
    
    for (let i = 0; i < nums.length; i++) {
        if (cache[nums[i]] !== undefined) {
            return [cache[nums[i]], i];
        }
        
        cache[target - nums[i]] = i;
    }
    
    return null;
};

时间复杂度: O(n) 空间复杂度: O(n)