Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦境迷离
Cs Summary Reflection
提交
e41f84b7
C
Cs Summary Reflection
项目概览
梦境迷离
/
Cs Summary Reflection
12 个月 前同步成功
通知
4
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
Cs Summary Reflection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e41f84b7
编写于
5月 01, 2020
作者:
梦境迷离
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
rust leetcode 14
上级
668238ce
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
176 addition
and
0 deletion
+176
-0
rust-leetcode/README.md
rust-leetcode/README.md
+79
-0
rust-leetcode/src/Solution.rs
rust-leetcode/src/Solution.rs
+97
-0
未找到文件。
rust-leetcode/README.md
浏览文件 @
e41f84b7
...
...
@@ -1377,3 +1377,82 @@ impl Solution {
}
}
```
*
14 长公共前缀
```
rust
impl
Solution
{
//0 ms, 2.1 MB
pub
fn
longest_common_prefix
(
strs
:
Vec
<
String
>
)
->
String
{
//选出最小的字符串w,使用w的所有子串去匹配strs中的单词
let
mut
min_word
:
&
String
=
&
""
.to_owned
();
let
mut
min_length
=
usize
::
max_value
();
if
strs
.is_empty
()
{
return
min_word
.clone
();
}
let
mut
result
:
&
str
=
""
;
strs
.iter
()
.for_each
(|
word
|
{
if
min_length
>
word
.len
()
{
min_length
=
word
.len
();
min_word
=
word
;
}
});
while
min_length
>
0
{
let
mut
sub_word
=
&
min_word
[
0
..
min_length
];
let
mut
is_max
=
true
;
for
w
in
strs
.iter
()
{
if
w
.starts_with
(
sub_word
)
==
false
{
is_max
=
false
;
break
;
}
}
if
is_max
{
result
=
sub_word
;
break
;
}
min_length
-=
1
;
}
result
.to_owned
()
}
//0 ms, 2.1 MB
pub
fn
longest_common_prefix2
(
strs
:
Vec
<
String
>
)
->
String
{
let
strs
=
&
strs
;
let
mut
result
:
String
=
""
.to_owned
();
if
strs
.is_empty
()
{
return
result
;
}
//选取第一个单词w,对w的长度从大到小进行切片,将切片与所有单词进行匹配
result
=
strs
[
0
]
.clone
();
for
(
index
,
word
)
in
strs
.iter
()
.enumerate
()
{
while
!
word
.starts_with
(
result
.as_str
())
{
result
=
result
[
0
..
result
.len
()
-
1
]
.to_owned
();
if
result
.len
()
==
0
{
return
""
.to_owned
();
}
}
}
result
}
//4 ms, 2.1 MB
pub
fn
longest_common_prefix3
(
strs
:
Vec
<
String
>
)
->
String
{
let
strs
=
&
strs
;
let
mut
result
:
String
=
""
.to_owned
();
if
strs
.is_empty
()
{
return
result
;
}
//选取第一个单词w,与其他字符串依次比较对应位置上的字符
let
word
=
&
strs
[
0
]
.clone
();
let
init_word
:
Vec
<
char
>
=
word
.chars
()
.collect
();
for
i
in
0
..
init_word
.len
()
{
let
mut
c
:
char
=
init_word
[
i
];
for
j
in
1
..
strs
.len
()
{
let
cs
:
Vec
<
char
>
=
strs
[
j
]
.chars
()
.collect
();
if
i
<
cs
.len
()
&&
c
!=
cs
[
i
]
||
i
==
cs
.len
()
{
return
word
[
0
..
i
]
.to_owned
();
}
}
}
word
.to_owned
()
}
}
```
\ No newline at end of file
rust-leetcode/src/Solution.rs
浏览文件 @
e41f84b7
...
...
@@ -1852,6 +1852,102 @@ fn leetcode_500() {
print_vec_string
(
ret
);
}
///最长公共前缀
fn
leetcode_14
()
{
println!
(
"leetcode_14"
);
impl
Solution
{
//0 ms, 2.1 MB
pub
fn
longest_common_prefix
(
strs
:
Vec
<
String
>
)
->
String
{
//选出最小的字符串w,使用w的所有子串去匹配strs中的单词
let
mut
min_word
:
&
String
=
&
""
.to_owned
();
let
mut
min_length
=
usize
::
max_value
();
if
strs
.is_empty
()
{
return
min_word
.clone
();
}
let
mut
result
:
&
str
=
""
;
strs
.iter
()
.for_each
(|
word
|
{
if
min_length
>
word
.len
()
{
min_length
=
word
.len
();
min_word
=
word
;
}
});
while
min_length
>
0
{
let
mut
sub_word
=
&
min_word
[
0
..
min_length
];
let
mut
is_max
=
true
;
for
w
in
strs
.iter
()
{
if
w
.starts_with
(
sub_word
)
==
false
{
is_max
=
false
;
break
;
}
}
if
is_max
{
result
=
sub_word
;
break
;
}
min_length
-=
1
;
}
result
.to_owned
()
}
//0 ms, 2.1 MB
pub
fn
longest_common_prefix2
(
strs
:
Vec
<
String
>
)
->
String
{
let
strs
=
&
strs
;
let
mut
result
:
String
=
""
.to_owned
();
if
strs
.is_empty
()
{
return
result
;
}
//选取第一个单词w,对w的长度从大到小进行切片,将切片与所有单词进行匹配
result
=
strs
[
0
]
.clone
();
for
(
index
,
word
)
in
strs
.iter
()
.enumerate
()
{
while
!
word
.starts_with
(
result
.as_str
())
{
result
=
result
[
0
..
result
.len
()
-
1
]
.to_owned
();
if
result
.len
()
==
0
{
return
""
.to_owned
();
}
}
}
result
}
//4 ms, 2.1 MB
pub
fn
longest_common_prefix3
(
strs
:
Vec
<
String
>
)
->
String
{
let
strs
=
&
strs
;
let
mut
result
:
String
=
""
.to_owned
();
if
strs
.is_empty
()
{
return
result
;
}
//选取第一个单词w,与其他字符串依次比较对应位置上的字符
let
word
=
&
strs
[
0
]
.clone
();
let
init_word
:
Vec
<
char
>
=
word
.chars
()
.collect
();
for
i
in
0
..
init_word
.len
()
{
let
mut
c
:
char
=
init_word
[
i
];
for
j
in
1
..
strs
.len
()
{
let
cs
:
Vec
<
char
>
=
strs
[
j
]
.chars
()
.collect
();
if
i
<
cs
.len
()
&&
c
!=
cs
[
i
]
||
i
==
cs
.len
()
{
return
word
[
0
..
i
]
.to_owned
();
}
}
}
word
.to_owned
()
}
}
let
ret
=
Solution
::
longest_common_prefix
(
vec!
[
"flower"
.to_owned
(),
"flow"
.to_owned
(),
"flight"
.to_owned
()]);
let
ret2
=
Solution
::
longest_common_prefix
(
vec!
[]);
println!
(
"{}"
,
ret
);
println!
(
"{}"
,
ret2
);
let
ret
=
Solution
::
longest_common_prefix2
(
vec!
[
"flower"
.to_owned
(),
"flow"
.to_owned
(),
"flight"
.to_owned
()]);
let
ret2
=
Solution
::
longest_common_prefix2
(
vec!
[]);
println!
(
"{}"
,
ret
);
println!
(
"{}"
,
ret2
);
let
ret
=
Solution
::
longest_common_prefix3
(
vec!
[
"aa"
.to_owned
(),
"a"
.to_owned
()]);
let
ret2
=
Solution
::
longest_common_prefix3
(
vec!
[]);
println!
(
"{}"
,
ret
);
println!
(
"{}"
,
ret2
);
}
///所有方法调用
pub
fn
solutions
()
{
interview_58_2
();
...
...
@@ -1909,6 +2005,7 @@ pub fn solutions() {
leetcode_13
();
leetcode_876
();
leetcode_500
();
leetcode_14
();
}
fn
print_vec
(
nums
:
Vec
<
i32
>
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录