Skip to content

Commit 3c8c99d

Browse files
author
luyunfang
committed
week2 initial commit
1 parent f9fa35c commit 3c8c99d

6 files changed

Lines changed: 215 additions & 64 deletions

File tree

.idea/workspace.xml

Lines changed: 127 additions & 63 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Week2/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@
66
2) 树的节点 + (左子树) + (右子树), 左子树 = 左子树节点 + (subleftroot.left) + (subleftroot.right), 右子树 = 右子树节点 + (subrightroot.left) + (subrightroot.right),
77
而所有节点都是同一属性的TreeNode, 这就是一个层层嵌套的结构,一个树是含有重复嵌套结构或者说重复子问题的结构。
88
3)在实际的一些应用场景,解决树的一些问题例如插入、删除操作需要先遍历再做访问,在这里可以使用循环的方法,但是要借助栈做缓存保留遍历的记忆,这个本质上还是和递归里层层走进嵌入的模式是一样的。
9-

Week2/__init__.py

Whitespace-only changes.

Week2/easy/__init__.py

Whitespace-only changes.

Week2/easy/isAnagram.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# ============================================
2+
# Leetco 242. 有效的字母异位词
3+
# 审题: 1. 进阶的情况: 如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况
4+
# not seen before
5+
# 解体思路: 字典形式储存词频
6+
# time complexity: O(2n)≈O(n)
7+
# ============================================
8+
9+
10+
# TODO Solution1
11+
#执行用时:48 ms, 在所有 Python 提交中击败了51.42%的用户
12+
#内存消耗:14.9 MB, 在所有 Python 提交中击败了11.08%的用户
13+
class Solution1(object):
14+
def isAnagram(self, s, t):
15+
"""
16+
:type s: str
17+
:type t: str
18+
:rtype: bool
19+
"""
20+
# record the frequency/count of letters in s by key-value data structure
21+
dict_ct = {}
22+
if len(s) != len(t):
23+
return False
24+
for i in range(len(s)):
25+
if s[i] not in dict_ct:
26+
dict_ct[s[i]] = 0
27+
dict_ct[s[i]] += 1
28+
for i in range(len(t)):
29+
if t[i] in dict_ct:
30+
dict_ct[t[i]] -= 1
31+
else:
32+
return False
33+
34+
if dict_ct[t[i]] == 0:
35+
del dict_ct[t[i]]
36+
37+
return (not dict_ct)
38+
39+
40+
# TODO 内存还是没有上来
41+
class Solution2(object):
42+
def isAnagram(self, s, t):
43+
"""
44+
:type s: str
45+
:type t: str
46+
:rtype: bool
47+
"""
48+
if len(s) != len(s):
49+
return False
50+
if sorted(s) == sorted(t):
51+
return True
52+
else:
53+
return False

0 commit comments

Comments
 (0)