上药三品,神与气精

曾因酒醉鞭名马 生怕情多累美人


  • 首页

  • 关于

  • 分类

  • 标签

  • 归档

  • 搜索

leetcode-801

发表于 2019-01-25 | 阅读次数:
字数统计: 0 | 阅读时长 ≈ 1

leetcode-799

发表于 2019-01-25 | 阅读次数:
字数统计: 84 | 阅读时长 ≈ 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

class Solution {
public:
double champagneTower(int poured, int query_row, int query_glass) {
double result[101][101] = {0.0};
result[0][0] = poured;
for (int i = 0; i < 100; i++) {
for (int j = 0; j <= i; j++) {
if (result[i][j] >= 1) {
result[i + 1][j] += (result[i][j] - 1) / 2.0;
result[i + 1][j + 1] += (result[i][j] - 1) / 2.0;
result[i][j] = 1;
}
}
}
return result[query_row][query_glass];
}
};

leetcode-798

发表于 2019-01-25 | 阅读次数:
字数统计: 53 | 阅读时长 ≈ 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14

class Solution(object):
def bestRotation(self, A):
"""
:type A: List[int]
:rtype: int
"""
N = len(A)
change = [1] * N
for i in xrange(N):
change[(i - A[i] + 1) % N] -= 1
for i in xrange(1, N):
change[i] += change[i - 1]
return change.index(max(change))

leetcode-797

发表于 2019-01-25 | 分类于 leetcode | 阅读次数:
字数统计: 95 | 阅读时长 ≈ 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

class Solution {
public:
void dfs(vector<vector<int>>& g, vector<vector<int>>& res, vector<int> path, int cur) {
path.push_back(cur);
if (cur == g.size() - 1) res.push_back(path);
else for (auto it: g[cur]) dfs(g, res, path, it);
}
vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& g) {
vector<vector<int>> paths;
vector<int> path;
dfs(g, paths, path, 0);
return paths;
}
};

ds之dfs

发表于 2019-01-24 | 分类于 data structure | 阅读次数:
字数统计: 87 | 阅读时长 ≈ 1

深度优先搜索 更符合计算机的习惯

也就是一般使用栈来处理问题(递归)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution(object):
def levelOrder(self, root):
if not root:
return []
self.result = []
self._dfs(root, 0)
return self.result

def _dfs(self, node, level):
if not node: return

if len(self.result) < level + 1:
self.result.append([])

self.result[level].append(node.val)

self._dfs(node.left, level+1)
self._dfs(node.right, level+1)
1…515253…109
John Cheung

John Cheung

improve your python skills

543 日志
33 分类
45 标签
RSS
GitHub Email
© 2020 John Cheung
本站访客数:
|
主题 — NexT.Pisces v5.1.4
博客全站共226.3k字