Skip to content

Commit d56b0b3

Browse files
committed
进行了分类
1 parent 4b4f1ab commit d56b0b3

File tree

56 files changed

+173
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+173
-1
lines changed
File renamed without changes.
File renamed without changes.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
"""
2+
3+
Given two strings s and t, determine if they are isomorphic.
4+
5+
Two strings are isomorphic if the characters in s can be replaced to get t.
6+
7+
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.
8+
9+
For example,
10+
Given "egg", "add", return true.
11+
12+
Given "foo", "bar", return false.
13+
14+
Given "paper", "title", return true.
15+
16+
"""
17+
18+
"""my solution
19+
用一个字典保存
20+
"""
21+
22+
class Solution(object):
23+
def isIsomorphic(self, s, t):
24+
"""
25+
:type s: str
26+
:type t: str
27+
:rtype: bool
28+
"""
29+
if len(s)!=len(t):
30+
return False
31+
res = dict()
32+
for i in range(0,len(s)):
33+
if s[i] in res.keys():
34+
if res[s[i]] == t[i]:
35+
continue
36+
else:
37+
return False
38+
else:
39+
if t[i] in res.values():
40+
return False
41+
else:
42+
res[s[i]] = t[i]
43+
return True
44+
45+
"""other solutions
46+
"""
47+
48+
49+
def isIsomorphic1(self, s, t):
50+
d1, d2 = {}, {}
51+
for i, val in enumerate(s):
52+
d1[val] = d1.get(val, []) + [i]
53+
for i, val in enumerate(t):
54+
d2[val] = d2.get(val, []) + [i]
55+
return sorted(d1.values()) == sorted(d2.values())
56+
57+
58+
def isIsomorphic2(self, s, t):
59+
d1, d2 = [[] for _ in xrange(256)], [[] for _ in xrange(256)]
60+
for i, val in enumerate(s):
61+
d1[ord(val)].append(i)
62+
for i, val in enumerate(t):
63+
d2[ord(val)].append(i)
64+
return sorted(d1) == sorted(d2)
65+
66+
"""巧妙利用zip的原理"""
67+
def isIsomorphic3(self, s, t):
68+
return len(set(zip(s, t))) == len(set(s)) == len(set(t))
69+
70+
71+
def isIsomorphic4(self, s, t):
72+
return [s.find(i) for i in s] == [t.find(j) for j in t]
73+
74+
75+
def isIsomorphic5(self, s, t):
76+
return map(s.find, s) == map(t.find, t)
77+
78+
79+
def isIsomorphic(self, s, t):
80+
d1, d2 = [0 for _ in xrange(256)], [0 for _ in xrange(256)]
81+
for i in xrange(len(s)):
82+
if d1[ord(s[i])] != d2[ord(t[i])]:
83+
return False
84+
d1[ord(s[i])] = i + 1
85+
d2[ord(t[i])] = i + 1
86+
return True
87+

easy/dict保存/__init__.py

Whitespace-only changes.
File renamed without changes.
File renamed without changes.

easy/foyid 算法/__init__.py

Whitespace-only changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)