Skip to content

Commit b68b135

Browse files
committed
1 parent 2ddb9ad commit b68b135

File tree

5 files changed

+263
-0
lines changed

5 files changed

+263
-0
lines changed

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ mod n0022_generate_parentheses;
2525
mod n0023_merge_k_sorted_lists;
2626
mod n0024_swap_nodes_in_pairs;
2727
mod n0025_reverse_nodes_in_k_group;
28+
mod n0026_remove_duplicates_from_sorted_array;
29+
mod n0027_remove_element;
30+
mod n0028_implement_strstr;
31+
mod n0029_divide_two_integers;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* [26] Remove Duplicates from Sorted Array
3+
*
4+
* Given a sorted array nums, remove the duplicates <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> such that each element appear only once and return the new length.
5+
*
6+
* Do not allocate extra space for another array, you must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with O(1) extra memory.
7+
*
8+
* Example 1:
9+
*
10+
*
11+
* Given nums = [1,1,2],
12+
*
13+
* Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.
14+
*
15+
* It doesn't matter what you leave beyond the returned length.
16+
*
17+
* Example 2:
18+
*
19+
*
20+
* Given nums = [0,0,1,1,1,2,2,3,3,4],
21+
*
22+
* Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.
23+
*
24+
* It doesn't matter what values are set beyond the returned length.
25+
*
26+
*
27+
* Clarification:
28+
*
29+
* Confused why the returned value is an integer but your answer is an array?
30+
*
31+
* Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
32+
*
33+
* Internally you can think of this:
34+
*
35+
*
36+
* // nums is passed in by reference. (i.e., without making a copy)
37+
* int len = removeDuplicates(nums);
38+
*
39+
* // any modification to nums in your function would be known by the caller.
40+
* // using the length returned by your function, it prints the first len elements.
41+
* for (int i = 0; i < len; i++) {
42+
* print(nums[i]);
43+
* }
44+
*
45+
*/
46+
pub struct Solution {}
47+
48+
// submission codes start here
49+
50+
impl Solution {
51+
pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
52+
nums.dedup();
53+
nums.len() as i32
54+
}
55+
}
56+
57+
// submission codes end
58+
59+
#[cfg(test)]
60+
mod tests {
61+
use super::*;
62+
63+
#[test]
64+
fn test_26() {
65+
assert_eq!(Solution::remove_duplicates(&mut vec![]), 0);
66+
let mut vec1 = vec![1,1,1,1,3];
67+
assert_eq!(Solution::remove_duplicates(&mut vec1), 2);
68+
assert_eq!(vec1, vec![1,3]);
69+
let mut vec2 = vec![1,1,2];
70+
assert_eq!(Solution::remove_duplicates(&mut vec2), 2);
71+
assert_eq!(vec2, vec![1,2]);
72+
}
73+
}

src/n0027_remove_element.rs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* [27] Remove Element
3+
*
4+
* Given an array nums and a value val, remove all instances of that value <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> and return the new length.
5+
*
6+
* Do not allocate extra space for another array, you must do this by modifying the input array <a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank">in-place</a> with O(1) extra memory.
7+
*
8+
* The order of elements can be changed. It doesn't matter what you leave beyond the new length.
9+
*
10+
* Example 1:
11+
*
12+
*
13+
* Given nums = [3,2,2,3], val = 3,
14+
*
15+
* Your function should return length = 2, with the first two elements of nums being 2.
16+
*
17+
* It doesn't matter what you leave beyond the returned length.
18+
*
19+
*
20+
* Example 2:
21+
*
22+
*
23+
* Given nums = [0,1,2,2,3,0,4,2], val = 2,
24+
*
25+
* Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
26+
*
27+
* Note that the order of those five elements can be arbitrary.
28+
*
29+
* It doesn't matter what values are set beyond the returned length.
30+
*
31+
* Clarification:
32+
*
33+
* Confused why the returned value is an integer but your answer is an array?
34+
*
35+
* Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
36+
*
37+
* Internally you can think of this:
38+
*
39+
*
40+
* // nums is passed in by reference. (i.e., without making a copy)
41+
* int len = removeElement(nums, val);
42+
*
43+
* // any modification to nums in your function would be known by the caller.
44+
* // using the length returned by your function, it prints the first len elements.
45+
* for (int i = 0; i < len; i++) {
46+
* print(nums[i]);
47+
* }
48+
*
49+
*/
50+
pub struct Solution {}
51+
52+
// submission codes start here
53+
54+
impl Solution {
55+
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
56+
if nums.len() < 1 { return 0 }
57+
let (mut start, mut end) = (0_usize, nums.len()-1);
58+
while start < end {
59+
if nums[start] == val {
60+
nums[start] = nums[end-1];
61+
end -= 1;
62+
} else {
63+
start += 1;
64+
}
65+
}
66+
end as i32
67+
}
68+
}
69+
70+
// submission codes end
71+
72+
#[cfg(test)]
73+
mod tests {
74+
use super::*;
75+
76+
#[test]
77+
fn test_27() {
78+
let mut vec1 = vec![0,1,2,2,3,0,4,2];
79+
assert_eq!(Solution::remove_element(&mut vec1, 2), 5);
80+
assert_eq!(vec1[0..5], [0,1,4,0,3]);
81+
assert_eq!(Solution::remove_element(&mut vec![], 2), 0);
82+
assert_eq!(Solution::remove_element(&mut vec![1,2,2,2,2,2,2], 2), 1);
83+
assert_eq!(Solution::remove_element(&mut vec![2,2,2,2,2,2,2], 2), 0);
84+
assert_eq!(Solution::remove_element(&mut vec![1], 1), 0);
85+
}
86+
}

src/n0028_implement_strstr.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* [28] Implement strStr()
3+
*
4+
* Implement <a href="http://www.cplusplus.com/reference/cstring/strstr/" target="_blank">strStr()</a>.
5+
*
6+
* Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
7+
*
8+
* Example 1:
9+
*
10+
*
11+
* Input: haystack = "hello", needle = "ll"
12+
* Output: 2
13+
*
14+
*
15+
* Example 2:
16+
*
17+
*
18+
* Input: haystack = "aaaaa", needle = "bba"
19+
* Output: -1
20+
*
21+
*
22+
* Clarification:
23+
*
24+
* What should we return when needle is an empty string? This is a great question to ask during an interview.
25+
*
26+
* For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's <a href="http://www.cplusplus.com/reference/cstring/strstr/" target="_blank">strstr()</a> and Java's <a href="https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)" target="_blank">indexOf()</a>.
27+
*
28+
*/
29+
pub struct Solution {}
30+
31+
// submission codes start here
32+
33+
impl Solution {
34+
pub fn str_str(haystack: String, needle: String) -> i32 {
35+
if needle.is_empty() { return 0 }
36+
haystack.find(&needle).map_or(-1_i32, |v| v as i32)
37+
}
38+
}
39+
40+
// submission codes end
41+
42+
#[cfg(test)]
43+
mod tests {
44+
use super::*;
45+
46+
#[test]
47+
fn test_28() {
48+
assert_eq!(Solution::str_str("hello".to_string(), "ll".to_string()), 2)
49+
}
50+
}

src/n0029_divide_two_integers.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* [29] Divide Two Integers
3+
*
4+
* Given two integers dividend and divisor, divide two integers without using multiplication, division and mod operator.
5+
*
6+
* Return the quotient after dividing dividend by divisor.
7+
*
8+
* The integer division should truncate toward zero.
9+
*
10+
* Example 1:
11+
*
12+
*
13+
* Input: dividend = 10, divisor = 3
14+
* Output: 3
15+
*
16+
* Example 2:
17+
*
18+
*
19+
* Input: dividend = 7, divisor = -3
20+
* Output: -2
21+
*
22+
* Note:
23+
*
24+
*
25+
* Both dividend and divisor will be 32-bit signed integers.
26+
* The divisor will never be 0.
27+
* Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [-2^31, 2^31 - 1]. For the purpose of this problem, assume that your function returns 2^31 - 1 when the division result overflows.
28+
*
29+
*
30+
*/
31+
pub struct Solution {}
32+
33+
// submission codes start here
34+
35+
impl Solution {
36+
pub fn divide(dividend: i32, divisor: i32) -> i32 {
37+
38+
}
39+
}
40+
41+
// submission codes end
42+
43+
#[cfg(test)]
44+
mod tests {
45+
use super::*;
46+
47+
#[test]
48+
fn test_29() {
49+
}
50+
}

0 commit comments

Comments
 (0)