8. 字符串转换整数 (atoi)
This commit is contained in:
+34
-46
@@ -3,62 +3,50 @@ mod tree;
|
|||||||
|
|
||||||
struct Solution {}
|
struct Solution {}
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn min_window(s: String, t: String) -> String {
|
pub fn my_atoi(s: String) -> i32 {
|
||||||
let s = s.chars().collect::<Vec<char>>();
|
let mut ans = 0_i64;
|
||||||
let t = t.chars().collect::<Vec<char>>();
|
let mut neg = false;
|
||||||
if t.len() == 1 && s[0] == t[0] {
|
let s = s.chars().collect::<Vec<_>>();
|
||||||
return t.iter().collect();
|
let mut start = 0;
|
||||||
|
for i in 0..s.len() {
|
||||||
|
if s[i] == ' ' {
|
||||||
|
start = i + 1;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
let mut arr2 = vec![0; 123];
|
start = i + if s[i] == '+' || s[i] == '-' {
|
||||||
let mut arr1 = vec![0; 123];
|
if s[i] == '-' {
|
||||||
// 65-90 97-122
|
neg = true
|
||||||
//return "".to_string();
|
|
||||||
for c in t.iter() {
|
|
||||||
arr2[*c as usize] += 1;
|
|
||||||
}
|
}
|
||||||
// let (mut l, mut r)= (-1, -1)
|
1
|
||||||
let mut ans = 10000000;
|
} else {
|
||||||
let mut ans_index = 100000000;
|
0
|
||||||
let mut l = 0;
|
};
|
||||||
let mut is_find_ans = false;
|
break;
|
||||||
for r in 0..s.len() {
|
|
||||||
let cur = s[r] as usize;
|
|
||||||
arr1[cur] += 1;
|
|
||||||
if (is_find_ans && s[l] as usize == cur || !is_find_ans) && arr1[cur] >= arr2[cur] {
|
|
||||||
if || -> bool {
|
|
||||||
for i in 65..=90 {
|
|
||||||
if arr1[i] < arr2[i] {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
for i in start..s.len() {
|
||||||
|
if s[i] == '0' {
|
||||||
|
start = i + 1;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
for i in 97..=122 {
|
break;
|
||||||
if arr1[i] < arr2[i] {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
for i in start..s.len() {
|
||||||
|
let c = s[i];
|
||||||
|
if c.is_ascii_digit() {
|
||||||
|
ans = ans * 10 + if neg { -1 } else { 1 } * (c as u8 - '0' as u8) as i64;
|
||||||
|
if ans > i32::MAX as i64 {
|
||||||
|
return i32::MAX;
|
||||||
}
|
}
|
||||||
return true;
|
if ans < i32::MIN as i64 {
|
||||||
}() {
|
return i32::MIN;
|
||||||
while arr1[s[l] as usize] > arr2[s[l] as usize] {
|
|
||||||
arr1[s[l] as usize] -= 1;
|
|
||||||
l += 1;
|
|
||||||
}
|
}
|
||||||
is_find_ans = true;
|
continue;
|
||||||
if r - l + 1 < ans {
|
|
||||||
ans = r - l + 1;
|
|
||||||
ans_index = l;
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
ans as i32
|
||||||
}
|
|
||||||
if ans_index == 100000000 {
|
|
||||||
return "".to_string();
|
|
||||||
}
|
|
||||||
s[ans_index..ans_index + ans].iter().collect()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn main() {
|
fn main() {
|
||||||
println!(
|
println!("{:?}", Solution::my_atoi("1337c0d3".to_string()));
|
||||||
"{:?}",
|
|
||||||
Solution::min_window("cabwefgewcwaefgcf".to_string(), "cae".to_string())
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user