Skip to content

Commit ce89772

Browse files
committed
0620
1 parent d60db87 commit ce89772

File tree

55 files changed

+1874
-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.

55 files changed

+1874
-1
lines changed

sort_by_leetcode/array/easy/217. Contains Duplicate.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,28 @@
22
33
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
44
5+
Subscribe to see which companies asked this question.
6+
57
"""
6-
"""使用set不能有重复值的特性,判断前后长度是否相同"""
8+
"""利用hash table,别忘了判断空数组的情况.其实更麻烦"""
9+
10+
class Solution(object):
11+
def containsDuplicate(self, nums):
12+
"""
13+
:type nums: List[int]
14+
:rtype: bool
15+
"""
16+
if not nums:
17+
return False
18+
dic = dict()
19+
for num in nums:
20+
if num in dic:
21+
return True
22+
dic[num] = 1
23+
return False
24+
25+
"""直接利用set的性质反而更简单"""
26+
727
class Solution(object):
828
def containsDuplicate(self, nums):
929
"""

sort_by_leetcode/hashtable/__init__.py

Whitespace-only changes.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
3+
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
4+
5+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
6+
7+
Example:
8+
Given nums = [2, 7, 11, 15], target = 9,
9+
10+
Because nums[0] + nums[1] = 2 + 7 = 9,
11+
return [0, 1].
12+
13+
"""
14+
15+
class Solution(object):
16+
def twoSum(self, nums, target):
17+
"""
18+
:type nums: List[int]
19+
:type target: int
20+
:rtype: List[int]
21+
"""
22+
dic = dict()
23+
for index,value in enumerate(nums):
24+
sub = target - value
25+
if sub in dic:
26+
return [dic[sub],index]
27+
else:
28+
dic[value] = index
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
3+
Given an array of integers, every element appears twice except for one. Find that single one.
4+
5+
Note:
6+
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
7+
8+
"""
9+
"""这道题虽然放在了hashtable里,但其实用二进制的算法更容易求解
10+
两个相同数的二进制异或为0,所以遍历一边数组,出现两次的异或值变为0,那么剩下的数就是出现一次的数
11+
"""
12+
13+
class Solution(object):
14+
def singleNumber(self, nums):
15+
"""
16+
:type nums: List[int]
17+
:rtype: int
18+
"""
19+
res = 0
20+
for i in nums:
21+
res = res ^ i
22+
return res
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
3+
Write an algorithm to determine if a number is "happy".
4+
5+
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
6+
7+
Example: 19 is a happy number
8+
9+
12 + 92 = 82
10+
82 + 22 = 68
11+
62 + 82 = 100
12+
12 + 02 + 02 = 1
13+
Credits:
14+
Special thanks to @mithmatt and @ts for adding this problem and creating all test cases.
15+
16+
"""
17+
18+
"""解法1,用列表保存出现过的数,当出现重复的数字的时候,说明出现了循环,循环有两种情况,一种不是hayyp数,循环的数必不是1,如果是happy数,那么会出现1的不断循环"""
19+
class Solution(object):
20+
def isHappy(self, n):
21+
"""
22+
:type n: int
23+
:rtype: bool
24+
"""
25+
c = set()
26+
while not n in c:
27+
c.add(n)
28+
n = sum([int(x) ** 2 for x in str(n)])
29+
return n==1
30+
31+
"""解法2,使用追赶法,快的指针每次前进两步,慢的指针每次只前进一步,当快的指针追上慢的指针即二者数字相同时,同样说明出现了
32+
循环,情况同上"""
33+
class Solution(object):
34+
def isHappy(self, n):
35+
"""
36+
:type n: int
37+
:rtype: bool
38+
"""
39+
slow = n
40+
quick = sum([int(x) ** 2 for x in str(n)])
41+
while quick != slow:
42+
quick = sum([int(x) ** 2 for x in str(quick)])
43+
quick = sum([int(x) ** 2 for x in str(quick)])
44+
slow = sum([int(x) ** 2 for x in str(slow)])
45+
return slow == 1
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
3+
Description:
4+
5+
Count the number of prime numbers less than a non-negative number, n.
6+
7+
"""
8+
9+
"""统计比n小的质数有多少,首先设置一个数组,全部质为true,然后遍历2-sqrt(n)的数,把他们的倍数所在的数组位置
10+
置为True这里为了避免重复,从i*i的位置开始,而不是从i*2的位置开始,因为i*2,i*3,i*n-1其实已经置为false了
11+
"""
12+
class Solution(object):
13+
def countPrimes(self, n):
14+
"""
15+
:type n: int
16+
:rtype: int
17+
"""
18+
if n < 3:
19+
return 0
20+
res = [True] * n
21+
res[0] = res[1] = False
22+
for i in range(2,int(math.sqrt(n)) + 1):
23+
res[i*i:n:i] = [False] * len(res[i*i:n:i])
24+
return sum(res)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
"""一开始我的解法是这样的,但是这是不对的,如下面的情况s='ab',t='aa' 就会出现错误"""
19+
class Solution(object):
20+
def isIsomorphic(self, s, t):
21+
"""
22+
:type s: str
23+
:type t: str
24+
:rtype: bool
25+
"""
26+
if len(s) != len(t):
27+
return False
28+
dic = dict()
29+
for i in range(len(s)):
30+
if s[i] in dic and dic[s[i]] != t[i]:
31+
return False
32+
else:
33+
dic[s[i]] = t[i]
34+
return True
35+
"""所以用两个dict分别对应保存两个字符串对应位置的对方字符,只要其中一个不满足条件,则返回错误"""
36+
class Solution(object):
37+
def isIsomorphic(self, s, t):
38+
"""
39+
:type s: str
40+
:type t: str
41+
:rtype: bool
42+
"""
43+
if len(s) != len(t):
44+
return False
45+
dic1 = dict()
46+
dic2 = dict()
47+
for i in range(len(s)):
48+
if (s[i] in dic1 and dic1[s[i]] != t[i]) or (t[i] in dic2 and dic2[t[i]] != s[i]):
49+
return False
50+
else:
51+
dic1[s[i]] = t[i]
52+
dic2[t[i]] = s[i]
53+
return True
54+
55+
"""其实,还有更简单的解法,使用zip将两个数组对应位置元素相连,再利用set不能有重复元素的特性,判断三者的长度是否相同即可"""
56+
class Solution(object):
57+
def isIsomorphic(self, s, t):
58+
"""
59+
:type s: str
60+
:type t: str
61+
:rtype: bool
62+
"""
63+
return len(set(zip(s,t))) == len(set(s)) == len(set(t))
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
3+
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
4+
5+
Subscribe to see which companies asked this question.
6+
7+
"""
8+
"""利用hash table,别忘了判断空数组的情况.其实更麻烦"""
9+
10+
class Solution(object):
11+
def containsDuplicate(self, nums):
12+
"""
13+
:type nums: List[int]
14+
:rtype: bool
15+
"""
16+
if not nums:
17+
return False
18+
dic = dict()
19+
for num in nums:
20+
if num in dic:
21+
return True
22+
dic[num] = 1
23+
return False
24+
25+
"""直接利用set的性质反而更简单"""
26+
27+
class Solution(object):
28+
def containsDuplicate(self, nums):
29+
"""
30+
:type nums: List[int]
31+
:rtype: bool
32+
"""
33+
return len(nums) != len(set(nums))
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
3+
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
4+
5+
"""
6+
"""仍然用dict保存数组元素出现的位置,两种情况下更新"""
7+
class Solution(object):
8+
def containsNearbyDuplicate(self, nums, k):
9+
"""
10+
:type nums: List[int]
11+
:type k: int
12+
:rtype: bool
13+
"""
14+
dic = dict()
15+
for index,value in enumerate(nums):
16+
if value in dic and index - dic[value] <= k:
17+
return True
18+
dic[value] = index
19+
return False
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""
2+
Given two strings s and t, write a function to determine if t is an anagram of s.
3+
4+
For example,
5+
s = "anagram", t = "nagaram", return true.
6+
s = "rat", t = "car", return false.
7+
8+
Note:
9+
You may assume the string contains only lowercase alphabets.
10+
11+
"""
12+
"""用两个字典保存字符出现的情况,判断两个字典是否相同即可"""
13+
class Solution(object):
14+
def isAnagram(self, s, t):
15+
"""
16+
:type s: str
17+
:type t: str
18+
:rtype: bool
19+
"""
20+
dic1 = {}
21+
dic2 = {}
22+
for i in s:
23+
dic1[i] = dic1.get(i,0)+1
24+
for i in t:
25+
dic2[i] = dic2.get(i,0)+1
26+
return dic1 == dic2

0 commit comments

Comments
 (0)