Skip to content

Commit 4e30071

Browse files
committed
solve #167
1 parent 63537e5 commit 4e30071

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

Diff for: src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,5 @@ mod n0155_min_stack;
153153
mod n0162_find_peak_element;
154154
mod n0164_maximum_gap;
155155
mod n0165_compare_version_numbers;
156+
mod n0166_fraction_to_recurring_decimal;
157+
mod n0167_two_sum_ii_input_array_is_sorted;

Diff for: src/n0166_fraction_to_recurring_decimal.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* [166] Fraction to Recurring Decimal
3+
*
4+
* Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.
5+
*
6+
* If the fractional part is repeating, enclose the repeating part in parentheses.
7+
*
8+
* Example 1:
9+
*
10+
*
11+
* Input: numerator = 1, denominator = 2
12+
* Output: "0.5"
13+
*
14+
*
15+
* Example 2:
16+
*
17+
*
18+
* Input: numerator = 2, denominator = 1
19+
* Output: "2"
20+
*
21+
* Example 3:
22+
*
23+
*
24+
* Input: numerator = 2, denominator = 3
25+
* Output: "0.(6)"
26+
*
27+
*
28+
*/
29+
pub struct Solution {}
30+
31+
// submission codes start here
32+
33+
// TODO
34+
impl Solution {
35+
pub fn fraction_to_decimal(numerator: i32, denominator: i32) -> String {
36+
"".to_owned()
37+
}
38+
}
39+
40+
// submission codes end
41+
42+
#[cfg(test)]
43+
mod tests {
44+
use super::*;
45+
46+
#[test]
47+
fn test_166() {
48+
}
49+
}

Diff for: src/n0167_two_sum_ii_input_array_is_sorted.rs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* [167] Two Sum II - Input array is sorted
3+
*
4+
* Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
5+
*
6+
* The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.
7+
*
8+
* Note:
9+
*
10+
*
11+
* Your returned answers (both index1 and index2) are not zero-based.
12+
* You may assume that each input would have exactly one solution and you may not use the same element twice.
13+
*
14+
*
15+
* Example:
16+
*
17+
*
18+
* Input: numbers = [2,7,11,15], target = 9
19+
* Output: [1,2]
20+
* Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
21+
*
22+
*/
23+
pub struct Solution {}
24+
25+
// submission codes start here
26+
27+
impl Solution {
28+
pub fn two_sum(numbers: Vec<i32>, target: i32) -> Vec<i32> {
29+
let mut i = 0_usize;
30+
let mut j = numbers.len() - 1;
31+
while i < j {
32+
let sum = numbers[i] + numbers[j];
33+
if sum > target {
34+
j -= 1;
35+
} else if sum < target {
36+
i += 1;
37+
} else {
38+
break;
39+
}
40+
}
41+
return vec![i as i32 + 1, j as i32 + 1]
42+
}
43+
}
44+
45+
// submission codes end
46+
47+
#[cfg(test)]
48+
mod tests {
49+
use super::*;
50+
51+
#[test]
52+
fn test_167() {
53+
assert_eq!(Solution::two_sum(vec![2,7,11,15], 9), vec![1,2]);
54+
}
55+
}

0 commit comments

Comments
 (0)