LeetCode Hash Table算法题(Easy级别)整理 Part 1

771. Jewels and Stones

You’re given strings J representing the types of stones that are jewels, and S representing the stones you have.  Each character in S is a type of stone you have.  You want to know how many of the stones you have are also jewels. The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A".

解题思路:依次遍历J中的宝石J[i],并计算S中有多少J[i]。

class Solution(object):
    def numJewelsInStones(self, J, S):
        """
        :type J: str
        :type S: str
        :rtype: int
        """
        ans = 0
        for j in J:
            ans+=S.count(j)
        return ans

500. Keyboard Row

Given a List of words, return the words that can be typed using letters of alphabet on only one row’s of American keyboard like the image below.

解题思路:依次判断每个词的每个字符是否属于同一行即可。用字典可以加快速度。

class Solution(object):
    def findWords(self, words):
        """
        :type words: List[str]
        :rtype: List[str]
        """
        alphabets = ['qwertyuiopQWERTYUIOP','asdfghjklASDFGHJKL','zxcvbnmZXCVBNM']
        self.d = {}
        for i in range(3):
            for j in range(len(alphabets[i])):
                self.d[alphabets[i][j]] = i
                
        def isLegal(word):
            cur = self.d[word[0]]
            for c in word[1:]:
                if self.d[c] != cur:
                    return False
            return True
        
        return list(filter(isLegal,words))

575. Distribute Candies

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister. Return the maximum number of kinds of candies the sister could gain.

解题思路:易知姐姐最多能分到的糖果数为总糖果数的一半,当糖果的种类数多于或等于糖果数一半时;而其他情况,姐姐能分到的不同的糖果数就只有糖果的种类数。

class Solution(object):
    def distributeCandies(self, candies):
        """
        :type candies: List[int]
        :rtype: int
        """
        return min(len(candies)//2,len(set(candies)))

463. Island Perimeter

You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn’t have “lakes” (water inside that isn’t connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don’t exceed 100. Determine the perimeter of the island.

解题思路:从左往右,从上往下遍历数组,遇到1则边数加4,并判断其右边和下边的数是否为1,为1则边的总数减2。

class Solution(object):
    def islandPerimeter(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: int
        """
        row = len(grid)
        if not row:
            return 0
        col = len(grid[0])
        if not col:
            return 0
        count = 0
        for i in range(row):
            for j in range(col):
                if grid[i][j]:
                    count+=4
                    if i == row-1:
                        if j != col-1:
                            count-=2*grid[i][j+1]
                    else:
                        count-=2*grid[i+1][j]
                        if j != col-1:
                            count-=2*grid[i][j+1]
        return count

690. Employee Importance

You are given a data structure of employee information, which includes the employee’s unique id, his importance value and his direct subordinates’ id.

For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. They have importance value 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []]. Note that although employee 3 is also a subordinate of employee 1, the relationship is not direct.

Now given the employee information of a company, and an employee id, you need to return the total importance value of this employee and all his subordinates.

解题思路:深度优先递归求解即可。

"""
# Employee info
class Employee(object):
    def __init__(self, id, importance, subordinates):
        # It's the unique id of each node.
        # unique id of this employee
        self.id = id
        # the importance value of this employee
        self.importance = importance
        # the id of direct subordinates
        self.subordinates = subordinates
"""
class Solution(object):
    d = {}
    total = 0
    
    def getImportance(self, employees, id):
        """
        :type employees: Employee
        :type id: int
        :rtype: int
        """
        for x in employees:
            self.d[x.id] = [x.importance] + x.subordinates
        self.total+=self.d.get(id)[0]
        self.getDirectSubordinatesImportances(self.d.get(id)[1:])
        return self.total
        
    def getDirectSubordinatesImportances(self, idList):
        for i in idList:
            self.total+=self.d.get(i)[0]
            self.getDirectSubordinatesImportances(self.d.get(i)[1:])

349. Intersection of Two Arrays

Given two arrays, write a function to compute their intersection.

Note: Each element in the result must be unique. The result can be in any order.

解题思路:转成集合的交集运算即可。

class Solution(object):
    def intersection(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        if not nums1 or not nums2:
            return []
        return list(set(nums1)&set(nums2))

242. Valid Anagram

Given two strings s and t, write a function to determine if t is an anagram of s.

解题思路:利用字典即可。

class Solution(object):
    def isAnagram(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        dictS = {}
        dictT = {}
        for n in s:
            if n not in dictS:
                dictS[n] = 1
            else:
                dictS[n]+=1
        for n in t:
            if n not in dictT:
                dictT[n] = 1
            else:
                dictT[n]+=1
        return dictS == dictT

217. Contains Duplicate

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

解题思路:对于这种判重的题目,字典一般都可以解决。

class Solution(object):
    def containsDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        d = {}
        for n in nums:
            if n not in d:
                d[n] = 1
            else:
                return True
        return False

判重的另一法宝——》集合。

class Solution(object):
    def containsDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        return len(nums) != len(set(nums))

447. Number of Boomerangs

Given n points in the plane that are all pairwise distinct, a “boomerang” is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters).

Find the number of boomerangs. You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive).

解题思路:这道题没有太简便的方法,只是利用了字典可以稍微的快一点 = =依次对各个点寻找其余点到它距离相等的情况,然后求组合的个数即可。

import math
class Solution(object):
    def numberOfBoomerangs(self, points):
        """
        :type points: List[List[int]]
        :rtype: int
        """
        nbPoints = len(points)
        if nbPoints < 3:
            return 0
        count = 0
        for i in range(nbPoints):
            d = {}
            for j in range(nbPoints):
                dx = points[i][0]-points[j][0]
                dy = points[i][1]-points[j][1]
                dis = dx**2+dy**2
                if dis not in d:
                    d[dis] = 1
                else:
                    d[dis]+=1
            for v in d.values():
                if v > 1:
                    count+=(v*(v-1))
        return count

599. Minimum Index Sum of Two Lists

Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings. You need to help them find out their common interest with the least list index sum. If there is a choice tie between answers, output all of them with no order requirement. You could assume there always exists an answer.

解题思路:先将第一个列表存入字典,然后判断第二个列表中有哪些和第一个列表重合的餐馆名,并同时计算距离。最后对距离字典进行遍历判断即可。

class Solution(object):
    def findRestaurant(self, list1, list2):
        """
        :type list1: List[str]
        :type list2: List[str]
        :rtype: List[str]
        """
        lenList1 = len(list1)
        lenList2 = len(list2)
        d1 = {}
        for i in range(lenList1):
            d1[list1[i]] = i
        d2 = {}
        for i in range(lenList2):
            if list2[i] in d1:
                d2[list2[i]] = i + d1[list2[i]]
        minDis = lenList1+lenList2
        ret = []
        for k,v in d2.items():
            if v < minDis:
                minDis = v
                ret = [k]
            elif v == minDis:
                ret.append(k)
        return ret

409. Longest Palindrome

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters. This is case sensitive, for example "Aa" is not considered a palindrome here.

解题思路:利用字典先将字符串的各个字符出现的次数进行记录,之后遍历字典,对于出现次数大于2的情况进行记录即可。

class Solution(object):
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: int
        """
        d = {}
        for c in s:
            if c not in d:
                d[c] = 1
            else:
                d[c]+=1
        ret = 0
        for v in d.values():
            ret+=(v//2)
        ret*=2
        if ret < len(s):
            return ret+1
        return ret

350. Intersection of Two Arrays II

Given two arrays, write a function to compute their intersection.

Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].

Note:Each element in the result should appear as many times as it shows in both arrays. The result can be in any order.

解题思路:遇到这种需要利用到两个数组的重复信息的时候,用字典基本没错。第一次先把其中一个存入一个字典,第二次再用字典求出两个数组的交集情况即可。

class Solution(object):
    def intersect(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        if not nums1 or not nums2:
            return []
        d1 = {}
        for n in nums1:
            if n not in d1:
                d1[n] = 1
            else:
                d1[n]+=1
        d2 = {}
        for n in nums2:
            if n in d1:
                if n not in d2:
                    d2[n] = 1
                else:
                    d2[n]+=1
        ret = []
        for k,v in d2.items():
            ret.extend([k]*min(v,d1[k]))
        return ret

720. Longest Word in Dictionary

Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest lexicographical order.

If there is no answer, return the empty string.

解题思路:挺坑的= =没有营养的题目

class Solution(object):
    def longestWord(self, words):
        """
        :type words: List[str]
        :rtype: str
        """
        length = len(words)
        if length < 2:
            return ''
        wordsLen = [len(w) for w in words]
        wordsDistributions = {}
        for w in words:
            wordsDistributions[w] = 1
        maxLen = -1
        ans = []
        for i in range(length):
            judge = True
            for j in range(wordsLen[i]):
                if words[i][:j+1] not in wordsDistributions:
                    judge = False
                    break
            if judge:
                if wordsLen[i] > maxLen:
                    maxLen = wordsLen[i]
                    ans = [words[i]]
                elif wordsLen[i] == maxLen:
                    ans.append(words[i])
        if not ans:
            return ''
        ans.sort()
        return ans[0]

202. Happy Number

Write an algorithm to determine if a number is “happy”.

A 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, and 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 numbers.

解题思路:利用字典记录每次计算出的结果。在每次计算后都判断当前元素是否已在字典里,若是,说明存在循环,不是happy number。

class Solution(object):
    def isHappy(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n <= 0:
            return False
        memo = {}
        memo[n] = 1
        while True:
            t = 0
            while n:
                t+=(n%10)**2
                n//=10
            if t == 1:
                return True
            if t in memo:
                return False
            n = t
            memo[n] = 1

594. Longest Harmonious Subsequence

We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactly 1. Now, given an integer array, you need to find the length of its longest harmonious subsequence among all its possible subsequences.

解题思路:将每个数和其出现的次数存入字典中,之后在字典中判断即可。

class Solution(object):
    def findLHS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        d = {}
        for n in nums:
            if n not in d:
                d[n] = 1
            else:
                d[n]+=1
        maxLen = 0
        for k,v in d.items():
            if k+1 in d:
                ans = v + d.get(k+1)
                if ans > maxLen:
                    maxLen = ans
        return maxLen

645. Set Mismatch

The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in the set, which results in repetition of one number and loss of another number. Given an array nums representing the data status of this set after the error. Your task is to firstly find the number occurs twice and then find the number that is missing. Return them in the form of an array.

解题思路:利用集合和一些数学知识,可以很容易的解出此题。重复的元素可以直接用原数组nums的和减去set(nums)的和得到;而缺失的元素则为n*(n+1)/2-set(nums)。

class Solution(object):
    def findErrorNums(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        delta = sum(set(nums))
        return [sum(nums)-delta,len(nums)*(len(nums)+1)//2-delta]

1. Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

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

解题思路:通过一轮遍历即可。遍历数组nums,对当前的num判断其补是否已在字典中,若是,返回下标列表;若否,将num加入字典中。

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        d = {}
        for i in xrange(len(nums)):
            t = target - nums[i]
            if d.has_key(t):
                return [d.get(t),i]
            d[nums[i]] = i

205. Isomorphic Strings

Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

解题思路:此题就是要判断s和t中的字符是否存在一一对应的映射关系。通过两轮遍历,建立映射可得。

class Solution(object):
    def isIsomorphic(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: bool
        """
        slen = len(s)
        tlen = len(t)
        if slen != tlen:
            return False
        ds = {}
        dt = {}
        for i in range(slen):
            if s[i] not in ds:
                ds[s[i]] = t[i]
            else:
                if ds[s[i]] != t[i]:
                    return False
            if t[i] not in dt:
                dt[t[i]] = s[i]
            else:
                if dt[t[i]] != s[i]:
                    return False
        return True

438. Find All Anagrams in a String

Given a string s and a non-empty string p, find all the start indices of p‘s anagrams in s. Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100. The order of output does not matter.

解题思路

    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞