Skip to content

Commit 0d91074

Browse files
committed
前200道题中的简单题
1 parent ec1c5a1 commit 0d91074

11 files changed

+404
-0
lines changed

easy/141_Linked List Cycle.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
3+
Given a linked list, determine if it has a cycle in it.
4+
5+
Follow up:
6+
Can you solve it without using extra space?
7+
8+
"""
9+
10+
"""判断列表中是否有环,只需设置两个指针,一个跑的快,一个跑的慢,当跑的快的追上跑的慢的的时候,肯定就又环,如果碰到next不存在的情况,
11+
抛出异常,肯定没有环
12+
"""
13+
14+
# Definition for singly-linked list.
15+
# class ListNode(object):
16+
# def __init__(self, x):
17+
# self.val = x
18+
# self.next = None
19+
20+
class Solution(object):
21+
def hasCycle(self, head):
22+
"""
23+
:type head: ListNode
24+
:rtype: bool
25+
"""
26+
if not head:
27+
return False
28+
walker = head
29+
runner = head.next
30+
try:
31+
while walker != runner:
32+
walker = walker.next
33+
runner = runner.next.next
34+
return True
35+
except:
36+
return False

easy/155_Min Stack.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
3+
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
4+
5+
push(x) -- Push element x onto stack.
6+
pop() -- Removes the element on top of the stack.
7+
top() -- Get the top element.
8+
getMin() -- Retrieve the minimum element in the stack.
9+
10+
11+
"""
12+
13+
14+
class MinStack(object):
15+
def __init__(self):
16+
self.q = []
17+
18+
# @param x, an integer
19+
# @return an integer
20+
def push(self, x):
21+
curMin = self.getMin()
22+
if curMin == None or x < curMin:
23+
curMin = x
24+
self.q.append((x, curMin));
25+
26+
# @return nothing
27+
def pop(self):
28+
self.q.pop()
29+
30+
# @return an integer
31+
def top(self):
32+
if len(self.q) == 0:
33+
return None
34+
else:
35+
return self.q[len(self.q) - 1][0]
36+
37+
# @return an integer
38+
def getMin(self):
39+
if len(self.q) == 0:
40+
return None
41+
else:
42+
return self.q[len(self.q) - 1][1]
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
3+
Write a program to find the node at which the intersection of two singly linked lists begins.
4+
5+
6+
For example, the following two linked lists:
7+
8+
A: a1 → a2
9+
10+
c1 → c2 → c3
11+
12+
B: b1 → b2 → b3
13+
begin to intersect at node c1.
14+
15+
16+
Notes:
17+
18+
If the two linked lists have no intersection at all, return null.
19+
The linked lists must retain their original structure after the function returns.
20+
You may assume there are no cycles anywhere in the entire linked structure.
21+
Your code should preferably run in O(n) time and use only O(1) memory.
22+
Credits:
23+
Special thanks to @stellari for adding this problem and creating all test cases.
24+
25+
"""
26+
27+
"""两个指针,如果两个链表有交集的话,肯定会跑到同一个节点,没有交集,则最后的pa和pb都是None,都是None也能跳出循环
28+
长度差异也考虑进去了
29+
"""
30+
31+
32+
# Definition for singly-linked list.
33+
# class ListNode(object):
34+
# def __init__(self, x):
35+
# self.val = x
36+
# self.next = None
37+
38+
class Solution(object):
39+
def getIntersectionNode(self, headA, headB):
40+
"""
41+
:type head1, head1: ListNode
42+
:rtype: ListNode
43+
"""
44+
if not headA or not headB:
45+
return None
46+
47+
else:
48+
pa = headA
49+
pb = headB
50+
while pa is not pb:
51+
pa = headB if pa == None else pa.next
52+
pb = headA if pb == None else pb.next
53+
return pa
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
3+
Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
4+
5+
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
6+
7+
You may assume that each input would have exactly one solution and you may not use the same element twice.
8+
9+
Input: numbers={2, 7, 11, 15}, target=9
10+
Output: index1=1, index2=2
11+
12+
"""
13+
14+
"""仍然是字典的思路"""
15+
class Solution(object):
16+
def twoSum(self, numbers, target):
17+
"""
18+
:type numbers: List[int]
19+
:type target: int
20+
:rtype: List[int]
21+
"""
22+
res = dict()
23+
for i in range(0,len(numbers)):
24+
sub = target - numbers[i]
25+
if sub in res.keys():
26+
return [res[sub]+1,i+1]
27+
else:
28+
res[numbers[i]] = i
29+
return []
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
3+
Given a positive integer, return its corresponding column title as appear in an Excel sheet.
4+
5+
For example:
6+
7+
1 -> A
8+
2 -> B
9+
3 -> C
10+
...
11+
26 -> Z
12+
27 -> AA
13+
28 -> AB
14+
15+
16+
"""
17+
"""
18+
my solution,这里要注意,不能直接除或者余26,首先要减一,举个例子就可以知道
19+
"""
20+
21+
class Solution(object):
22+
def convertToTitle(self, n):
23+
"""
24+
:type n: int
25+
:rtype: str
26+
"""
27+
res = ''
28+
while n:
29+
res = chr((n - 1) % 26 + 65) + res
30+
n = (n - 1) / 26
31+
return res

easy/169_Majority Element.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
3+
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
4+
5+
You may assume that the array is non-empty and the majority element always exist in the array.
6+
7+
"""
8+
9+
"""
10+
my solution :使用一个字典
11+
"""
12+
13+
class Solution(object):
14+
def majorityElement(self, nums):
15+
"""
16+
:type nums: List[int]
17+
:rtype: int
18+
"""
19+
res = dict()
20+
for i in nums:
21+
if i in res.keys():
22+
res[i] = res[i]+1
23+
else:
24+
res[i] = 1
25+
if res[i] > len(nums)/2:
26+
return i
27+
28+
29+
"""other solution
30+
常数级的空间复杂度,因为最多的元素超过一半,所以该元素的count值减去其他所有元素的count值一定大于0,所以最后的cand一定是出现次数最多的元素
31+
"""
32+
33+
class Solution(object):
34+
def majorityElement(self, nums):
35+
"""
36+
:type nums: List[int]
37+
:rtype: int
38+
"""
39+
cand = nums[0]
40+
count = 1
41+
for i in nums[1:]:
42+
if count == 0:
43+
cand,count = i,1
44+
else:
45+
if i == cand:
46+
count = count + 1
47+
else:
48+
count = count - 1
49+
return cand
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
3+
Related to question Excel Sheet Column Title
4+
5+
Given a column title as appear in an Excel sheet, return its corresponding column number.
6+
7+
For example:
8+
9+
A -> 1
10+
B -> 2
11+
C -> 3
12+
...
13+
Z -> 26
14+
AA -> 27
15+
AB -> 28
16+
17+
"""
18+
19+
"""
20+
my solution
21+
"""
22+
23+
24+
class Solution(object):
25+
def titleToNumber(self, s):
26+
"""
27+
:type s: str
28+
:rtype: int
29+
"""
30+
count = 0
31+
for i in s:
32+
count = count * 26 + (ord(i) - 65) % 26 + 1
33+
return count
34+
35+
"""other solution : 1 line"""
36+
37+
def titleToNumber(self, s):
38+
return reduce(lambda x,y:x*26+y,map(lambda x:ord(x)-ord('A')+1,s))
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
3+
Given an integer n, return the number of trailing zeroes in n!.
4+
5+
Note: Your solution should be in logarithmic time complexity.
6+
7+
Credits:
8+
Special thanks to @ts for adding this problem and creating all test cases.
9+
10+
"""
11+
12+
13+
class Solution(object):
14+
def trailingZeroes(self, n):
15+
zeroCnt = 0
16+
while n > 0:
17+
n = n / 5;
18+
zeroCnt += n
19+
20+
return zeroCnt

easy/189_Rotate Array.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
3+
Rotate an array of n elements to the right by k steps.
4+
5+
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
6+
7+
Note:
8+
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
9+
10+
[show hint]
11+
12+
Hint:
13+
Could you do it in-place with O(1) extra space?
14+
Related problem: Reverse Words in a String II
15+
16+
"""
17+
18+
"""my solution"""
19+
20+
21+
class Solution(object):
22+
def rotate(self, nums, k):
23+
"""
24+
:type nums: List[int]
25+
:type k: int
26+
:rtype: void Do not return anything, modify nums in-place instead.
27+
"""
28+
n = len(nums)
29+
nums[:] = nums[n - k:] + nums[:n - k]
30+
31+

easy/190_Reverse Bits.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
3+
Reverse bits of a given 32 bits unsigned integer.
4+
5+
For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000).
6+
7+
Follow up:
8+
If this function is called many times, how would you optimize it?
9+
10+
Related problem: Reverse Integer
11+
12+
"""
13+
14+
"""
15+
my solution
16+
17+
"""
18+
19+
class Solution:
20+
# @param n, an integer
21+
# @return an integer
22+
def reverseBits(self, n):
23+
stack = []
24+
while n:
25+
stack.append(n%2)
26+
n /= 2
27+
while(len(stack)<32):
28+
stack.append(0)
29+
for i in range(0,len(stack)):
30+
n = n*2+stack[i]
31+
return n

0 commit comments

Comments
 (0)