add initial data
This commit is contained in:
1 parent
048a82b101
commit
51d935d06b
66 files changed
+1329
No files matched your search
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "leetcode"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "leetcode"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,94 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
||||
pub fn my_atoi(s: String) -> i32 {
|
||||
let mut chars = s.trim().chars().peekable();
|
||||
|
||||
let is_negative = match chars.peek() {
|
||||
Some(&'+') => {
|
||||
chars.next();
|
||||
false
|
||||
}
|
||||
Some(&'-') => {
|
||||
chars.next();
|
||||
true
|
||||
}
|
||||
_ => false,
|
||||
};
|
||||
|
||||
let mut accum: i64 = 0;
|
||||
for c in chars {
|
||||
let Some(digit) = c.to_digit(10) else {
|
||||
break;
|
||||
};
|
||||
accum = accum * 10 - digit as i64;
|
||||
accum = accum.max(i32::MIN as i64);
|
||||
}
|
||||
|
||||
if is_negative {
|
||||
accum as i32
|
||||
} else {
|
||||
(-accum).min(i32::MAX as i64) as i32
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::my_atoi;
|
||||
|
||||
#[test]
|
||||
fn test_1() {
|
||||
assert_eq!(my_atoi("42".to_string()), 42);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_2() {
|
||||
assert_eq!(my_atoi(" -042".to_string()), -42);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_3() {
|
||||
assert_eq!(my_atoi("1337c0d3".to_string()), 1337);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_4() {
|
||||
assert_eq!(my_atoi("0-1".to_string()), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_5() {
|
||||
assert_eq!(my_atoi("words and 987".to_string()), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_6() {
|
||||
assert_eq!(my_atoi((i32::MAX as i64 + 1).to_string()), i32::MAX);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_7() {
|
||||
assert_eq!(my_atoi((i32::MAX).to_string()), i32::MAX);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_8() {
|
||||
assert_eq!(my_atoi("9223372036854775808".to_string()), i32::MAX);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_9() {
|
||||
assert_eq!(
|
||||
my_atoi("1234567890123456789012345678901234567890".to_string()),
|
||||
i32::MAX
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_10() {
|
||||
assert_eq!(
|
||||
my_atoi("10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000522545459".to_string()),
|
||||
i32::MAX
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user