Skip to content

Commit 5b4a6e5

Browse files
committed
idx
2 parents 616a0fc + 9db15d8 commit 5b4a6e5

File tree

87 files changed

+2216
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+2216
-0
lines changed

problems/add_binary/solution.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def addBinary(self, a: str, b: str) -> str:
3+
return str(bin(int(a, 2) + int(b, 2)))[2:]

problems/add_binary/solution.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
impl Solution {
2+
pub fn add_binary(a: String, b: String) -> String {
3+
let a_b = u128::from_str_radix(&a, 2).unwrap();
4+
let b_b = u128::from_str_radix(&b, 2).unwrap();
5+
6+
let sum = a_b + b_b;
7+
format!("{sum:b}")
8+
}
9+
}

problems/add_strings/solution.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
impl Solution {
2+
pub fn add_strings(mut num1: String, mut num2: String) -> String {
3+
4+
let leading_zs = (num1.len() as i32 - num2.len() as i32).abs();
5+
6+
let mut zeros = String::with_capacity(leading_zs as usize);
7+
8+
for _ in 0..leading_zs {
9+
zeros.push('0');
10+
}
11+
12+
if num1.len() < num2.len() {
13+
num1 = zeros + &num1;
14+
} else if num1.len() > num2.len() {
15+
num2 = zeros + &num2;
16+
}
17+
18+
let nums1rev = num1.chars().rev().collect::<Vec<char>>();
19+
let nums2rev = num2.chars().rev().collect::<Vec<char>>();
20+
21+
22+
let mut solution_rev: Vec<u32> = Vec::with_capacity(nums1rev.len() + 1);
23+
// Both vecs will be the same length due to adding leading 0s
24+
//
25+
let mut carry: u32 = 0;
26+
for i in 0..nums1rev.len() {
27+
let ans = nums1rev[i].to_digit(10).unwrap() + nums2rev[i].to_digit(10).unwrap() + carry;
28+
29+
carry = 0;
30+
31+
if ans < 10 {
32+
solution_rev.push(ans);
33+
} else {
34+
carry = 1;
35+
solution_rev.push(ans % 10);
36+
}
37+
38+
}
39+
40+
if carry == 1 {
41+
solution_rev.push(1);
42+
}
43+
44+
return solution_rev.into_iter().rev().map(|n| n.to_string()).collect();
45+
46+
}
47+
}

problems/add_two_integers/solution.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
int sum(int num1, int num2){
4+
return num1 + num2;
5+
}

problems/add_two_integers/solution.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def sum(self, num1: int, num2: int) -> int:
3+
return num1 + num2
4+

problems/add_two_integers/solution.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 sum(num1: i32, num2: i32) -> i32 {
3+
num1 + num2
4+
}
5+
}

problems/add_two_numbers/solution.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
impl Solution {
2+
pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
3+
match (l1, l2) {
4+
(None, None) => None,
5+
(Some(n), None) | (None, Some(n)) => Some(n),
6+
(Some(n1), Some(n2)) => {
7+
let sum = n1.val + n2.val;
8+
if sum < 10 {
9+
Some(Box::new(ListNode {
10+
val: sum,
11+
next: Solution::add_two_numbers(n1.next, n2.next)
12+
}))
13+
} else {
14+
let carry = Some(Box::new(ListNode::new(1)));
15+
Some(Box::new(ListNode {
16+
val: sum - 10,
17+
next: Solution::add_two_numbers(Solution::add_two_numbers(carry, n1.next), n2.next)
18+
}))
19+
}
20+
}
21+
}
22+
}
23+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
impl Solution {
2+
pub fn average(salary: Vec<i32>) -> f64 {
3+
// Get sum
4+
5+
let mut total = 0;
6+
7+
let divisor = salary.len() - 2;
8+
9+
// the lowest has to be ignored initially but must be bigger than 0
10+
let mut lowest = i32::MAX;
11+
12+
let mut highest = i32::MIN;
13+
14+
for sal in salary {
15+
if sal < lowest {
16+
lowest = sal;
17+
}
18+
19+
if sal > highest {
20+
highest = sal;
21+
}
22+
23+
24+
total += sal
25+
}
26+
27+
total -= highest;
28+
29+
total -= lowest;
30+
31+
total as f64 / divisor as f64
32+
}
33+
}

problems/base_7/solution.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
impl Solution {
2+
pub fn convert_to_base7(num: i32) -> String {
3+
let negative = num.is_negative();
4+
let mut num = num.abs() as u32;
5+
6+
let mut n = String::new();
7+
if num == 0 {
8+
return "0".into();
9+
}
10+
11+
while num != 0 {
12+
n.push(char::from_digit(num % 7, 10).unwrap());
13+
num /= 7;
14+
}
15+
16+
if negative {
17+
n.push('-');
18+
}
19+
20+
n.chars().rev().collect()
21+
}
22+
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn max_profit(prices: Vec<i32>) -> i32 {
3+
4+
let (mut min, mut max) = (i32::MAX, i32::MIN);
5+
6+
for price in prices {
7+
max = max.max(price - min);
8+
min = min.min(price);
9+
}
10+
11+
if max < 0 {
12+
max = 0
13+
}
14+
max
15+
}
16+
}

problems/big_countries/solution.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Write your MySQL query statement below
2+
SELECT name, population, World.area
3+
FROM World
4+
WHERE World.area >= 3000000 OR population >= 25000000
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
3+
#include <stdio.h>
4+
5+
// struct TreeNode {
6+
// int val;
7+
// struct TreeNode *left;
8+
// struct TreeNode *right;
9+
// };
10+
11+
typedef struct TreeNode TreeNode;
12+
13+
14+
void traverse(TreeNode* tree, int* nums, int* returnSize) {
15+
16+
17+
if (tree->left) {
18+
traverse(tree->left, nums, returnSize);
19+
20+
};
21+
22+
// Add one after adding the value in the current tree.
23+
nums[(*returnSize)++]=tree->val;
24+
25+
if (tree->right) {
26+
traverse(tree->right, nums, returnSize);
27+
}
28+
29+
}
30+
31+
int* inorderTraversal(struct TreeNode* root, int* returnSize){
32+
int* nums = (int*) malloc(100*sizeof(int));
33+
*returnSize = 0;
34+
35+
if (root) {
36+
traverse(root, nums, returnSize);
37+
}
38+
39+
nums = realloc(nums, sizeof(int) * (*returnSize));
40+
return nums;
41+
}

problems/climbing_stairs/solution.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
pub fn climb_stairs(n: i32) -> i32 {
3+
4+
fn fib(n: i32) -> i32 {
5+
let mut a = 0;
6+
let mut b = 1;
7+
let mut temp;
8+
9+
for _ in 0..n {
10+
temp = a;
11+
a = b;
12+
b = b + temp;
13+
}
14+
return a
15+
}
16+
return fib(n+1)
17+
}
18+
19+
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
pub fn max_area(height: Vec<i32>) -> i32 {
3+
let max_idx = height.len();
4+
let mut start = 0;
5+
let mut end = max_idx - 1;
6+
let mut max_area = height[start].min(height[end]) * (end - start) as i32;
7+
8+
while start < end {
9+
if height[start] > height[end] {
10+
end -= 1;
11+
} else {
12+
start += 1;
13+
}
14+
max_area = max_area.max(height[start].min(height[end]) * (end - start) as i32)
15+
}
16+
17+
max_area
18+
}
19+
20+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::collections::HashSet;
2+
impl Solution {
3+
pub fn contains_duplicate(nums: Vec<i32>) -> bool {
4+
let mut set = HashSet::new();
5+
6+
for num in nums.into_iter() {
7+
if !set.insert(num) {
8+
return true;
9+
}
10+
}
11+
return false;
12+
}
13+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* struct TreeNode *left;
6+
* struct TreeNode *right;
7+
* };
8+
*/
9+
struct TreeNode* sortedArrayToBST(int* nums, int numsSize) {
10+
if (numsSize == 0) {
11+
return 0;
12+
}
13+
14+
struct TreeNode* node = malloc(sizeof(struct TreeNode));
15+
if (numsSize == 1) {
16+
node->val = *nums;
17+
node->left = 0;
18+
node->right = 0;
19+
return node;
20+
}
21+
22+
int half_idx = numsSize / 2;
23+
24+
// Set the middle number to the node's value
25+
node->val = nums[half_idx];
26+
// printArr(nums, half_idx);
27+
// printArr(nums + half_idx + 1, numsSize - (half_idx + 1));
28+
// Sort all numbers up to but not including the middle number
29+
node->left = sortedArrayToBST(nums, half_idx);
30+
// Sort all numbers after the middle number
31+
node->right = sortedArrayToBST(nums + half_idx + 1, numsSize - (half_idx + 1));
32+
33+
return node;
34+
}

problems/count_and_say/solution.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
impl Solution {
2+
pub fn count_and_say(n: i32) -> String {
3+
let mut current = String::from("1");
4+
if n == 1 {
5+
return current;
6+
}
7+
8+
for _ in 0..(n - 1) {
9+
current = runlength_encode(&current);
10+
}
11+
12+
return current;
13+
}
14+
}
15+
16+
fn runlength_encode(s: &str) -> String {
17+
if s.is_empty() {
18+
return String::new();
19+
}
20+
21+
let mut chars = s.chars();
22+
23+
let mut res = String::new();
24+
let mut current_char = chars.next().unwrap();
25+
let mut ch_count = 1;
26+
27+
while let Some(ch) = chars.next() {
28+
if ch == current_char {
29+
ch_count += 1;
30+
} else {
31+
res.push_str(&ch_count.to_string());
32+
res.push(current_char);
33+
current_char = ch;
34+
ch_count = 1;
35+
}
36+
}
37+
38+
res.push_str(&ch_count.to_string());
39+
res.push(current_char);
40+
res
41+
}

problems/counting_bits/solution.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 count_bits(n: i32) -> Vec<i32> {
3+
let mut arr: Vec<i32> = vec![];
4+
5+
for mut i in 0..=n {
6+
7+
8+
let mut ones = 0;
9+
while i != 0 {
10+
i = i & (i - 1);
11+
ones += 1
12+
}
13+
14+
arr.push(ones)
15+
16+
}
17+
18+
arr
19+
}
20+
21+
22+
}

0 commit comments

Comments
 (0)