# 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"`.

``````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]
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.

``````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)
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, ], and employee 2 has [2, 10, ], 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)
self.getDirectSubordinatesImportances(self.d.get(id)[1:])
return self.total

def getDirectSubordinatesImportances(self, idList):
for i in idList:
self.total+=self.d.get(i)
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]-points[j]
dy = points[i]-points[j]
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.

``````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``````

### 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.

``````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.

``````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.

``````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.

``````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.

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