Skip to content

Commit 8dff829

Browse files
committed
Solve #238
1 parent e4ab1e0 commit 8dff829

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,5 @@ mod n0229_majority_element_ii;
200200
mod n0230_kth_smallest_element_in_a_bst;
201201
mod n0231_power_of_two;
202202
mod n0232_implement_queue_using_stacks;
203+
mod n0233_number_of_digit_one;
204+
mod n0238_product_of_array_except_self;

src/n0233_number_of_digit_one.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* [233] Number of Digit One
3+
*
4+
* Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.
5+
*
6+
* Example:
7+
*
8+
*
9+
* Input: 13
10+
* Output: 6
11+
* Explanation: Digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.
12+
*
13+
*
14+
*/
15+
pub struct Solution {}
16+
17+
// submission codes start here
18+
19+
impl Solution {
20+
pub fn count_digit_one(n: i32) -> i32 {
21+
0
22+
}
23+
}
24+
25+
// submission codes end
26+
27+
#[cfg(test)]
28+
mod tests {
29+
use super::*;
30+
31+
#[test]
32+
fn test_233() {}
33+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* [238] Product of Array Except Self
3+
*
4+
* Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].
5+
*
6+
* Example:
7+
*
8+
*
9+
* Input: [1,2,3,4]
10+
* Output: [24,12,8,6]
11+
*
12+
*
13+
* Note: Please solve it without division and in O(n).
14+
*
15+
* Follow up:<br />
16+
* Could you solve it with constant space complexity? (The output array does not count as extra space for the purpose of space complexity analysis.)
17+
*
18+
*/
19+
pub struct Solution {}
20+
21+
// submission codes start here
22+
23+
/*
24+
x 2 3 4 = 24
25+
1 x 3 4 = 12
26+
1 2 x 4 = 8
27+
1 2 3 x = 6
28+
*/
29+
impl Solution {
30+
pub fn product_except_self(nums: Vec<i32>) -> Vec<i32> {
31+
if nums.len() < 2 { return vec![] }
32+
let mut res = vec![1; nums.len()];
33+
let mut n = 1;
34+
for i in (0..nums.len()-1).rev() {
35+
n *= nums[i+1];
36+
res[i] = n;
37+
}
38+
n = 1;
39+
for i in 1..nums.len() {
40+
n *= nums[i-1];
41+
res[i] *= n;
42+
}
43+
res
44+
}
45+
}
46+
47+
// submission codes end
48+
49+
#[cfg(test)]
50+
mod tests {
51+
use super::*;
52+
53+
#[test]
54+
fn test_238() {
55+
assert_eq!(Solution::product_except_self(vec![1,2,3,4]), vec![24,12,8,6]);
56+
}
57+
}

0 commit comments

Comments
 (0)