Skip to content

Commit

Permalink
Commit #2 RUST
Browse files Browse the repository at this point in the history
  • Loading branch information
gloatoriginal authored Feb 11, 2021
1 parent 71deb02 commit 986f764
Show file tree
Hide file tree
Showing 16 changed files with 331 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Defanging an IP Add/Problem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Given a valid (IPv4) IP address, return a defanged version of that IP address.

A defanged IP address replaces every period "." with "[.]".



Example 1:

Input: address = "1.1.1.1"
Output: "1[.]1[.]1[.]1"
Example 2:

Input: address = "255.100.50.0"
Output: "255[.]100[.]50[.]0"

5 changes: 5 additions & 0 deletions Defanging an IP Add/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
impl Solution {
pub fn defang_i_paddr(address: String) -> String {
address.replace(".", "[.]")
}
}
24 changes: 24 additions & 0 deletions Goal Parser/Problem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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.

Given the string command, return the Goal Parser's interpretation of command.



Example 1:

Input: command = "G()(al)"
Output: "Goal"
Explanation: The Goal Parser interprets the command as follows:
G -> G
() -> o
(al) -> al
The final concatenated result is "Goal".
Example 2:

Input: command = "G()()()()(al)"
Output: "Gooooal"
Example 3:

Input: command = "(al)G(al)()()G"
Output: "alGalooG"

22 changes: 22 additions & 0 deletions Goal Parser/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
impl Solution {
pub fn interpret(command: String) -> String {
let com_chr: Vec<char> = command.chars().collect();
let mut ret_str = Vec::new();
for (i, chr) in command.chars().enumerate() {
//println!("i: {}, chr: {}", i, chr);
match chr {
'G' => ret_str.push('G'),
'(' => {
if com_chr[i+1] == ')' { ret_str.push('o'); }
else {
ret_str.push('a');
ret_str.push('l');
}
},
_ => (),

}
}
ret_str.into_iter().collect()
}
}
28 changes: 28 additions & 0 deletions Implement strStr/Problem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Clarification:

What should we return when needle is an empty string? This is a great question to ask during an interview.

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().



Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2
Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1
Example 3:

Input: haystack = "", needle = ""
Output: 0


Constraints:

0 <= haystack.length, needle.length <= 5 * 104
haystack and needle consist of only lower-case English characters.
26 changes: 26 additions & 0 deletions Implement strStr/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
impl Solution {
pub fn str_str(haystack: String, needle: String) -> i32 {
if haystack.contains(&needle) {
let haystackVec: Vec<char> = haystack.chars().collect();
let needleVec: Vec<char> = needle.chars().collect();
if needleVec.len() == 0 { return 0; }

for i in 0..haystackVec.len() {
let mut n = i;
let mut j = 0;
if haystackVec[i] == needleVec[j] {
while j < needleVec.len() {
//println!("entered while i={} and j={}", n, j);
if haystackVec[n] == needleVec[j]{
//println!("haystacktarget -> {} needletarget -> {}", haystackVec[n], needleVec[j]);
j+=1;
n+=1;
if j >= needleVec.len() { return i as i32; }
} else { j = needleVec.len()+1; }
}
}
}
return 0;
} else { return -1; }
}
}
32 changes: 32 additions & 0 deletions Kids with the Greatest Number of Candies/Problem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Given the array candies and the integer extraCandies, where candies[i] represents the number of candies that the ith kid has.

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.



Example 1:

Input: candies = [2,3,5,1,3], extraCandies = 3
Output: [true,true,true,false,true]
Explanation:
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.
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.
Kid 3 has 5 candies and this is already the greatest number of candies among the kids.
Kid 4 has 1 candy and even if he or she receives all extra candies will only have 4 candies.
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.
Example 2:

Input: candies = [4,2,1,1,2], extraCandies = 1
Output: [true,false,false,false,false]
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.
Example 3:

Input: candies = [12,1,12], extraCandies = 10
Output: [true,false,true]


Constraints:

2 <= candies.length <= 100
1 <= candies[i] <= 100
1 <= extraCandies <= 50
12 changes: 12 additions & 0 deletions Kids with the Greatest Number of Candies/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
impl Solution {
pub fn kids_with_candies(candies: Vec<i32>, extra_candies: i32) -> Vec<bool> {
let most_candy = candies.iter().max().unwrap();
let mut return_vec = Vec::new();
for candy in candies.iter(){
if candy + extra_candies >= *most_candy { return_vec.push(true); }
else { return_vec.push(false) }
}

return_vec
}
}
28 changes: 28 additions & 0 deletions Richest Customer Wealth/Problem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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.

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.



Example 1:

Input: accounts = [[1,2,3],[3,2,1]]
Output: 6
Explanation:
1st customer has wealth = 1 + 2 + 3 = 6
2nd customer has wealth = 3 + 2 + 1 = 6
Both customers are considered the richest with a wealth of 6 each, so return 6.
Example 2:

Input: accounts = [[1,5],[7,3],[3,5]]
Output: 10
Explanation:
1st customer has wealth = 6
2nd customer has wealth = 10
3rd customer has wealth = 8
The 2nd customer is the richest with a wealth of 10.
Example 3:

Input: accounts = [[2,8,7],[7,1,3],[1,9,5]]
Output: 17

13 changes: 13 additions & 0 deletions Richest Customer Wealth/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
impl Solution {
pub fn maximum_wealth(accounts: Vec<Vec<i32>>) -> i32 {
let mut most_wealth = 0;
for account in accounts {
let mut account_wealth = 0;
for amount in account {
account_wealth+=amount;
}
if account_wealth > most_wealth { most_wealth = account_wealth; }
}
most_wealth
}
}
26 changes: 26 additions & 0 deletions Running sum of 1d Array/Problem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).

Return the running sum of nums.



Example 1:

Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].
Example 2:

Input: nums = [1,1,1,1,1]
Output: [1,2,3,4,5]
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].
Example 3:

Input: nums = [3,1,2,10,1]
Output: [3,4,6,16,17]


Constraints:

1 <= nums.length <= 1000
-10^6 <= nums[i] <= 10^6
13 changes: 13 additions & 0 deletions Running sum of 1d Array/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
impl Solution {
pub fn running_sum(nums: Vec<i32>) -> Vec<i32> {
let mut run_vec = Vec::new();
for (i, num) in nums.iter().enumerate() {
let mut new_num = 0;
for j in 0..i+1 {
new_num = nums[j] + new_num;
}
run_vec.push(new_num);
}
run_vec
}
}
32 changes: 32 additions & 0 deletions Search Insert Position/Problem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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.



Example 1:

Input: nums = [1,3,5,6], target = 5
Output: 2
Example 2:

Input: nums = [1,3,5,6], target = 2
Output: 1
Example 3:

Input: nums = [1,3,5,6], target = 7
Output: 4
Example 4:

Input: nums = [1,3,5,6], target = 0
Output: 0
Example 5:

Input: nums = [1], target = 0
Output: 0


Constraints:

1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums contains distinct values sorted in ascending order.
-104 <= target <= 104
18 changes: 18 additions & 0 deletions Search Insert Position/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
impl Solution {
pub fn search_insert(nums: Vec<i32>, target: i32) -> i32 {
if target <= nums[0]{ return 0; }
let mut retVal = 0;
for i in 0..nums.len() {
if nums[i] == target { retVal = i; }
else if i+1 < nums.len(){
if nums[i] < target && nums[i+1] > target {
retVal = i+1;
}
}
}
if retVal == 0 { retVal = nums.len(); }
retVal as i32
}


}
26 changes: 26 additions & 0 deletions Shuffle the Array/Problem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn].

Return the array in the form [x1,y1,x2,y2,...,xn,yn].



Example 1:

Input: nums = [2,5,1,3,4,7], n = 3
Output: [2,3,5,4,1,7]
Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].
Example 2:

Input: nums = [1,2,3,4,4,3,2,1], n = 4
Output: [1,4,2,3,3,2,4,1]
Example 3:

Input: nums = [1,1,2,2], n = 2
Output: [1,2,1,2]


Constraints:

1 <= n <= 500
nums.length == 2n
1 <= nums[i] <= 10^3
11 changes: 11 additions & 0 deletions Shuffle the Array/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
impl Solution {
pub fn shuffle(nums: Vec<i32>, n: i32) -> Vec<i32> {
let mut new_nums = Vec::new();
for i in 0..n {
new_nums.push(nums[i as usize]);
let n_index = (i as i32)+n;
new_nums.push(nums[n_index as usize]);
}
new_nums
}
}

0 comments on commit 986f764

Please sign in to comment.