Movatterモバイル変換


[0]ホーム

URL:


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

Commit2615e7b

Browse files
authored
Merge pull requestneetcode-gh#440 from tomijaga/main
Rust Solutions
2 parents798dc39 +825616c commit2615e7b

11 files changed

+313
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::{
2+
collections::HashSet,
3+
iter::FromIterator,
4+
};
5+
6+
implSolution{
7+
pubfnlongest_consecutive(nums:Vec<i32>) ->i32{
8+
letmut set:HashSet<i32> =HashSet::from_iter(nums.into_iter());
9+
10+
letmut max_cnt =0;
11+
12+
for nin&set{
13+
if !set.contains(&(n-1)){
14+
letmut next = n +1;
15+
letmut cnt =1;
16+
while set.contains(&next){
17+
cnt +=1;
18+
next+=1;
19+
}
20+
21+
max_cnt = max_cnt.max(cnt);
22+
}
23+
}
24+
25+
max_cnt
26+
}
27+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use std::collections::HashMap;
2+
3+
implSolution{
4+
pubfntwo_sum(nums:Vec<i32>,target:i32) ->Vec<i32>{
5+
letmut map =HashMap::new();
6+
7+
for(i, n)in nums.into_iter().enumerate(){
8+
let diff = target - n;
9+
10+
ifletSome(&j) = map.get(&diff){
11+
returnvec![iasi32, jasi32];
12+
}else{
13+
map.insert(n, i);
14+
}
15+
}
16+
17+
unreachable!()
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use std::collections::HashSet;
2+
3+
implSolution{
4+
pubfncontains_duplicate(nums:Vec<i32>) ->bool{
5+
letmut map =HashSet::new();
6+
7+
for nin nums.iter(){
8+
9+
if map.contains(n){
10+
returntrue;
11+
}
12+
13+
map.insert(n);
14+
};
15+
16+
false
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
implSolution{
2+
pubfnproduct_except_self(mutnums:Vec<i32>) ->Vec<i32>{
3+
letmut res =vec![1; nums.len()];
4+
5+
for iin(1..nums.len()){
6+
res[i] = nums[i -1]* res[i -1];
7+
}
8+
9+
letmut right =1;
10+
11+
for(i, n)in res.iter_mut().enumerate().rev(){
12+
*n =*n* right;
13+
right *= nums[i];
14+
}
15+
16+
res
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use std::collections::HashMap;
2+
3+
implSolution{
4+
pubfnis_anagram(s:String,t:String) ->bool{
5+
if(t.len() != s.len()){
6+
returnfalse;
7+
}
8+
9+
letmut map:HashMap<char,usize> =HashMap::new();
10+
11+
for(a, b)in s.chars().zip(t.chars()){
12+
*map.entry(a).or_default() +=1;
13+
*map.entry(b).or_default() -=1;
14+
}
15+
16+
map.into_values().all(|cnt| cnt ==0)
17+
}
18+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
structCodec{}
2+
3+
implCodec{
4+
fnnew() ->Self{
5+
Self{}
6+
}
7+
8+
fnencode(&self,strs:Vec<String>) ->String{
9+
letmut store =String::new();
10+
11+
for sin strs{
12+
let len = s.len()asu8;
13+
14+
store.push(lenaschar);
15+
store.push_str(&s);
16+
}
17+
18+
store
19+
}
20+
21+
fndecode(&self,s:String) ->Vec<String>{
22+
let s:Vec<char> = s.chars().collect();
23+
letmut i =0;
24+
25+
letmut res =vec![];
26+
while i < s.len(){
27+
let len = s[i]asu8asusize;
28+
i+=1;
29+
30+
let j = i + len;
31+
32+
if j <= s.len(){
33+
let slice =&s[i..i + len];
34+
res.push(slice.into_iter().collect::<String>());
35+
}
36+
37+
i+=len;
38+
}
39+
40+
res
41+
}
42+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use std::collections::HashMap;
2+
use std::cmp::Ordering;
3+
4+
implSolution{
5+
pubfntop_k_frequent(nums:Vec<i32>,k:i32) ->Vec<i32>{
6+
letmut map:HashMap<i32,i32> =HashMap::new();
7+
8+
for nin nums{
9+
*map.entry(n).or_default() +=1;
10+
}
11+
12+
letmut freq:Vec<(i32,i32)> = map.into_iter().collect();
13+
14+
let res =if k == arr.len()asi32{
15+
&freq
16+
}else{
17+
quick_select(&mut freq, k)
18+
};
19+
20+
res.into_iter()
21+
.map(|&(n, _)| n)
22+
.collect()
23+
}
24+
}
25+
26+
pubfnquick_select(slice:&mut[(i32,i32)],k:i32) ->&[(i32,i32)]{
27+
let(mut pivot,mut i,mut j) =(0,1,1);
28+
29+
for indexin1..slice.len(){
30+
if slice[index].1 >= slice[pivot].1{
31+
slice.swap(index, j);
32+
j+=1;
33+
}else{
34+
slice.swap(index, i);
35+
i+=1;
36+
j+=1;
37+
}
38+
}
39+
40+
slice.swap(pivot, i -1);
41+
pivot = i -1;
42+
43+
let larger_items =(j - pivot)asi32;
44+
45+
match larger_items.cmp(&k){
46+
Ordering::Less =>quick_select(&mut slice[0..j], k),
47+
Ordering::Greater =>quick_select(&mut slice[pivot +1..j], k),
48+
Ordering::Equal =>&slice[pivot..j],
49+
}
50+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use std::collections::HashSet;
2+
3+
4+
implSolution{
5+
pubfnis_valid_sudoku(board:Vec<Vec<char>>) ->bool{
6+
7+
letmut row:HashSet<char> =HashSet::new();
8+
letmut col:HashSet<char> =HashSet::new();
9+
letmut bx:HashSet<char> =HashSet::new();
10+
11+
for iin0..9{
12+
for jin0..9{
13+
let r = board[i][j];
14+
let c = board[j][i];
15+
let b = board[i /3*3 + j /3][i/3*3 + j%3];
16+
17+
if r !='.'{
18+
if !row.contains(&r){
19+
row.insert(r);
20+
}else{
21+
returnfalse;
22+
}
23+
}
24+
25+
if c !='.'{
26+
if !col.contains(&c){
27+
col.insert(c);
28+
}else{
29+
returnfalse;
30+
}
31+
}
32+
33+
if b !='.'{
34+
if !bx.contains(&b){
35+
bx.insert(b);
36+
}else{
37+
returnfalse;
38+
}
39+
}
40+
}
41+
42+
row.clear();
43+
col.clear();
44+
bx.clear();
45+
}
46+
47+
true
48+
}
49+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std::collections::HashMap;
2+
3+
implSolution{
4+
pubfngroup_anagrams(strs:Vec<String>) ->Vec<Vec<String>>{
5+
letmut map:HashMap<[u16;26],Vec<String>> =HashMap::new();
6+
7+
for sin strs{
8+
letmut key =[0_u16;26];
9+
10+
for cin s.chars(){
11+
key[casusize -'a'asusize] +=1;
12+
}
13+
14+
ifletSome(vals) = map.get_mut(&key){
15+
vals.push(s);
16+
}else{
17+
map.insert(key,vec![s]);
18+
}
19+
}
20+
21+
map.into_values().collect::<Vec<Vec<String>>>()
22+
}
23+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
implSolution{
2+
pubfnmax_area(height:Vec<i32>) ->i32{
3+
let(mut l,mut r) =(0, height.len() -1);
4+
5+
letmut max =0;
6+
7+
while(l < r){
8+
let(lh, rh) =(height[l], height[r]);
9+
let h = lh.min(rh);
10+
11+
let d =(r - l)asi32;
12+
let area = d* h;
13+
14+
if area > max{
15+
max = area;
16+
}
17+
18+
if rh< lh{
19+
while r >0 && height[r] <= rh{
20+
r-=1;
21+
}
22+
}else{
23+
while l < height.len() && height[l] <= lh{
24+
l+=1;
25+
}
26+
}
27+
}
28+
29+
max
30+
}
31+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp