Skip to content

Commit 9802039

Browse files
committed
add 134, 135
1 parent c2034d7 commit 9802039

6 files changed

Lines changed: 184 additions & 2 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Solution(object):
6+
def singleNumber(self, nums):
7+
"""
8+
:type nums: List[int]
9+
:rtype: int
10+
"""
11+
num = nums[0]
12+
for i in nums[1:]:
13+
num = num ^ i
14+
return num
15+
16+
"""
17+
[1]
18+
[1,2,3,4,4,3,2]
19+
"""

DepthFirstSearch/README.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,40 @@
1-
深度优先搜索策略
1+
深度优先搜索
22

33

4+
WikiPedia 里对 DFS 的说明如下:
45

6+
> Depth-first search (DFS) is an algorithm for traversing or searching tree or graph data structures. One starts at the root (selecting some arbitrary node as the root in the case of a graph) and explores as far as possible along each branch before backtracking.
57
68

7-
注意复位当前节点属性。
9+
# 一般化实现
10+
11+
递归实现
12+
13+
14+
def DFS(G, v):
15+
visited[v] = True
16+
for cur_node in v.adjacentNodes():
17+
if not visited[cur_node]:
18+
# Do something for the current node
19+
DFS(G, cur_node)
20+
21+
栈实现
22+
23+
def DFS(G, v):
24+
nodes = stack()
25+
nodes.push(v)
26+
while not nodes.empty():
27+
cur_node = nodes.pop()
28+
# Do something for the current node
29+
visited[cur_node] = True
30+
for w in cur_node.adjacentNodes():
31+
if not visited[w]:
32+
nodes.push(w)
33+
34+
[Clone Graph](https://leetcode.com/problems/clone-graph/) 是一个图的复制问题,可以用 DFS 来遍历图中所有的顶点。
35+
36+
37+
更多阅读
38+
39+
[Depth-first search](https://en.wikipedia.org/wiki/Depth-first_search)
840

DynamicProgramming/135_Candy.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Solution(object):
6+
def candy(self, ratings):
7+
candies = [0 for i in ratings]
8+
candies[0] = 1
9+
children_nums = len(ratings)
10+
11+
# Scan from left to right
12+
for i in range(children_nums-1):
13+
if ratings[i+1] > ratings[i]:
14+
candies[i+1] = candies[i] + 1
15+
else:
16+
candies[i+1] = 1
17+
18+
minimum_candies = 0
19+
# Scan from right to left
20+
for i in range(children_nums-1, 0, -1):
21+
if ratings[i-1] > ratings[i]:
22+
candies[i-1] = max(candies[i] + 1, candies[i-1])
23+
else:
24+
candies[i-1] = max(1, candies[i-1])
25+
minimum_candies += candies[i]
26+
27+
return minimum_candies + candies[0]
28+
29+
30+
"""
31+
[0]
32+
[2,2,1]
33+
[3,4,8,6,7]
34+
[4,3,8,6,7]
35+
[1,2,4,4,3]
36+
[1,2,4,4,5]
37+
[4,4,4,4,4]
38+
"""

Graph/133_CloneGraph.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
# Definition for a undirected graph node
6+
# class UndirectedGraphNode(object):
7+
# def __init__(self, x):
8+
# self.label = x
9+
# self.neighbors = []
10+
11+
class Solution(object):
12+
def cloneGraph(self, node):
13+
if not node:
14+
return None
15+
16+
copyed_node_pair = {}
17+
copy_head = UndirectedGraphNode(node.label)
18+
copy_head.neighbors = []
19+
copyed_node_pair[node] = copy_head
20+
21+
nodes_stack = []
22+
nodes_stack.append(node)
23+
while nodes_stack:
24+
one_node = nodes_stack.pop()
25+
26+
for neighbor in one_node.neighbors:
27+
if neighbor not in copyed_node_pair:
28+
copy_node = UndirectedGraphNode(neighbor.label)
29+
copy_node.neighbors = []
30+
copyed_node_pair[neighbor] = copy_node
31+
nodes_stack.append(neighbor)
32+
33+
copyed_node_pair[one_node].neighbors.append(
34+
copyed_node_pair[neighbor])
35+
36+
return copy_head
37+
38+
"""
39+
{0,0,0}
40+
{0,1,2#1,2#2,2}
41+
"""

Greedy/134_GasStation.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#! /usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
# Refer to http://blog.csdn.net/kenden23/article/details/14106137
4+
5+
6+
class Solution(object):
7+
"""
8+
Consider we start at gas station i, and until j we firstly run out of gas.
9+
10+
That's say remain(i,j) = R(i) + ... + R(j) < 0, R(i) >= 0, R(j) < 0
11+
and remain(i, m) >= 0, where i =< m < j,
12+
We assume R(k) = gas(k) - cost(k) here.
13+
14+
Further more, we can make sure remain(m+1, k) < 0.
15+
Just because remain(i,j) < 0 and remain(i, m) >= 0.
16+
So, next we just need to start from index k+1.
17+
18+
So, firstly find all the (i,j) pairs, but just need to record the last j.
19+
Then if there is an unique(it's guaranteed) solution, it must be (j+1)
20+
"""
21+
def canCompleteCircuit(self, gas, cost):
22+
station_num = len(gas)
23+
mark_station = -1
24+
all_remain = 0
25+
remain_gas = 0
26+
for i in range(station_num):
27+
all_remain += (gas[i]-cost[i])
28+
remain_gas += (gas[i]-cost[i])
29+
if remain_gas < 0:
30+
mark_station = i
31+
remain_gas = 0
32+
33+
if all_remain >= 0:
34+
return (mark_station + 1) % station_num
35+
else:
36+
return -1
37+
38+
return -1
39+
40+
"""
41+
[4]
42+
[5]
43+
[1,10,2,3,4,5,6]
44+
[2,4,3,4,5,6,7]
45+
[1,2,3,4,5,6,10]
46+
[1,2,2,3,4,15,4]
47+
[2,0,1,2,3,4,0]
48+
[0,1,0,0,0,0,11]
49+
"""

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,13 @@
118118
* 121. [Best Time to Buy and Sell Stock](DynamicProgramming/121_BestTimeToBuyAndSellStock.py)
119119
* 123. [Best Time to Buy and Sell Stock III](DynamicProgramming/123_BestTimeToBuyAndSellStockIII.py)
120120
* 132. [Palindrome Partitioning II](DynamicProgramming/132_PalindromePartitioningII.py)
121+
* 135. [Candy](DynamicProgramming/135_Candy.py)
121122

122123
# [Greedy](Greedy/)
123124

124125
* 055. Jump Game
125126
* 122. [Best Time to Buy and Sell Stock II](Greedy/122_BestTimeToBuyAndSellStockII.py)
127+
* 134. [Gas Station](Greedy/134_GasStation.py)
126128

127129
# [Stack](Stack/)
128130

@@ -155,6 +157,7 @@
155157
# [Bit Manipulation](BitManipulation/)
156158

157159
* 078. Subsets
160+
* 136. [Single Number](BitManipulation/136_SingleNumber.py)
158161

159162
# DFA
160163

0 commit comments

Comments
 (0)