声明:
- 此文档只做自我的LeetCode记录,并无其他想法。
- 答案代码全为Python 3 版本解答,其他语言目前不做解答。
- 如需沟通,请发邮件、进群或者直接评论。
记录如下:
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.
Example:
1
2
3
4
|
Given
nums
=
[
2
,
7
,
11
,
15
]
,
target
=
9
,
Because
nums
[
<
strong
>
0
<
/
strong
>
]
+
nums
[
<
strong
>
1
<
/
strong
>
]
=
2
+
7
=
9
,
return
[
<
strong
>
0
<
/
strong
>
,
<
strong
>
1
<
/
strong
>
]
.
|
思路:
遍历nums,然后在nums中找到Target减去nmus[i]的值,并且这个在nums中的位置不能和i相同。
答案:
1
2
3
4
5
6
7
8
9
10
11
|
class
Solution
:
def
twoSum
(
self
,
nums
,
target
)
:
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for
i
in
range
(
len
(
nums
)
)
:
if
(
(
target
-
nums
[
i
]
)
in
nums
)
and
(
(
nums
.
index
(
target
-
nums
[
i
]
)
!=
i
)
)
:
return
[
i
,
nums
.
index
(
target
-
nums
[
i
]
)
]
return
None
|
参考资料:资料1
描述:
Given an integer n, return the number of trailing zeroes in n!.
Example 1:
1
2
3
|
<
strong
>
Input
:
<
/
strong
>
3
<
strong
>
Output
:
<
/
strong
>
0
<
strong
>
Explanation
:
<
/
strong
>
3
!
=
6
,
no
trailing
zero
.
|
Example 2:
1
2
3
|
<
strong
>
Input
:
<
/
strong
>
5
<
strong
>
Output
:
<
/
strong
>
1
<
strong
>
Explanation
:
<
/
strong
>
5
!
=
120
,
one
trailing
zero
.
|
Note: Your solution should be in logarithmic time complexity.
阶乘查看末尾为零个数,实际就是看有多少个5*2,因为末尾零的个数是指有多少个10相乘,阶乘中的1-9中能够拆为2和5 的有5和2的倍数,显然2个数要比5多,此时我们只要找到有多少个5,就可以知道有多少个末尾零。
1
2
3
4
5
6
7
8
9
10
11
12
|
class
Solution
:
def
trailingZeroes
(
self
,
n
)
:
"""
:type n: int
:rtype: int
"""
count
=
0
n
=
int
(
n
)
while
n
>
0
:
n
/
/=
5
;
count
+=
n
;
return
count
|
参考资料: 资料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"
.
Example 1:
1
2
|
<
strong
>
Input
:
<
/
strong
>
J
=
"aA"
,
S
=
"aAAbbbb"
<
strong
>
Output
:
<
/
strong
>
3
|
Example 2:
1
2
|
<
strong
>
Input
:
<
/
strong
>
J
=
"z"
,
S
=
"ZZ"
<
strong
>
Output
:
<
/
strong
>
0
|
Note:
S
andJ
will consist of letters and have length at most 50.- The characters in
J
are distinct.
从S字符串里循环取值去和J字符串里的值对比。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class
Solution
:
def
numJeweLsInStones
(
self
,
J
,
S
)
:
'''
:param J:
:param S:
:return:
'''
count
=
0
for
j
in
J
:
for
s
in
S
:
if
j
==
s
:
count
+=
1
return
count
|
看到一个是这样的:
1
2
3
4
5
6
7
8
9
10
11
12
|
class
Solution
:
def
numJewelsInStones
(
self
,
J
,
S
)
:
""
"
:type J: str
:type S: str
:rtype: int
"
""
jewels
=
0
for
s
in
S
:
if
s
in
J
:
jewels
+=
1
return
jewels
|
709. To Lower Case
Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
Example 1:
1
|
<
strong
>
Input
:
<
/
strong
>
<
span
id
=
"example-input-1-1"
>
"Hello"
<
/
span
>
<
strong
>
Output
:
<
/
strong
>
<
span
id
=
"example-output-1"
>
"hello"
<
/
span
>
|
Example 2:
1
|
<
strong
>
Input
:
<
/
strong
>
<
span
id
=
"example-input-2-1"
>
"here"
<
/
span
>
<
strong
>
Output
:
<
/
strong
>
<
span
id
=
"example-output-2"
>
"here"
<
/
span
>
|
Example 3:
1
|
<
strong
>
Input
:
<
/
strong
>
<
span
id
=
"example-input-3-1"
>
"LOVELY"
<
/
span
>
<
strong
>
Output
:
<
/
strong
>
<
span
id
=
"example-output-3"
>
"lovely"
<
/
span
>
|
思路:
python 3 中的Str 自带有一个把大写字母转换为小写字母的内置方法:str.lower()
答案:
1
2
3
4
5
6
7
|
class
Solution
:
def
toLowerCase
(
self
,
str
)
:
""
"
:type str: str
:rtype: str
"
""
return
str
.
lower
(
)
|
905. Sort Array By Parity
描述:
Given an array A
of non-negative integers, return an array consisting of all the even elements of A
, followed by all the odd elements of A
.
You may return any answer array that satisfies this condition.
Example 1:
1
2
|
<
strong
>
Input
:
<
/
strong
>
<
span
id
=
"example-input-1-1"
>
[
3
,
1
,
2
,
4
]
<
/
span
>
<
strong
>
Output
:
<
/
strong
>
<
span
id
=
"example-output-1"
>
[
2
,
4
,
3
,
1
]
<
/
span
>
The
outputs
[
4
,
2
,
3
,
1
]
,
[
2
,
4
,
1
,
3
]
,
and
[
4
,
2
,
1
,
3
]
would
also
be
accepted
.
|
Note:
1 <= A.length <= 5000
0 <= A[i] <= 5000
思路:
奇偶校验主要看是否能够被2整除,python 中使用 % 进行求余。
答案:
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
:
def
sortArrayByParity
(
self
,
A
)
:
""
"
自己的代码:
:type A: List[int]
:rtype: List[int]
"
""
even_list
=
[
]
odd_list
=
[
]
for
a
in
A
:
if
a
%
2
==
0
:
even_list
.
append
(
a
)
else
:
odd_list
.
append
(
a
)
return
even_list
+
odd_list
# 优化的代码:
def
sortArrayByParity
(
self
,
A
)
:
""
"
:type A: List[int]
:rtype: List[int]
"
""
o
=
[
each
for
each
in
A
if
each
%
2
==
0
]
j
=
[
each
for
each
in
A
if
not
each
%
2
==
0
]
return
o
+
j
|
461. Hamming Distance
描述:
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers x
and y
, calculate the Hamming distance.
Note:
0 ≤ x
, y
< 231.
Example:
1
2
3
4
5
6
7
8
9
10
|
<
b
>
Input
:
<
/
b
>
x
=
1
,
y
=
4
<
b
>
Output
:
<
/
b
>
2
<
b
>
Explanation
:
<
/
b
>
1
(
0
0
0
1
)
4
(
0
1
0
0
)
↑
↑
The
above
arrows
point
to
positions
where
the
corresponding
bits
are
different
.
|
思路:
这道题的思路:
把每个数,转化为二进制,然后比较对应位是否一致,统计不一致的数量,就是return的结果。此答案的妙处:
1、python 中的运算符:^,解释:按位异或运算符:当两对应的二进位相异时,结果为1 。
通过^直接做二级制转换和对位比较。
2、bin()方法的结果是str()类型,str类型中的内置count()方法,统计其中的“1”,来得到想要的结果。
难点:
1、得找到一个好的方式来比较二级制的位数和对应位的值是否一致
2、通过自己的循环方式不够简练
运算符^ 正好解决了以上的所有问题。
答案:
1
2
3
4
5
6
7
8
|
class
Solution
:
def
hammingDistance
(
self
,
x
,
y
)
:
""
"
:type x: int
:type y: int
:rtype: int
"
""
return
bin
(
int
(
x
)
^
int
(
y
)
)
.
count
(
"1"
)
|
描述:
There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0)after it completes its moves.
The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.
Note: The way that the robot is "facing" is irrelevant. "R" will always make the robot move to the right once, "L" will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.
Example 1:
1
2
3
|
<
b
>
Input
:
<
/
b
>
"UD"
<
b
>
Output
:
<
/
b
>
true
<
strong
>
Explanation
<
/
strong
>
:
The
robot
moves
up
once
,
and
then
down
once
.
All
moves
have
the
same
magnitude
,
so
it
ended
up
at
the
origin
where
it
started
.
Therefore
,
we
return
true
.
|
Example 2:
1
2
3
|
<
b
>
Input
:
<
/
b
>
"LL"
<
b
>
Output
:
<
/
b
>
false
<
strong
>
Explanation
<
/
strong
>
:
The
robot
moves
left
twice
.
It
ends
up
two
"moves"
to
the
left
of
the
origin
.
We
return
false
because
it
is
not
at
the
origin
at
the
end
of
its
moves
.
|
思路:
机器人能否回到原味,主要判断相反的动作是否能够互相抵消。
通过str()的cout()内置方法,统计各个移动操作:U、D、L、R,如果相反操作可以抵消,True,反之,False。
答案:
1
2
3
4
5
6
7
|
class
Solution
:
def
judgeCircle
(
self
,
moves
)
:
"""
:type moves: str
:rtype: bool
"""
return
moves
.
count
(
"L"
)
==
moves
.
count
(
"R"
)
and
moves
.
count
(
'U'
)
==
moves
.
count
(
'D'
)
|
929 |
思路:
第一步:通过“@”拆分邮箱地址,分为local和domain,domain是唯一的,不允许操作
第二步:获取“+”之前的local地址,因为地址在不记录“+”之后的内容
第三步:去除掉local中的“.”,因为带不带“.”都被认作一个邮箱地址
第四步:把经过“+”和“.”处理的local和“”@和domain合并起来
最后,通过for循环得到一个set集合,输出len(set())得到数量
参考答案:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class
Solution
:
def
numUniqueEmails
(
self
,
emails
)
:
"""
:type emails: List[str]
:rtype: int
"""
email_list
=
[
]
for
email
in
emails
:
email_head
,
email_tail
=
email
.
split
(
"@"
)
if
email_head
.
count
(
'+'
)
:
email_head
=
email_head
.
split
(
"+"
)
[
0
]
email_head
=
email_head
.
replace
(
"."
,
""
)
email_list
.
append
(
email_head
+
"@"
+
email_tail
)
return
len
(
set
(
email_list
)
)
|