Skip to content

Commit

Permalink
Added solution for 67. Add Binary: https://leetcode.com/problems/add-…
Browse files Browse the repository at this point in the history
  • Loading branch information
asesh committed Nov 15, 2024
1 parent ca1569d commit 6414a53
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 25 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "444"
endingLineNumber = "444"
startingLineNumber = "445"
endingLineNumber = "445"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -30,8 +30,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "410"
endingLineNumber = "410"
startingLineNumber = "411"
endingLineNumber = "411"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -46,8 +46,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "335"
endingLineNumber = "335"
startingLineNumber = "336"
endingLineNumber = "336"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -62,8 +62,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "303"
endingLineNumber = "303"
startingLineNumber = "304"
endingLineNumber = "304"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -110,8 +110,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "265"
endingLineNumber = "265"
startingLineNumber = "266"
endingLineNumber = "266"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -126,8 +126,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "228"
endingLineNumber = "228"
startingLineNumber = "229"
endingLineNumber = "229"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -142,8 +142,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "187"
endingLineNumber = "187"
startingLineNumber = "188"
endingLineNumber = "188"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down Expand Up @@ -174,8 +174,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "176"
endingLineNumber = "176"
startingLineNumber = "177"
endingLineNumber = "177"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand All @@ -190,8 +190,8 @@
filePath = "algorithm/main.cpp"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "162"
endingLineNumber = "162"
startingLineNumber = "163"
endingLineNumber = "163"
landmarkName = "main(argc, argv)"
landmarkType = "9">
</BreakpointContent>
Expand Down
7 changes: 4 additions & 3 deletions algorithm/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
#include "graph.hpp"

int main(int argc, const char* argv[]) {
invoke_add_binary();

invoke_valid_sudoku();
// invoke_median_of_two_sorted_arrays();

// invoke_valid_sudoku();

// invoke_group_anagrams();

Expand Down Expand Up @@ -64,8 +67,6 @@ int main(int argc, const char* argv[]) {

// invoke_reverse_words_in_a_string();

// invoke_median_of_two_sorted_arrays();

// invoke_four_number_sum();

// invoke_jump_game_ii();
Expand Down
67 changes: 63 additions & 4 deletions algorithm/number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -974,11 +974,11 @@ void invoke_reverse_integer() {
}

/*
nums1: [1,3], nums2: [2] => Output: 2.0
Input: nums1: [1,3], nums2: [2] => Output: 2.0
nums1: [1,2], nums2: [3,4] => Output: 2.50000
Input: nums1: [1,2], nums2: [3,4] => Output: 2.50000
nums1: [1,3,4,5], nums: [2,3,6,7] => Output: 3.50000
Input: nums1: [1,3,4,5], nums: [2,3,6,7] => Output: 3.50000
Process:
Total elements: 8 -> even
mid_index = ((8/2)-1) => [3+1]/2 = 7/2 = 3.5
Expand All @@ -991,7 +991,7 @@ double median_of_two_sorted_arrays(std::vector<int>& nums1, std::vector<int>& nu
}
void invoke_median_of_two_sorted_arrays() {
std::vector<int> nums1 = {1,2}, nums2 = {3,4};
std::cout<<median_of_two_sorted_arrays(nums1, nums2);
std::cout<<"The median of two sorted array is: "<<median_of_two_sorted_arrays(nums1, nums2);
}

/*
Expand Down Expand Up @@ -1749,3 +1749,62 @@ void invoke_h_index() {
std::vector<int> input = {3,0,6,1,5};
std::cout<<"The number of citations needed: "<<h_index(input);
}

/*
Input: [2,2,3,2] => Output: 3
Total: 9 1001
2 10 & 1001 => 1
2
3 11 & 1001 => 1
2
10 | 10 = 10
10 | 11 = 11
Input: [0,1,0,1,0,1,99] => Output: 99
*/
int single_number_ii(std::vector<int>& input) {
int output = 0;

return output;
}
void invoke_single_number_ii() {
std::vector<int> input = {2,2,3,2};
std::cout<<"The single ii is: "<<single_number_ii(input);
}

/*
Input: one: "11", two: "1" => Output: "100"
11
01
---
100
*/
std::string add_binary(std::string& one, std::string& two) {
std::string output;
int first_index = one.size() - 1, second_index = two.size() - 1;
int carry = 0, sum = 0;

while(first_index >= 0 || second_index >= 0) {
sum = carry;

if(first_index >= 0) {
sum += one[first_index--] - '0';
}
if(second_index >= 0) {
sum += one[second_index--] - '0';
}
carry = sum > 1 ? 1 : 0;
output += std::to_string(sum % 2);
}
if(carry) {
output += std::to_string(carry);
}
std::reverse(output.begin(), output.end());

return output;
}
void invoke_add_binary() {
std::string one = "11", two = "1";
std::cout<<"The sum of binary is: "<<add_binary(one, two);
}
10 changes: 10 additions & 0 deletions algorithm/number.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,14 @@ void invoke_spiral_matrix();
*/
void invoke_h_index();

/*
137. Single Number II: https://leetcode.com/problems/single-number-ii
*/
void invoke_single_number_ii();

/*
67. Add Binary: https://leetcode.com/problems/add-binary
*/
void invoke_add_binary();

#endif /* NUMBER_HPP */

0 comments on commit 6414a53

Please sign in to comment.