Skip to content

Commit 986f764

Browse files
Commit #2 RUST
1 parent 71deb02 commit 986f764

File tree

16 files changed

+331
-0
lines changed

16 files changed

+331
-0
lines changed

Defanging an IP Add/Problem.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Given a valid (IPv4) IP address, return a defanged version of that IP address.
2+
3+
A defanged IP address replaces every period "." with "[.]".
4+
5+
6+
7+
Example 1:
8+
9+
Input: address = "1.1.1.1"
10+
Output: "1[.]1[.]1[.]1"
11+
Example 2:
12+
13+
Input: address = "255.100.50.0"
14+
Output: "255[.]100[.]50[.]0"
15+

Defanging an IP Add/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
impl Solution {
2+
pub fn defang_i_paddr(address: String) -> String {
3+
address.replace(".", "[.]")
4+
}
5+
}

Goal Parser/Problem.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
You own a Goal Parser that can interpret a string command. The command consists of an alphabet of "G", "()" and/or "(al)" in some order. The Goal Parser will interpret "G" as the string "G", "()" as the string "o", and "(al)" as the string "al". The interpreted strings are then concatenated in the original order.
2+
3+
Given the string command, return the Goal Parser's interpretation of command.
4+
5+
6+
7+
Example 1:
8+
9+
Input: command = "G()(al)"
10+
Output: "Goal"
11+
Explanation: The Goal Parser interprets the command as follows:
12+
G -> G
13+
() -> o
14+
(al) -> al
15+
The final concatenated result is "Goal".
16+
Example 2:
17+
18+
Input: command = "G()()()()(al)"
19+
Output: "Gooooal"
20+
Example 3:
21+
22+
Input: command = "(al)G(al)()()G"
23+
Output: "alGalooG"
24+

Goal Parser/main.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
impl Solution {
2+
pub fn interpret(command: String) -> String {
3+
let com_chr: Vec<char> = command.chars().collect();
4+
let mut ret_str = Vec::new();
5+
for (i, chr) in command.chars().enumerate() {
6+
//println!("i: {}, chr: {}", i, chr);
7+
match chr {
8+
'G' => ret_str.push('G'),
9+
'(' => {
10+
if com_chr[i+1] == ')' { ret_str.push('o'); }
11+
else {
12+
ret_str.push('a');
13+
ret_str.push('l');
14+
}
15+
},
16+
_ => (),
17+
18+
}
19+
}
20+
ret_str.into_iter().collect()
21+
}
22+
}

Implement strStr/Problem.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
2+
3+
Clarification:
4+
5+
What should we return when needle is an empty string? This is a great question to ask during an interview.
6+
7+
For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().
8+
9+
10+
11+
Example 1:
12+
13+
Input: haystack = "hello", needle = "ll"
14+
Output: 2
15+
Example 2:
16+
17+
Input: haystack = "aaaaa", needle = "bba"
18+
Output: -1
19+
Example 3:
20+
21+
Input: haystack = "", needle = ""
22+
Output: 0
23+
24+
25+
Constraints:
26+
27+
0 <= haystack.length, needle.length <= 5 * 104
28+
haystack and needle consist of only lower-case English characters.

Implement strStr/main.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
impl Solution {
2+
pub fn str_str(haystack: String, needle: String) -> i32 {
3+
if haystack.contains(&needle) {
4+
let haystackVec: Vec<char> = haystack.chars().collect();
5+
let needleVec: Vec<char> = needle.chars().collect();
6+
if needleVec.len() == 0 { return 0; }
7+
8+
for i in 0..haystackVec.len() {
9+
let mut n = i;
10+
let mut j = 0;
11+
if haystackVec[i] == needleVec[j] {
12+
while j < needleVec.len() {
13+
//println!("entered while i={} and j={}", n, j);
14+
if haystackVec[n] == needleVec[j]{
15+
//println!("haystacktarget -> {} needletarget -> {}", haystackVec[n], needleVec[j]);
16+
j+=1;
17+
n+=1;
18+
if j >= needleVec.len() { return i as i32; }
19+
} else { j = needleVec.len()+1; }
20+
}
21+
}
22+
}
23+
return 0;
24+
} else { return -1; }
25+
}
26+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Given the array candies and the integer extraCandies, where candies[i] represents the number of candies that the ith kid has.
2+
3+
For each kid check if there is a way to distribute extraCandies among the kids such that he or she can have the greatest number of candies among them. Notice that multiple kids can have the greatest number of candies.
4+
5+
6+
7+
Example 1:
8+
9+
Input: candies = [2,3,5,1,3], extraCandies = 3
10+
Output: [true,true,true,false,true]
11+
Explanation:
12+
Kid 1 has 2 candies and if he or she receives all extra candies (3) will have 5 candies --- the greatest number of candies among the kids.
13+
Kid 2 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids.
14+
Kid 3 has 5 candies and this is already the greatest number of candies among the kids.
15+
Kid 4 has 1 candy and even if he or she receives all extra candies will only have 4 candies.
16+
Kid 5 has 3 candies and if he or she receives at least 2 extra candies will have the greatest number of candies among the kids.
17+
Example 2:
18+
19+
Input: candies = [4,2,1,1,2], extraCandies = 1
20+
Output: [true,false,false,false,false]
21+
Explanation: There is only 1 extra candy, therefore only kid 1 will have the greatest number of candies among the kids regardless of who takes the extra candy.
22+
Example 3:
23+
24+
Input: candies = [12,1,12], extraCandies = 10
25+
Output: [true,false,true]
26+
27+
28+
Constraints:
29+
30+
2 <= candies.length <= 100
31+
1 <= candies[i] <= 100
32+
1 <= extraCandies <= 50
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
impl Solution {
2+
pub fn kids_with_candies(candies: Vec<i32>, extra_candies: i32) -> Vec<bool> {
3+
let most_candy = candies.iter().max().unwrap();
4+
let mut return_vec = Vec::new();
5+
for candy in candies.iter(){
6+
if candy + extra_candies >= *most_candy { return_vec.push(true); }
7+
else { return_vec.push(false) }
8+
}
9+
10+
return_vec
11+
}
12+
}

Richest Customer Wealth/Problem.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
You are given an m x n integer grid accounts where accounts[i][j] is the amount of money the i​​​​​​​​​​​th​​​​ customer has in the j​​​​​​​​​​​th​​​​ bank. Return the wealth that the richest customer has.
2+
3+
A customer's wealth is the amount of money they have in all their bank accounts. The richest customer is the customer that has the maximum wealth.
4+
5+
6+
7+
Example 1:
8+
9+
Input: accounts = [[1,2,3],[3,2,1]]
10+
Output: 6
11+
Explanation:
12+
1st customer has wealth = 1 + 2 + 3 = 6
13+
2nd customer has wealth = 3 + 2 + 1 = 6
14+
Both customers are considered the richest with a wealth of 6 each, so return 6.
15+
Example 2:
16+
17+
Input: accounts = [[1,5],[7,3],[3,5]]
18+
Output: 10
19+
Explanation:
20+
1st customer has wealth = 6
21+
2nd customer has wealth = 10
22+
3rd customer has wealth = 8
23+
The 2nd customer is the richest with a wealth of 10.
24+
Example 3:
25+
26+
Input: accounts = [[2,8,7],[7,1,3],[1,9,5]]
27+
Output: 17
28+

Richest Customer Wealth/main.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn maximum_wealth(accounts: Vec<Vec<i32>>) -> i32 {
3+
let mut most_wealth = 0;
4+
for account in accounts {
5+
let mut account_wealth = 0;
6+
for amount in account {
7+
account_wealth+=amount;
8+
}
9+
if account_wealth > most_wealth { most_wealth = account_wealth; }
10+
}
11+
most_wealth
12+
}
13+
}

Running sum of 1d Array/Problem.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).
2+
3+
Return the running sum of nums.
4+
5+
6+
7+
Example 1:
8+
9+
Input: nums = [1,2,3,4]
10+
Output: [1,3,6,10]
11+
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
12+
Example 2:
13+
14+
Input: nums = [1,1,1,1,1]
15+
Output: [1,2,3,4,5]
16+
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
17+
Example 3:
18+
19+
Input: nums = [3,1,2,10,1]
20+
Output: [3,4,6,16,17]
21+
22+
23+
Constraints:
24+
25+
1 <= nums.length <= 1000
26+
-10^6 <= nums[i] <= 10^6

Running sum of 1d Array/main.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn running_sum(nums: Vec<i32>) -> Vec<i32> {
3+
let mut run_vec = Vec::new();
4+
for (i, num) in nums.iter().enumerate() {
5+
let mut new_num = 0;
6+
for j in 0..i+1 {
7+
new_num = nums[j] + new_num;
8+
}
9+
run_vec.push(new_num);
10+
}
11+
run_vec
12+
}
13+
}

Search Insert Position/Problem.txt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
2+
3+
4+
5+
Example 1:
6+
7+
Input: nums = [1,3,5,6], target = 5
8+
Output: 2
9+
Example 2:
10+
11+
Input: nums = [1,3,5,6], target = 2
12+
Output: 1
13+
Example 3:
14+
15+
Input: nums = [1,3,5,6], target = 7
16+
Output: 4
17+
Example 4:
18+
19+
Input: nums = [1,3,5,6], target = 0
20+
Output: 0
21+
Example 5:
22+
23+
Input: nums = [1], target = 0
24+
Output: 0
25+
26+
27+
Constraints:
28+
29+
1 <= nums.length <= 104
30+
-104 <= nums[i] <= 104
31+
nums contains distinct values sorted in ascending order.
32+
-104 <= target <= 104

Search Insert Position/main.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
impl Solution {
2+
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
3+
if target <= nums[0]{ return 0; }
4+
let mut retVal = 0;
5+
for i in 0..nums.len() {
6+
if nums[i] == target { retVal = i; }
7+
else if i+1 < nums.len(){
8+
if nums[i] < target && nums[i+1] > target {
9+
retVal = i+1;
10+
}
11+
}
12+
}
13+
if retVal == 0 { retVal = nums.len(); }
14+
retVal as i32
15+
}
16+
17+
18+
}

Shuffle the Array/Problem.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn].
2+
3+
Return the array in the form [x1,y1,x2,y2,...,xn,yn].
4+
5+
6+
7+
Example 1:
8+
9+
Input: nums = [2,5,1,3,4,7], n = 3
10+
Output: [2,3,5,4,1,7]
11+
Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].
12+
Example 2:
13+
14+
Input: nums = [1,2,3,4,4,3,2,1], n = 4
15+
Output: [1,4,2,3,3,2,4,1]
16+
Example 3:
17+
18+
Input: nums = [1,1,2,2], n = 2
19+
Output: [1,2,1,2]
20+
21+
22+
Constraints:
23+
24+
1 <= n <= 500
25+
nums.length == 2n
26+
1 <= nums[i] <= 10^3

Shuffle the Array/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
impl Solution {
2+
pub fn shuffle(nums: Vec<i32>, n: i32) -> Vec<i32> {
3+
let mut new_nums = Vec::new();
4+
for i in 0..n {
5+
new_nums.push(nums[i as usize]);
6+
let n_index = (i as i32)+n;
7+
new_nums.push(nums[n_index as usize]);
8+
}
9+
new_nums
10+
}
11+
}

0 commit comments

Comments
 (0)