Leetcode专题之哈希表

62 阅读1分钟

1002. Find Common Characters

Given a string array words, return an array of all characters that show up in all strings within the words (including duplicates) . You may return the answer in any order.

 

Example 1:

Input: words = ["bella","label","roller"]
Output: ["e","l","l"]

Example 2:

Input: words = ["cool","lock","cook"]
Output: ["c","o"]

这道题虽然是一道简单题,但是比较难想到解法。先统计第一个字符串所有字符出现的次数,代码如下:

int[] hash = new int[26]; // 用来统计所有字符串里字符出现的最小频率
for (int i = 0; i < A[0].size(); i++) { // 用第一个字符串给hash初始化
    hash[A[0][i] - 'a']++;
}

接下来,把其他字符串里字符的出现次数也统计出来一次放在hashOtherStr中。

然后hash 和 hashOtherStr 取最小值,这是本题关键所在,此时取最小值,就是 一个字符在所有字符串里出现的最小次数了。

代码如下:

int hashOtherStr[26] = {0}; // 统计除第一个字符串外字符的出现频率
for (int i = 1; i < A.size(); i++) {
    int[] hashOtherStr = new int[26];
    for (int j = 0; j < A[i].size(); j++) {
        hashOtherStr[A[i][j] - 'a']++;
    }
    // 这是关键所在
    for (int k = 0; k < 26; k++) { // 更新hash,保证hash里统计26个字符在所有字符串里出现的最小次数
        hash[k] = Math.min(hash[k], hashOtherStr[k]);
    }
}

最后的代码:

class Solution {
    public List<String> commonChars(String[] words) {
        List<String> res = new ArrayList<String>();
        int[] hash = new int[26];
        if (words.length == 0) {
            return res;
        }
        for (int i = 0; i < words.length; i++){
            String str = words[i];
            if (i == 0) {
                for (int j = 0; j < str.length(); j++){
                   hash[str.charAt(j) - 'a']++;
                }
            } else {
                int[] otherhash = new int[26];
                for (int j = 0; j < str.length(); j++){
                    otherhash[str.charAt(j) - 'a'] ++;
                }

                for(int k=0; k < 26; k++){
                    hash[k] = Math.min(hash[k], otherhash[k]);
                }
            }
        }
        for(int i=0; i < 26; i++){
            while(hash[i] != 0){
                char c= (char) (i+'a');
                res.add(String.valueOf(c));
                hash[i]--;
            }
        }
        return res;
    }
}

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

首先题里提醒了如果不等于1会陷入无限循环,所以可以通过哈希表存储

从一个integer n获取这个integer每一位的digit可以通过n%10,然后再把n/10直到n为0, 见下面的getNextNumber


class Solution {
    public boolean isHappy(int n) {
        Map<Integer, Integer> map = new HashMap<>();
        while(n != 1 && !map.containsKey(n)) {
            map.put(n, 1);
            n = getNextNumber(n);
        }
        return n == 1;
    }

    public int getNextNumber(int n) {
        int res = 0;
        while(n > 0) {
            int temp = n % 10;
            res += temp * temp;
            n = n/10;
        }
        return res;
    }
}