Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 23d77ca

Browse files
author
guangsheng.li01
committed
Solved 0169 & 0343
1 parent 52684ec commit 23d77ca

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

‎src/a0169_majority_element.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* [0169] majority-element
3+
*/
4+
5+
pub struct Solution {}
6+
7+
// solution impl starts here
8+
9+
impl Solution {
10+
pub fn majority_element(nums: Vec<i32>) -> i32 {
11+
let mut i = 0;
12+
let mut count = 0;
13+
for j in nums {
14+
if count == 0 {
15+
i = j;
16+
}
17+
count += if j == i { 1 } else { -1 }
18+
}
19+
i
20+
}
21+
}
22+
23+
// solution impl ends here
24+
25+
// solution tests starts here
26+
27+
#[cfg(test)]
28+
mod tests {
29+
use super::*;
30+
31+
#[test]
32+
fn test_case0() {
33+
assert_eq!(Solution::majority_element(vec![3, 2, 3]), 3);
34+
assert_eq!(Solution::majority_element(vec![3, 3, 4]), 3);
35+
}
36+
37+
#[test]
38+
fn test_case1() {
39+
assert_eq!(Solution::majority_element(vec![2, 2, 1, 1, 1, 2, 2]), 2);
40+
}
41+
}
42+
43+
// solution tests ends here

‎src/a0343_integer_break.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* [0343] integer-break
3+
*/
4+
5+
pub struct Solution {}
6+
7+
// solution impl starts here
8+
9+
impl Solution {
10+
pub fn integer_break(n: i32) -> i32 {
11+
if n <= 3 {
12+
return n - 1;
13+
}
14+
15+
let a = n / 3;
16+
let b = n % 3;
17+
if b == 0 {
18+
return 3i32.pow(a as u32);
19+
}
20+
21+
if b == 1 {
22+
return 3i32.pow(a as u32 - 1) * 4;
23+
}
24+
25+
3i32.pow(a as u32) * 2
26+
}
27+
}
28+
29+
// solution impl ends here
30+
31+
// solution tests starts here
32+
33+
#[cfg(test)]
34+
mod tests {
35+
use super::*;
36+
37+
#[test]
38+
fn test_case0() {
39+
assert_eq!(Solution::integer_break(2), 1);
40+
assert_eq!(Solution::integer_break(10), 36);
41+
}
42+
}
43+
44+
// solution tests ends here

‎src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ mod a0103_binary_tree_zigzag_level_order_traversal;
1919
mod a0118_pascals_triangle;
2020
mod a0119_pascals_triangle_ii;
2121
mod a0145_binary_tree_postorder_traversal;
22+
mod a0169_majority_element;
2223
mod a0172_factorial_trailing_zeroes;
2324
mod a0322_coin_change;
25+
mod a0343_integer_break;
2426
mod a0392_is_subsequence;
2527
mod a0400_nth_digit;
2628
mod a0404_sum_of_left_leaves;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /