Compare commits
2 Commits
a688ae2cd8
...
f866d1ba7d
Author | SHA1 | Date |
---|---|---|
Bauke | f866d1ba7d | |
Bauke | 75b206bb24 |
|
@ -1,17 +1,7 @@
|
|||
# Generated by Cargo
|
||||
# will have compiled files and executables
|
||||
Cargo.lock
|
||||
debug/
|
||||
target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
||||
*.pdb
|
||||
|
||||
# Code coverage results
|
||||
.coverage
|
||||
coverage/
|
||||
|
|
|
@ -2,11 +2,16 @@
|
|||
|
||||
[package]
|
||||
name = "leetcode"
|
||||
description = "Leetcode solutions in Rust."
|
||||
repository = "https://git.bauke.xyz/Bauke/leetcode"
|
||||
license="Unlicense"
|
||||
version = "0.0.0"
|
||||
authors = ["Bauke <me@bauke.xyz>"]
|
||||
edition = "2021"
|
||||
publish = false
|
||||
|
||||
[lib]
|
||||
path = "source/lib.rs"
|
||||
|
||||
[dev-dependencies]
|
||||
test-case = "2.0.2"
|
||||
test-case = "2.2.1"
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
This is free and unencumbered software released into the public domain.
|
||||
|
||||
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
|
||||
|
||||
In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
For more information, please refer to <https://unlicense.org/>
|
|
@ -33,7 +33,7 @@ args = [
|
|||
"tarpaulin",
|
||||
"--exclude-files=target/*",
|
||||
"--out=html",
|
||||
"--output-dir=.coverage",
|
||||
"--output-dir=coverage",
|
||||
"--skip-clean",
|
||||
"--target-dir=target/tarpaulin"
|
||||
]
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
# Leetcode 🧑🏫
|
||||
|
||||
> **Leetcode solutions in Rust.**
|
||||
|
||||
## License
|
||||
|
||||
Distributed under the [Unlicense](https://spdx.org/licenses/Unlicense.html) license, see [LICENSE](https://git.bauke.xyz/Bauke/leetcode/src/branch/main/LICENSE) for more information.
|
|
@ -1,16 +1,21 @@
|
|||
pub fn first_missing_positive(mut numbers: Vec<i32>) -> i32 {
|
||||
pub fn first_missing_positive(numbers: Vec<i32>) -> i32 {
|
||||
let numbers_length = numbers.len();
|
||||
|
||||
// Mark all numbers below 1 and above the maximum range as i32::MAX.
|
||||
for index in 0..numbers_length {
|
||||
if numbers[index] < 1 || numbers[index] > numbers_length as i32 {
|
||||
numbers[index] = i32::MAX;
|
||||
}
|
||||
}
|
||||
let mut numbers = numbers
|
||||
.into_iter()
|
||||
.map(|number| {
|
||||
if number < 1 || number > numbers_length as i32 {
|
||||
i32::MAX
|
||||
} else {
|
||||
number
|
||||
}
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
for index in 0..numbers_length {
|
||||
// For each index get its number and subtract 1.
|
||||
let index = numbers[index].abs() as usize - 1;
|
||||
let index = numbers[index].unsigned_abs() as usize - 1;
|
||||
|
||||
// If that index then isn't i32::MAX - 1 (ie. negative or above range).
|
||||
if index as i32 != i32::MAX - 1 {
|
||||
|
@ -32,5 +37,5 @@ pub fn first_missing_positive(mut numbers: Vec<i32>) -> i32 {
|
|||
None
|
||||
}
|
||||
})
|
||||
.unwrap_or_else(|| numbers_length + 1) as i32
|
||||
.unwrap_or(numbers_length + 1) as i32
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ pub fn is_number(string: String) -> bool {
|
|||
return false;
|
||||
}
|
||||
|
||||
let mut parts = string.split("e");
|
||||
let mut parts = string.split('e');
|
||||
|
||||
let valid_significand = match parts.next() {
|
||||
Some(part) => part.parse::<f64>().is_ok(),
|
||||
|
|
|
@ -5,7 +5,7 @@ pub fn word_pattern(pattern: String, string: String) -> bool {
|
|||
let mut pattern_iter = pattern.chars();
|
||||
let mut string_iter = string.split_whitespace();
|
||||
|
||||
if pattern.len() != string_iter.clone().collect::<Vec<_>>().len() {
|
||||
if pattern.len() != string_iter.clone().count() {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue