Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ad210a9

Browse files
author
ruislan
committed
solved q1816
1 parent f5a51ef commit ad210a9

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

‎src/q/mod.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ mod q1743;
639639
mod q1744;
640640
mod q1753;
641641
mod q1787;
642+
mod q1816;
642643
mod q1818;
643644
mod q1833;
644645
mod q1846;

‎src/q/q1816.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use crate::q::Solution;
2+
3+
#[allow(unused)]
4+
impl Solution {
5+
pub fn truncate_sentence(s: String, k: i32) -> String {
6+
// 方法1
7+
// 这个没啥好说的,就是根据空格分割然后取前k个就行了
8+
// AC 0ms 2.1mb 72/72
9+
s.split_ascii_whitespace()
10+
.enumerate()
11+
.filter(|(i, w)| *i < k as usize)
12+
.map(|(i, w)| w)
13+
.collect::<Vec<_>>()
14+
.join(" ")
15+
}
16+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /