Skip to content

Commit 7be59b1

Browse files
author
lifeiyang
committed
2022-11-22-17:07:25
1 parent 50308b7 commit 7be59b1

File tree

5 files changed

+154
-0
lines changed

5 files changed

+154
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,58 @@
1+
import string
2+
3+
14
# leetcode submit region begin(Prohibit modification and deletion)
25
class Solution:
6+
def build_kmp(self, needle):
7+
# 定义 dp[i][j] 为: 在第 i 个位置,遇到 j 字母,应该去的位置
8+
dp = {}
9+
for i in range(len(needle)):
10+
dp[i] = {}
11+
for j in string.ascii_lowercase:
12+
dp[i][j] = 0
13+
14+
# 基础情况,在第 0 个位置,遇到第 0 个字母,应该跳转到第 1 个位置
15+
dp[0][needle[0]] = 1
16+
17+
# X 为跟随在 i 后边的重叠子问题状态
18+
X = 0
19+
for i in range(1, len(needle)):
20+
for j in string.ascii_lowercase:
21+
if j == needle[i]:
22+
dp[i][j] = i + 1
23+
else:
24+
dp[i][j] = dp[X][j]
25+
X = dp[X][needle[i]]
26+
return dp
27+
28+
def strStrKMP(self, haystack: str, needle: str) -> int:
29+
kmp_pattern_dp = self.build_kmp(needle)
30+
m, n = len(needle), len(haystack)
31+
j = 0
32+
for idx, char in enumerate(haystack):
33+
j = kmp_pattern_dp[j][char]
34+
if j == m:
35+
return idx - m + 1
36+
37+
return -1
38+
339
def strStr(self, haystack: str, needle: str) -> int:
40+
res = -1
41+
m, n = len(haystack), len(needle)
42+
if m < n:
43+
return res
44+
45+
for idx in range(m - n + 1):
46+
if haystack[idx:idx + n] == needle:
47+
return idx
48+
return res
49+
50+
451
# leetcode submit region end(Prohibit modification and deletion)
52+
53+
54+
if __name__ == "__main__":
55+
solution = Solution()
56+
print(solution.strStr("a", "a"), 0)
57+
print(solution.strStr("sadbutsad", "sad"), 0)
58+
print(solution.strStr("leetcode", "leeto"), -1)
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# !/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright @2022 AI, ZHIHU Inc. (zhihu.com)
5+
#
6+
# @author: lifeiyang <[email protected]>
7+
# @date: 2022/11/24
8+
#
9+
""""""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# 实现 kmeans
2+
3+
输入数据为:
4+
5+
data_list = [[0,5],[0,6],[4,0],[5,0]]
6+
k =2
7+
8+
对应图为:
9+
10+
![](https://cdn.lifeiyang.cn/img_2022/202211241634692.png)
11+
12+
输出的结果为:
13+
[[0,5],[0,6]][[4,0],[5,0]]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# !/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright @2022 AI, ZHIHU Inc. (zhihu.com)
5+
#
6+
# @author: lifeiyang <[email protected]>
7+
# @date: 2022/11/24
8+
#
9+
""""""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# !/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright @2022 AI, ZHIHU Inc. (zhihu.com)
5+
#
6+
# @author: lifeiyang <[email protected]>
7+
# @date: 2022/11/24
8+
#
9+
""""""
10+
import math
11+
12+
13+
class Solution:
14+
def distance(self, node1, node2):
15+
return math.sqrt(math.pow(node1[0] - node2[0], 2) + math.pow(node1[1] - node2[1], 2))
16+
17+
def point_mean(self, point_list):
18+
point_x = sum([point[0] for point in point_list]) / len(point_list)
19+
point_y = sum([point[1] for point in point_list]) / len(point_list)
20+
return (point_x, point_y)
21+
22+
def kmeans(self, data_list, k):
23+
# 1、随机初始化点
24+
k_cluster = {}
25+
for i in range(k):
26+
k_cluster[tuple(data_list[i])] = []
27+
28+
# 2、根据距离加入质心
29+
for point in data_list:
30+
min_distance = math.inf
31+
min_kernel = None
32+
for kernel in k_cluster:
33+
if min_distance > self.distance(point, kernel):
34+
min_distance = self.distance(point, kernel)
35+
min_kernel = kernel
36+
37+
k_cluster[min_kernel].append(point)
38+
39+
# 3、开始循环迭代
40+
k_kernel_old = k_cluster.copy()
41+
while True:
42+
# 新一轮的迭代
43+
# 1、寻找当前的质心
44+
k_cluster = {}
45+
for kernel in k_kernel_old:
46+
kernel_mean = self.point_mean(k_kernel_old[kernel])
47+
k_cluster[kernel_mean] = []
48+
49+
# 2、根据距离加入质心
50+
for point in data_list:
51+
min_distance = math.inf
52+
min_kernel = None
53+
for kernel in k_cluster:
54+
if min_distance > self.distance(point, kernel):
55+
min_distance = self.distance(point, kernel)
56+
min_kernel = kernel
57+
k_cluster[min_kernel].append(point)
58+
59+
if k_cluster == k_kernel_old:
60+
print("final kmeans:", k_cluster)
61+
break
62+
else:
63+
k_kernel_old = k_cluster.copy()
64+
print("now kmeans:", k_cluster)
65+
66+
67+
if __name__ == "__main__":
68+
solution = Solution()
69+
print(solution.kmeans([[0, 5], [0, 6], [4, 0], [5, 0]], 2))

0 commit comments

Comments
 (0)