mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2026-02-03 10:53:25 +08:00
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
This commit is contained in:
@@ -21,7 +21,7 @@
|
||||
|
||||
## 思路
|
||||
|
||||
如果想把这道题目做到极致,就不要只用额外的辅助空间了! (不过使用Java刷题的录友,一定要使用辅助空间,因为Java里的string不能修改)
|
||||
如果想把这道题目做到极致,就不要只用额外的辅助空间了! (不过使用Java和Python刷题的录友,一定要使用辅助空间,因为Java和Python里的string不能修改)
|
||||
|
||||
首先扩充数组到每个数字字符替换成 "number" 之后的大小。
|
||||
|
||||
@@ -215,6 +215,46 @@ public class Main {
|
||||
}
|
||||
```
|
||||
|
||||
### Python:
|
||||
```python
|
||||
class Solution(object):
|
||||
def subsitute_numbers(self, s):
|
||||
"""
|
||||
:type s: str
|
||||
:rtype: str
|
||||
"""
|
||||
|
||||
count = sum(1 for char in s if char.isdigit()) # 统计数字的个数
|
||||
expand_len = len(s) + (count * 5) # 计算扩充后字符串的大小, x->number, 每有一个数字就要增加五个长度
|
||||
res = [''] * expand_len
|
||||
|
||||
new_index = expand_len - 1 # 指向扩充后字符串末尾
|
||||
old_index = len(s) - 1 # 指向原字符串末尾
|
||||
|
||||
while old_index >= 0: # 从后往前, 遇到数字替换成“number”
|
||||
if s[old_index].isdigit():
|
||||
res[new_index-5:new_index+1] = "number"
|
||||
new_index -= 6
|
||||
else:
|
||||
res[new_index] = s[old_index]
|
||||
new_index -= 1
|
||||
old_index -= 1
|
||||
|
||||
return "".join(res)
|
||||
|
||||
if __name__ == "__main__":
|
||||
solution = Solution()
|
||||
|
||||
while True:
|
||||
try:
|
||||
s = input()
|
||||
result = solution.subsitute_numbers(s)
|
||||
print(result)
|
||||
except EOFError:
|
||||
break
|
||||
|
||||
```
|
||||
|
||||
### Go:
|
||||
````go
|
||||
package main
|
||||
|
||||
@@ -176,6 +176,81 @@ int main() {
|
||||
|
||||
### Java
|
||||
|
||||
```java
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Main {
|
||||
private static int[] father;
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
int pointNum = scanner.nextInt();
|
||||
father = new int[pointNum + 1];
|
||||
init();
|
||||
for (int i = 0; i < pointNum; i++) {
|
||||
join(scanner.nextInt(), scanner.nextInt());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 并查集初始化
|
||||
*/
|
||||
private static void init() {
|
||||
for (int i = 1; i < father.length; i++) {
|
||||
// 让每个元素指向自己
|
||||
father[i] = i;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 并查集寻根
|
||||
*
|
||||
* @param u
|
||||
* @return
|
||||
*/
|
||||
private static int find(int u) {
|
||||
// 判断 u 是否等于自己,如果是的话,直接返回自己
|
||||
// 如果不等于自己,就寻找根,寻找的时候,反复进行路径压缩
|
||||
return u == father[u] ? u : (father[u] = find(father[u]));
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断 u 和 v 是否同根
|
||||
*
|
||||
* @param u
|
||||
* @param v
|
||||
* @return
|
||||
*/
|
||||
private static boolean isSame(int u, int v) {
|
||||
return find(u) == find(v);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加 边 到并查集,v 指向 u
|
||||
*
|
||||
* @param u
|
||||
* @param v
|
||||
*/
|
||||
private static void join(int u, int v) {
|
||||
// --if-- 如果两个点已经同根,说明他们的信息已经存储到并查集中了,直接返回即可
|
||||
// 寻找u的根
|
||||
int uRoot = find(u);
|
||||
// 寻找v的根
|
||||
int vRoot = find(v);
|
||||
if (uRoot == vRoot) {
|
||||
// --if-- 如果u,v的根相同,说明两者已经连接了,直接输出
|
||||
System.out.println(u + " " + v);
|
||||
return;
|
||||
}
|
||||
// --if-- 将信息添加到并查集
|
||||
father[vRoot] = uRoot;
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Python
|
||||
|
||||
```python
|
||||
|
||||
Reference in New Issue
Block a user