This commit is contained in:
arielherself 2024-01-01 23:34:42 +08:00
parent 1aeecc6ba7
commit 434f3b0d3e
5 changed files with 127 additions and 14 deletions

34
src/bin/lc-373.rs Normal file
View File

@ -0,0 +1,34 @@
use std::cmp::Ordering;
struct Solution;
impl Solution {
pub fn k_smallest_pairs(nums1: Vec<i32>, nums2: Vec<i32>, k: i32) -> Vec<Vec<i32>> {
use std::collections::{BinaryHeap,HashSet};
let mut pq = BinaryHeap::<(i32,usize,usize)>::new();
pq.push((-nums1[0]-nums2[0],0,0));
let m=nums1.len();
let n = nums2.len();
let mut vis = HashSet::<(usize,usize)>::new();
let mut res = Vec::new();
let mut cnt = 0;
while let Some((_,i,j)) = pq.pop() {
if vis.contains(&(i,j)) {continue;}
vis.insert((i,j));
res.push(vec![nums1[i],nums2[j]]);
cnt += 1;
if cnt == k as usize {break;}
if i+1!=m {
pq.push((-nums1[i+1]-nums2[j],i+1,j));
}
if j+1!=n {
pq.push((-nums1[i]-nums2[j+1],i,j+1));
}
}
res
}
}
fn main() {
}

34
src/bin/lc-402.rs Normal file
View File

@ -0,0 +1,34 @@
use std::collections::VecDeque;
struct Solution;
impl Solution {
pub fn remove_kdigits(num: String, k: i32) -> String {
use std::collections::VecDeque;;
let num = num.into_bytes(); // TODO
let mut st = VecDeque::<u8>::new();
let mut cnt = 0;
for x in num {
while let Some(&y) = st.back() {
if x >= y || cnt == k { break; }
st.pop_back();
cnt += 1;
}
st.push_back(x);
}
for _ in cnt..k {
st.pop_back();
}
while let Some(48) = st.front() {
st.pop_front();
}
if st.is_empty() { st.push_back(48); }
String::from_utf8(st.into_iter().collect::<Vec<u8>>()).unwrap()
}
}
fn main() {
let num = "1432219".to_string();
let k = 3;
println!("{}",Solution::remove_kdigits(num,k));
}

23
src/bin/lc-406.rs Normal file
View File

@ -0,0 +1,23 @@
struct Solution;
impl Solution {
pub fn reconstruct_queue(mut people: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
people.sort_unstable_by(|x,y|{
if x[0] == y[0] {
x[1].cmp(&y[1])
} else {
y[0].cmp(&x[0])
}
});
let n = people.len();
let mut res = Vec::<Vec<i32>>::with_capacity(n);
for curr in people {
res.insert(curr[1] as usize, curr);
}
res
}
}
fn main() {
}

27
src/bin/lc-409.rs Normal file
View File

@ -0,0 +1,27 @@
struct Solution;
impl Solution {
pub fn longest_palindrome(s: String) -> i32 {
use std::collections::HashMap;
let mut s = s.into_bytes();
let mut mp = HashMap::<u8,i32>::new();
for i in s {
if let Some(&v) = mp.get(&i) {
mp.insert(i,v+1);
}else{
mp.insert(i,1);
}
}
let mut res = 0;
let mut f = 0;
for (_,v) in mp {
if v & 1 != 0 { f = 1; }
res += v >> 1;
}
res + f
}
}
fn main() {
}

View File

@ -1,15 +1,10 @@
trait Printable<T> {
fn print(a:T);
}
struct Test;
impl<T> Printable<T> for Test where T: std::fmt::Display {
fn print(a:T) {
println!("{a}");
}
}
mod test_mod;
mod test_mods;
mod a_sibling_file;
fn main() {
<Test>::print(1.5);
use test_mod::a_mod::hello_world;
use test_mods::another_mod;
hello_world();
another_mod::another_fn();
a_sibling_file::sibling_mod::sibling_fn();
}