Skip to content

Commit 825616c

Browse files
committed
Rust Solutions for array & hashing
1 parent 5515379 commit 825616c

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+
impl Solution {
7+
pub fn longest_consecutive(nums: Vec<i32>) -> i32 {
8+
let mut set : HashSet<i32> = HashSet::from_iter(nums.into_iter());
9+
10+
let mut max_cnt = 0;
11+
12+
for n in &set{
13+
if !set.contains(&(n-1)){
14+
let mut next = n + 1;
15+
let mut 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+
}

rust/01_arrays_&_hashing/1_two_sum.rs

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+
impl Solution {
4+
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
5+
let mut map = HashMap::new();
6+
7+
for (i, n) in nums.into_iter().enumerate(){
8+
let diff = target - n;
9+
10+
if let Some(&j) = map.get(&diff){
11+
return vec![i as i32, j as i32];
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+
impl Solution {
4+
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
5+
let mut map = HashSet::new();
6+
7+
for n in nums.iter(){
8+
9+
if map.contains(n){
10+
return true;
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+
impl Solution {
2+
pub fn product_except_self(mut nums: Vec<i32>) -> Vec<i32> {
3+
let mut res = vec![1; nums.len()];
4+
5+
for i in (1..nums.len()){
6+
res[i] = nums[i - 1] * res[i - 1];
7+
}
8+
9+
let mut 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+
impl Solution {
4+
pub fn is_anagram(s: String, t: String) -> bool {
5+
if (t.len() != s.len()){
6+
return false;
7+
}
8+
9+
let mut 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+
struct Codec {}
2+
3+
impl Codec {
4+
fn new() -> Self {
5+
Self {}
6+
}
7+
8+
fn encode(&self, strs: Vec<String>) -> String {
9+
let mut store = String::new();
10+
11+
for s in strs{
12+
let len = s.len() as u8;
13+
14+
store.push(len as char);
15+
store.push_str(&s);
16+
}
17+
18+
store
19+
}
20+
21+
fn decode(&self, s: String) -> Vec<String> {
22+
let s: Vec<char> = s.chars().collect();
23+
let mut i = 0;
24+
25+
let mut res = vec![];
26+
while i < s.len(){
27+
let len = s[i] as u8 as usize;
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+
impl Solution {
5+
pub fn top_k_frequent(nums: Vec<i32>, k: i32) -> Vec<i32> {
6+
let mut map: HashMap<i32, i32> = HashMap::new();
7+
8+
for n in nums{
9+
*map.entry(n).or_default() +=1;
10+
}
11+
12+
let mut freq: Vec<(i32, i32)> = map.into_iter().collect();
13+
14+
let res = if k == arr.len() as i32{
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+
pub fn quick_select(slice: &mut [(i32, i32)], k: i32) -> &[(i32, i32)]{
27+
let (mut pivot, mut i, mut j) = (0, 1, 1);
28+
29+
for index in 1..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) as i32;
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+
impl Solution {
5+
pub fn is_valid_sudoku(board: Vec<Vec<char>>) -> bool {
6+
7+
let mut row: HashSet<char> = HashSet::new();
8+
let mut col: HashSet<char> = HashSet::new();
9+
let mut bx : HashSet<char> = HashSet::new();
10+
11+
for i in 0..9{
12+
for j in 0..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+
return false;
22+
}
23+
}
24+
25+
if c != '.'{
26+
if !col.contains(&c){
27+
col.insert(c);
28+
}else{
29+
return false;
30+
}
31+
}
32+
33+
if b != '.'{
34+
if !bx.contains(&b){
35+
bx.insert(b);
36+
}else{
37+
return false;
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+
impl Solution {
4+
pub fn group_anagrams(strs: Vec<String>) -> Vec<Vec<String>> {
5+
let mut map: HashMap<[u16; 26], Vec<String>> = HashMap::new();
6+
7+
for s in strs{
8+
let mut key = [0_u16; 26];
9+
10+
for c in s.chars(){
11+
key[c as usize - 'a' as usize] += 1;
12+
}
13+
14+
if let Some(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+
impl Solution {
2+
pub fn max_area(height: Vec<i32>) -> i32 {
3+
let (mut l, mut r) = (0, height.len() - 1);
4+
5+
let mut 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) as i32;
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)