Skip to content

Commit 0d25862

Browse files
authored
add single_number (#1)
* init single-number.rs * fix path * minor * add LICENSE.md * update ignore * update * add resource * update readme * fix
1 parent 62eb669 commit 0d25862

File tree

5 files changed

+55
-3
lines changed

5 files changed

+55
-3
lines changed

.gitignore

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
kamyu104/target/
1+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
2+
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
3+
# Cargo.lock
4+
5+
# These are backup files generated by rustfmt
6+
**/*.rs.bk
7+
8+
**/target/

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2019 https://github.com/rust-interview
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ cargo test
1010

1111
* [Bit Manipulation](https://github.com/rust-interview/rust-leetcode-solutions#bit-manipulation)
1212

13+
17+
1318
kamyu104/src/main.rs
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
mod example;
1+
mod example;
2+
mod single_number;

kamyu104/src/single_number.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
#[allow(dead_code)]
5+
pub fn single_number(nums: Vec<i32>) -> i32 {
6+
nums.iter().fold(0, |acc, &x| acc ^ x)
7+
}
8+
9+
#[cfg(test)]
10+
mod tests {
11+
use super::*;
12+
13+
#[test]
14+
fn test_single_number() {
15+
assert_eq!(single_number(vec![2,2,1]), 1);
16+
assert_eq!(single_number(vec![4,1,2,1,2]), 4);
17+
}
18+
}

0 commit comments

Comments
 (0)