Skip to content

Commit 6c1df67

Browse files
committed
6.08
1 parent 5be12b7 commit 6c1df67

File tree

7 files changed

+187
-0
lines changed

7 files changed

+187
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
3+
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
4+
5+
Example:
6+
Given num = 16, return true. Given num = 5, return false.
7+
8+
Follow up: Could you solve it without loops/recursion?
9+
10+
11+
12+
"""
13+
14+
class Solution(object):
15+
def isPowerOfFour(self, num):
16+
"""
17+
:type num: int
18+
:rtype: bool
19+
"""
20+
return num>0 and (num & num-1)==0 and (num-1)%3==0

easy/其他/290_Word Pattern.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
3+
Given a pattern and a string str, find if str follows the same pattern.
4+
5+
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
6+
7+
Examples:
8+
pattern = "abba", str = "dog cat cat dog" should return true.
9+
pattern = "abba", str = "dog cat cat fish" should return false.
10+
pattern = "aaaa", str = "dog cat cat dog" should return false.
11+
pattern = "abba", str = "dog dog dog dog" should return false.
12+
Notes:
13+
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
14+
15+
"""
16+
17+
"""my solution
18+
使用zip和set
19+
"""
20+
21+
22+
class Solution(object):
23+
def wordPattern(self, pattern, str):
24+
"""
25+
:type pattern: str
26+
:type str: str
27+
:rtype: bool
28+
"""
29+
30+
return len(pattern) == len(str.split(' ')) and len(set(zip(pattern, str.split(' ')))) == len(
31+
set(pattern)) == len(set(str.split(' ')))
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
3+
Write a function that takes a string as input and returns the string reversed.
4+
5+
Example:
6+
Given s = "hello", return "olleh".
7+
8+
"""
9+
10+
class Solution(object):
11+
def reverseString(self, s):
12+
"""
13+
:type s: str
14+
:rtype: str
15+
"""
16+
return s[::-1]

easy/字符串/__init__.py

Whitespace-only changes.

easy/数组/283_Move Zeroes.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
3+
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
4+
5+
For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
6+
7+
Note:
8+
You must do this in-place without making a copy of the array.
9+
Minimize the total number of operations.
10+
11+
12+
"""
13+
14+
"""my solution
15+
先把不为零的往前放,最后补0
16+
"""
17+
18+
class Solution(object):
19+
def moveZeroes(self, nums):
20+
"""
21+
:type nums: List[int]
22+
:rtype: void Do not return anything, modify nums in-place instead.
23+
"""
24+
index = 0
25+
for num in nums:
26+
if num != 0:
27+
nums[index] = num
28+
index += 1
29+
for i in range(index,len(nums)):
30+
nums[i] = 0
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
3+
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
4+
5+
Example:
6+
Given nums = [-2, 0, 3, -5, 2, -1]
7+
8+
sumRange(0, 2) -> 1
9+
sumRange(2, 5) -> -1
10+
sumRange(0, 5) -> -3
11+
Note:
12+
You may assume that the array does not change.
13+
There are many calls to sumRange function.
14+
15+
"""
16+
"""这里用到的技巧就是数组存储前n项的累加和,这里要判断i是否为0"""
17+
18+
class NumArray(object):
19+
def __init__(self, nums):
20+
"""
21+
:type nums: List[int]
22+
"""
23+
self.dp = nums
24+
for i in range(1, len(nums)):
25+
self.dp[i] += self.dp[i - 1]
26+
27+
def sumRange(self, i, j):
28+
"""
29+
:type i: int
30+
:type j: int
31+
:rtype: int
32+
"""
33+
return self.dp[j] - (self.dp[i - 1] if i > 0 else 0)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
3+
Given two arrays, write a function to compute their intersection.
4+
5+
Example:
6+
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
7+
8+
Note:
9+
Each element in the result must be unique.
10+
The result can be in any order.
11+
12+
"""
13+
14+
"""my solution"""
15+
16+
17+
class Solution(object):
18+
def intersection(self, nums1, nums2):
19+
"""
20+
:type nums1: List[int]
21+
:type nums2: List[int]
22+
:rtype: List[int]
23+
"""
24+
25+
nums1 = set(nums1)
26+
return [x for x in set(nums2) if x in nums1]
27+
28+
"""other solution
29+
直接用set的基本操作:
30+
>>> x = set("jihite")
31+
>>> y = set(['d', 'i', 'm', 'i', 't', 'e'])
32+
>>> x #把字符串转化为set,去重了
33+
set(['i', 'h', 'j', 'e', 't'])
34+
>>> y
35+
set(['i', 'e', 'm', 'd', 't'])
36+
>>> x & y #交
37+
set(['i', 'e', 't'])
38+
>>> x | y #并
39+
set(['e', 'd', 'i', 'h', 'j', 'm', 't'])
40+
>>> x - y #差
41+
set(['h', 'j'])
42+
>>> y - x
43+
set(['m', 'd'])
44+
>>> x ^ y #对称差:x和y的交集减去并集
45+
set(['d', 'h', 'j', 'm'])
46+
47+
"""
48+
49+
def intersection(self, nums1, nums2):
50+
"""
51+
:type nums1: List[int]
52+
:type nums2: List[int]
53+
:rtype: List[int]
54+
"""
55+
nums1=set(nums1)
56+
nums2=set(nums2)
57+
return list(nums1&nums2)

0 commit comments

Comments
 (0)