-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
237 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ | |
*.exe | ||
*.o | ||
*.xml | ||
**.vscode | ||
**.vscode | ||
rustpractice/target/* |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "rustpractice" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
pub mod problem1; | ||
pub mod p13; | ||
pub mod p14; | ||
// use problem1::problem1::Solution; | ||
// use p13::p13::Solution; | ||
|
||
fn main() { | ||
println!("Hello, world!"); | ||
// let a = Solution{}; | ||
|
||
// let nums = vec![3,2,4]; | ||
|
||
|
||
// let r = a.two_sum(nums, 6); | ||
// println!("{:?}", r); | ||
|
||
|
||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// #[cfg(test)] | ||
pub mod p13 { | ||
use std::{cmp::min, collections::HashMap, hash::Hash}; | ||
pub struct Solution {} | ||
impl Solution { | ||
pub fn roman_to_int(self, s: String) -> i32 { | ||
let mut done_map = HashMap::new(); | ||
let mut m = HashMap::new(); | ||
m.insert("I", 1); | ||
m.insert("V", 5); | ||
m.insert("X", 10); | ||
m.insert("L", 50); | ||
m.insert("C", 100); | ||
m.insert("D", 500); | ||
m.insert("M", 1000); | ||
m.insert("IV", 4); | ||
m.insert("IX", 9); | ||
m.insert("XL", 40); | ||
m.insert("XC", 90); | ||
m.insert("CD", 400); | ||
m.insert("CM", 900); | ||
|
||
// let b = &s[0..s.len()]; | ||
// let part1 = &s[0..0]; | ||
// println!("part1 : {}";', part1); | ||
println!("s len:{:?}", s.len()); | ||
let mut sum: i32 = 0; | ||
for i in 0..s.len() { | ||
match done_map.get(&i) { | ||
Some(t) => {} | ||
None => { | ||
let last = min(i + 2, s.len()); | ||
let b = &s[i..i + 1]; | ||
let b2 = &s[i..last]; | ||
println!("i {} b2 {}", i, b2); | ||
// let c = b2.to_owned(); | ||
if ("IV" == b2) | ||
|| ("IX" == b2) | ||
|| ("XL" == b2) | ||
|| ("XC" == b2) | ||
|| ("CD" == b2) | ||
|| ("CM" == b2) | ||
{ | ||
// println!("sss"); | ||
let v = m.get(b2).unwrap(); | ||
println!("b2 {} add {}", b2, v); | ||
for j in i..last { | ||
done_map.insert(j, 0); | ||
} | ||
sum = sum + v; | ||
continue; | ||
} else { | ||
let v = m.get(b).unwrap(); | ||
println!("b {} add {}", b, v); | ||
done_map.insert(i, 0); | ||
sum = sum + v; | ||
} | ||
|
||
println!("-------------------- {:?}", b.to_string()); | ||
} | ||
} | ||
} | ||
println!("{}", sum); | ||
sum | ||
} | ||
} | ||
|
||
#[test] | ||
pub fn test() { | ||
let a = Solution {}; | ||
let s = "MCMXCIV"; | ||
|
||
assert_eq!(1994, a.roman_to_int(s.to_string())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#[cfg(test)] | ||
pub mod p14 { | ||
use std::cmp::min; | ||
|
||
pub struct Solution {} | ||
|
||
impl Solution { | ||
pub fn longest_common_prefix(strs: Vec<String>) -> String { | ||
// let max_str = strs.iter().max(); | ||
// let min_str = strs.iter().min(); | ||
// // println!("max_str {:?}",max_str.unwrap()); | ||
// // println!("min_str {:?}",min_str.unwrap()); | ||
|
||
// let max_chrs = max_str.unwrap().chars(); | ||
// let min_chrs = min_str.unwrap().chars(); | ||
// // println!("{:?} {:?}",max_chrs, min_chrs); | ||
// let b = max_chrs.zip(min_chrs).take_while(|x| x.0 == x.1); | ||
// println!("==============b==={:?}",b); | ||
// let r = b.map(|x| x.0).collect(); | ||
// // println!("==============R==={:?}",r); | ||
// r | ||
// // strs.iter() | ||
if strs.len() == 0 { | ||
return "".to_string(); | ||
} | ||
// let answer:String = "".to_string(); | ||
let firststr = &strs[0]; | ||
let mut ans = firststr.to_string(); | ||
|
||
for i in 1..strs.len() { | ||
ans = Self::strcmp(&ans, &strs[i]); | ||
// firststr = &answer; | ||
println!("{:?}", ans); | ||
if ans.len()==0 { | ||
break; | ||
} | ||
|
||
} | ||
ans | ||
|
||
} | ||
pub fn strcmp(str1: &String, str2: &String) -> String { | ||
let minlen = min(str1.len(), str2.len()); | ||
let mut index: usize = 0; | ||
for i in 0..minlen { | ||
if str1.as_bytes()[i] != str2.as_bytes()[i] { | ||
break; | ||
} else { | ||
index += 1; | ||
} | ||
} | ||
str1[..index].to_string() | ||
} | ||
} | ||
#[test] | ||
pub fn test() { | ||
// let solution = Solution {}; | ||
// let s = "MCMXCIV"; | ||
let strs = vec![ | ||
"flower".to_string(), | ||
"fzow".to_string(), | ||
"flight".to_string(), | ||
]; | ||
let a = Solution::longest_common_prefix(strs); | ||
println!("============={}", a); | ||
// assert_eq!(1994, a.roman_to_int(s.to_string())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#[cfg(test)] | ||
pub mod problem1 { | ||
use std::collections::HashMap; | ||
pub struct Solution {} | ||
|
||
impl Solution { | ||
pub fn two_sum(self, nums: Vec<i32>, target: i32) -> Vec<i32> { | ||
let mut map = HashMap::new(); | ||
let mut resultv = Vec::new(); | ||
|
||
for (i, v) in nums.iter().enumerate() { | ||
let diff = target - *v; | ||
let ta_idx = map.get(&diff); | ||
println!("diff:{:?} ta_idx:{:?}", diff, ta_idx); | ||
match ta_idx { | ||
Some(tidx) => { | ||
resultv.push(*tidx); | ||
resultv.push(i as i32); | ||
break; | ||
} | ||
None => { | ||
map.insert(*v, i as i32); | ||
} | ||
} | ||
println!("map:{:?} ", map); | ||
} | ||
// println!("{:?}",map); | ||
// println!("{:?}",nums); | ||
|
||
// for (i,v ) in nums.iter().enumerate(){ | ||
// let diff = target - *v; | ||
// let ta_idx = map.get(&diff); | ||
// println!("diff:{:?} ta_idx:{:?}", diff, ta_idx); | ||
// match ta_idx{ | ||
// Some(tidx) => { | ||
// let c = i as i32; | ||
// if c != *tidx{ | ||
// resultv.push(c); | ||
// resultv.push(*tidx); | ||
// break; | ||
// } | ||
// }, | ||
// None=>{} | ||
// } | ||
|
||
// } | ||
resultv | ||
} | ||
} | ||
#[test] | ||
pub fn test() { | ||
let a = Solution {}; | ||
let nums = vec![3, 2, 4]; | ||
let r = a.two_sum(nums, 6); | ||
} | ||
} |