class Solution(object):
def numJewelsInStones(self, jewels, stones):
"""
:type jewels: str
:type stones: str
:rtype: int
"""
dic={}
for i in jewels:
dic[i]=True
num=0
for i in stones:
if dic.get(i,-1)==True:
num+=1
return num
class Solution(object):
def numIdenticalPairs(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dic={}
num=0
for i in nums:
dic[i]=dic.get(i,0)+1
for i in dic.values():
num+=(i)*(i-1)/2
return num
class Solution(object):
def sumOfUnique(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dic={}
sum=0
for i in nums:
dic[i]=dic.get(i,0)+1
keys=dic.keys()
for i in keys:
if dic.get(i)==1:
sum+=i
return sum
class Solution(object):
def smallerNumbersThanCurrent(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
res=[0]*len(nums)
count=[0]*101
for i in nums:
count[i]+=1
total=0
for i in range(len(count)):
pre_count=total
total+=count[i]
count[i]=pre_count
for i in range(len(res)):
res[i]=count[nums[i]]
return res
class Solution(object):
def intersection(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
dic={}
data=[]
for i in nums1:
dic[i]=dic.get(i,0)+1
for i in nums2:
if dic.get(i,-1)!=-1:
data.append(i)
del dic[i]
return data
class Solution(object):
def uniqueOccurrences(self, arr):
"""
:type arr: List[int]
:rtype: bool
"""
dic={}
for i in arr:
dic[i]=dic.get(i,0)+1
time=dic.values()
return len(set(time))==len(time)
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums.sort()
for i in range(0,len(nums)-1,2):
if nums[i]!=nums[i+1]:
return nums[i]
return nums[-1]
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dic={}
for i in nums:
dic[i]=dic.get(i,0)+1
if dic[i]==2:
del dic[i]
return dic.keys()[0]
class Solution(object):
def findWords(self, words):
"""
:type words: List[str]
:rtype: List[str]
"""
dic1={}
dic2={}
dic3={}
for i in "qwertyuiop":
dic1[i]=1
dic1[chr(ord(i)-32)]=1
for i in "asdfghjkl":
dic2[i]=1
dic2[chr(ord(i)-32)]=1
for i in "zxcvbnm":
dic3[i]=1
dic3[chr(ord(i)-32)]=1
res=[]
for word in words:
if dic1.get(word[0])==1:
f=True
for c in word[1:]:
if dic1.get(c)!=1:
f=False
break
if f:
res.append(word)
elif dic2.get(word[0])==1:
f=True
for c in word[1:]:
if dic2.get(c)!=1:
f=False
break
if f:
res.append(word)
else :
f=True
for c in word[1:]:
if dic3.get(c)!=1:
f=False
break
if f:
res.append(word)
return res