Skip to content

Commit 5be12b7

Browse files
committed
6.08
1 parent 3c1c0b7 commit 5be12b7

9 files changed

+287
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
3+
Given two strings s and t, write a function to determine if t is an anagram of s.
4+
5+
For example,
6+
s = "anagram", t = "nagaram", return true.
7+
s = "rat", t = "car", return false.
8+
9+
Note:
10+
You may assume the string contains only lowercase alphabets.
11+
12+
"""
13+
14+
"""
15+
my solution
16+
用两个字典保存字母出现的次数,最后判断两个字典是否相同
17+
"""
18+
19+
class Solution(object):
20+
def isAnagram(self, s, t):
21+
"""
22+
:type s: str
23+
:type t: str
24+
:rtype: bool
25+
"""
26+
dic1 = {}
27+
dic2 = {}
28+
for i in s:
29+
dic1[i] = dic1.get(i,0)+1
30+
for i in t:
31+
dic2[i] = dic2.get(i,0)+1
32+
return dic1 == dic2

easy/其他/258_Add Digits.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
3+
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
4+
5+
For example:
6+
7+
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
8+
9+
Follow up:
10+
Could you do it without any loop/recursion in O(1) runtime?
11+
12+
Credits:
13+
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
14+
15+
"""
16+
"""
17+
this method depends on the truth:
18+
19+
N=(a[0] * 1 + a[1] * 10 + ...a[n] * 10 ^n),and a[0]...a[n] are all between [0,9]
20+
21+
we set M = a[0] + a[1] + ..a[n]
22+
23+
and another truth is that:
24+
25+
1 % 9 = 1
26+
27+
10 % 9 = 1
28+
29+
100 % 9 = 1
30+
31+
so N % 9 = a[0] + a[1] + ..a[n]
32+
33+
means N % 9 = M
34+
35+
so N = M (% 9)
36+
37+
as 9 % 9 = 0,so we can make (n - 1) % 9 + 1 to help us solve the problem when n is 9.as N is 9, ( 9 - 1) % 9 + 1 = 9
38+
"""
39+
class Solution(object):
40+
def addDigits(self, num):
41+
"""
42+
:type num: int
43+
:rtype: int
44+
"""
45+
return (num % 9 or 9) if num else 0
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
3+
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
4+
5+
Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad.
6+
7+
You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
8+
9+
"""
10+
11+
"""典型的二分搜索"""
12+
13+
"""值得注意的地方
14+
Before this problem, I have always use
15+
16+
mid = (start+end)) / 2;
17+
To get the middle value, but this can caused OVERFLOW !
18+
19+
when start and end are all about INT_MAX , then (start+end) of course will be overflow !
20+
21+
To avoid the problem we can use
22+
23+
mid = start+(end-start)/2;
24+
25+
"""
26+
27+
# The isBadVersion API is already defined for you.
28+
# @param version, an integer
29+
# @return a bool
30+
# def isBadVersion(version):
31+
32+
class Solution(object):
33+
def firstBadVersion(self, n):
34+
"""
35+
:type n: int
36+
:rtype: int
37+
"""
38+
left = 0
39+
right = n-1
40+
while left <= right:
41+
mid = (left + right) // 2
42+
if isBadVersion(mid):
43+
right = mid - 1
44+
else:
45+
left = mid + 1
46+
return left

easy/数组/268_Missing Number.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
3+
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
4+
5+
For example,
6+
Given nums = [0, 1, 3] return 2.
7+
8+
Note:
9+
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
10+
11+
"""
12+
"""
13+
my solution
14+
根据等差数列求和公式即可
15+
"""
16+
17+
18+
class Solution(object):
19+
def missingNumber(self, nums):
20+
"""
21+
:type nums: List[int]
22+
:rtype: int
23+
"""
24+
n = len(nums)
25+
return n * (n + 1) / 2 - sum(nums)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
3+
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
4+
5+
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
6+
7+
_______6______
8+
/ \
9+
___2__ ___8__
10+
/ \ / \
11+
0 _4 7 9
12+
/ \
13+
3 5
14+
For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
15+
"""
16+
17+
# Definition for a binary tree node.
18+
# class TreeNode(object):
19+
# def __init__(self, x):
20+
# self.val = x
21+
# self.left = None
22+
# self.right = None
23+
"""考虑二叉搜索树的性质,左子树的值都比根结点小,右子树的值都比根结点大,不断的迭代循环"""
24+
class Solution(object):
25+
def lowestCommonAncestor(self, root, p, q):
26+
"""
27+
:type root: TreeNode
28+
:type p: TreeNode
29+
:type q: TreeNode
30+
:rtype: TreeNode
31+
"""
32+
while root:
33+
if root.val < p.val and root.val < q.val:
34+
root = root.right
35+
elif root.val > p.val and root.val > q.val:
36+
root = root.left
37+
else:
38+
return root

easy/树/257_Binary Tree Paths.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
3+
Given a binary tree, return all root-to-leaf paths.
4+
5+
For example, given the following binary tree:
6+
7+
1
8+
/ \
9+
2 3
10+
\
11+
5
12+
All root-to-leaf paths are:
13+
14+
["1->2->5", "1->3"]
15+
16+
"""
17+
18+
"""
19+
my solution 递归的思想
20+
"""
21+
22+
23+
# Definition for a binary tree node.
24+
# class TreeNode(object):
25+
# def __init__(self, x):
26+
# self.val = x
27+
# self.left = None
28+
# self.right = None
29+
30+
class Solution(object):
31+
def binaryTreePaths(self, root):
32+
"""
33+
:type root: TreeNode
34+
:rtype: List[str]
35+
"""
36+
res = []
37+
if not root:
38+
return res
39+
if not root.left and not root.right:
40+
return [str(root.val)]
41+
42+
leftPath = self.binaryTreePaths(root.left)
43+
rightPath = self.binaryTreePaths(root.right)
44+
for item in leftPath:
45+
res.append("->".join([str(root.val), item]))
46+
for item in rightPath:
47+
res.append("->".join([str(root.val), item]))
48+
return res

easy/链表相关/234_Palindrome Linked List.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44
"""
55
画图理解更好,使用两个指针,当快的指针跑到头的时候,跑的慢的指针刚好到一半,随后将后一半元素的指向改变,随后循环判断元素是否相等
6-
相当于先把两个
6+
相当于先把一个链表分成两个
77
"""
88

99
# Definition for singly-linked list.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""
2+
3+
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
4+
5+
Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.
6+
7+
"""
8+
"""刚开始还以为这道题错了,后来看了答案豁然开朗,虽然只能知道要删除的节点,没必要直接删除它,用它后一个节点的值替换掉他的值就好了,还是有点神奇的,自己思路太闭塞"""
9+
10+
# Definition for singly-linked list.
11+
# class ListNode(object):
12+
# def __init__(self, x):
13+
# self.val = x
14+
# self.next = None
15+
16+
class Solution(object):
17+
def deleteNode(self, node):
18+
"""
19+
:type node: ListNode
20+
:rtype: void Do not return anything, modify node in-place instead.
21+
"""
22+
node.val = node.next.val
23+
node.next = node.next.next
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
3+
Write a program to check whether a given number is an ugly number.
4+
5+
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.
6+
7+
Note that 1 is typically treated as an ugly number.
8+
9+
"""
10+
11+
"""my solution"""
12+
13+
class Solution(object):
14+
def isUgly(self, num):
15+
"""
16+
:type num: int
17+
:rtype: bool
18+
"""
19+
if num==0:
20+
return False
21+
if num==1:
22+
return True
23+
while num % 2 == 0:
24+
num /= 2
25+
while num % 3 == 0:
26+
num /= 3
27+
while num % 5 == 0:
28+
num /=5
29+
return num == 1

0 commit comments

Comments
 (0)