Skip to content

Commit

Permalink
0077 new algo added.
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyubobobo committed Apr 7, 2019
1 parent f35825f commit 6ab1807
Show file tree
Hide file tree
Showing 30 changed files with 72 additions and 800 deletions.
7 changes: 3 additions & 4 deletions 0077-Combinations/cpp-0077/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.13)
project(cpp_0077)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_CXX_STANDARD 14)

set(SOURCE_FILES main.cpp)
add_executable(cpp_0077 ${SOURCE_FILES})
add_executable(cpp_0077 main3.cpp)
9 changes: 6 additions & 3 deletions 0077-Combinations/cpp-0077/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

using namespace std;


/// Naive Recursive
/// Time Complexity: O(n^k)
/// Time Complexity: O(k * C(n, k))
/// Space Complexity: O(k)
class Solution {

private:
vector<vector<int>> res;

Expand All @@ -29,6 +31,7 @@ class Solution {

return;
}

public:
vector<vector<int>> combine(int n, int k) {

Expand All @@ -44,7 +47,7 @@ class Solution {
};


void printVec(const vector<int>& vec){
void print_vec(const vector<int>& vec){

for(int e: vec)
cout << e << " ";
Expand All @@ -55,6 +58,6 @@ int main() {

vector<vector<int>> res = Solution().combine(4,2);
for( int i = 0 ; i < res.size() ; i ++ )
printVec(res[i]);
print_vec(res[i]);
return 0;
}
9 changes: 6 additions & 3 deletions 0077-Combinations/cpp-0077/main2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

using namespace std;


/// Naive Recursive Optimized
/// Time Complexity: O(n^k)
/// Time Complexity: O(k * C(n, k))
/// Space Complexity: O(k)
class Solution {

private:
vector<vector<int>> res;

Expand All @@ -30,6 +32,7 @@ class Solution {

return;
}

public:
vector<vector<int>> combine(int n, int k) {

Expand All @@ -45,7 +48,7 @@ class Solution {
};


void printVec(const vector<int>& vec){
void print_vec(const vector<int>& vec){

for(int e: vec)
cout << e << " ";
Expand All @@ -56,6 +59,6 @@ int main() {

vector<vector<int>> res = Solution().combine(4,2);
for( int i = 0 ; i < res.size() ; i ++ )
printVec(res[i]);
print_vec(res[i]);
return 0;
}
57 changes: 57 additions & 0 deletions 0077-Combinations/cpp-0077/main3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/// Source : https://leetcode.com/problems/combinations/description/
/// Author : liuyubobobo
/// Time : 2019-03-29

#include <iostream>
#include <vector>

using namespace std;


/// Using bit mask
/// Time Complexity: O(2^n * n)
/// Space Complexity: O(1)
class Solution {

public:
vector<vector<int>> combine(int n, int k) {

vector<vector<int>> res;

int LIMIT = (1 << n);
for(int i = 0; i < LIMIT; i ++){

vector<int> tres = get_vector(i);
if(tres.size() == k) res.push_back(tres);
}
return res;
}

private:
vector<int> get_vector(int num){

vector<int> res;
int i = 1;
while(num){
if(num % 2) res.push_back(i);
i ++, num /= 2;
}
return res;
}
};


void print_vec(const vector<int>& vec){

for(int e: vec)
cout << e << " ";
cout << endl;
}

int main() {

vector<vector<int>> res = Solution().combine(4,2);
for( int i = 0 ; i < res.size() ; i ++ )
print_vec(res[i]);
return 0;
}
6 changes: 0 additions & 6 deletions 1012-Complement-of-Base-10-Integer/cpp-1012/CMakeLists.txt

This file was deleted.

34 changes: 0 additions & 34 deletions 1012-Complement-of-Base-10-Integer/cpp-1012/main.cpp

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

56 changes: 0 additions & 56 deletions 1014-Capacity-To-Ship-Packages-Within-D-Days/cpp-1014/main.cpp

This file was deleted.

6 changes: 0 additions & 6 deletions 1015-Numbers-With-1-Repeated-Digit/cpp-1015/CMakeLists.txt

This file was deleted.

Loading

0 comments on commit 6ab1807

Please sign in to comment.