leetcode刷题记录9-11

介绍9

Related to question Excel Sheet Column Title

Given a column title as appear in an Excel sheet, return its corresponding column number.

For example:

1
2
3
4
5
6
7
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28

思路9

利用python内置函数把字母转化为数字

代码9

1
2
3
4
5
6
7
8
9
10
11
12
class Solution(object):
def titleToNumber(self, s):
"""
:type s: str
:rtype: int
"""
n=0
t=len(s)
for i in s:
t=t-1
n=n+(ord(i)-64)*26**t
return n

介绍10

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

For example,
s = “anagram”, t = “nagaram”, return true.
s = “rat”, t = “car”, return false.

Note:
You may assume the string contains only lowercase alphabets.

Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?

思路10

简单粗暴,既然差别只在于字母的顺序,那就统一重新排序好了

代码10

1
2
3
4
5
6
7
8
9
10
11
12
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
s=list(s)
t=list(t)
s.sort()
t.sort()
return s==t

介绍11

Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.

Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])

思路11

找出递归规律就行了

代码11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution(object):
def countNumbersWithUniqueDigits(self, n):
"""
:type n: int
:rtype: int
"""
if n==0:
return 1
elif n==1:
return 10
else:
t=[9-i for i in range(n-1)]
t=[1]+t
for i in range(n):
if i==0:
pass
else:
t[i]=t[i]*t[i-1]
t=[9*i for i in t]
t=[1]+t
for i in range(n+1):
if i==0:
pass
else:
t[i]=t[i]+t[i-1]
return t[-1]