Skip to content

Commit b0ffb4d

Browse files
committed
Add comments to explain the solutions
1 parent 44c7131 commit b0ffb4d

File tree

3 files changed

+85
-17
lines changed

3 files changed

+85
-17
lines changed

Diff for: src/grayCode/grayCode.cpp

+65-4
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,50 @@
2727

2828
#include <stdio.h>
2929
#include <stdlib.h>
30+
#include <time.h>
3031
#include <iostream>
3132
#include <vector>
3233
using namespace std;
3334

34-
35-
vector<int> grayCode(int n) {
35+
/*
36+
* I designed the following stupid algorithm base on the blow observation
37+
*
38+
* I noticed I can use a `mirror-like` binary tree to figure out the gray code.
39+
*
40+
* For example:
41+
*
42+
* 0
43+
* __/ \__
44+
* 0 1
45+
* / \ / \
46+
* 0 1 1 0
47+
* So, the gray code as below: (top-down, from left to right)
48+
*
49+
* 0 0 0
50+
* 0 0 1
51+
* 0 1 1
52+
* 0 1 0
53+
*
54+
* 0
55+
* _____/ \_____
56+
* 0 1
57+
* __/ \__ __/ \__
58+
* 0 1 1 0
59+
* / \ / \ / \ / \
60+
* 0 1 1 0 0 1 1 0
61+
*
62+
* So, the gray code as below:
63+
*
64+
* 0 0 0 0
65+
* 0 0 0 1
66+
* 0 0 1 1
67+
* 0 0 1 0
68+
* 0 1 1 0
69+
* 0 1 1 1
70+
* 0 1 0 1
71+
* 0 1 0 0
72+
*/
73+
vector<int> grayCode01(int n) {
3674
vector<int> v;
3775
//n = 1<<n;
3876

@@ -56,6 +94,29 @@ vector<int> grayCode(int n) {
5694
return v;
5795
}
5896

97+
/*
98+
* Actually, there is a better way.
99+
* The mathematical way is: (num >> 1) ^ num;
100+
* Please refer to http://en.wikipedia.org/wiki/Gray_code
101+
*/
102+
vector<int> grayCode02(int n) {
103+
vector<int> ret;
104+
int size = 1 << n;
105+
for(int i = 0; i < size; ++i) {
106+
ret.push_back((i >> 1)^i);
107+
}
108+
return ret;
109+
}
110+
111+
//random invoker
112+
vector<int> grayCode(int n) {
113+
srand(time(0));
114+
if (rand()%2){
115+
return grayCode01(n);
116+
}
117+
return grayCode02(n);
118+
}
119+
59120
void printBits(int n, int len){
60121
for(int i=len-1; i>=0; i--) {
61122
if (n & (1<<i)) {
@@ -69,7 +130,7 @@ void printBits(int n, int len){
69130
void printVector(vector<int>& v, int bit_len)
70131
{
71132
vector<int>::iterator it;
72-
133+
73134
for(it=v.begin(); it!=v.end(); ++it){
74135
//bitset<bit_len> bin(*it);
75136
printBits(*it, bit_len);
@@ -83,7 +144,7 @@ int main(int argc, char** argv)
83144
{
84145
int n = 2;
85146
if (argc>1){
86-
n = atoi(argv[1]);
147+
n = atoi(argv[1]);
87148
}
88149
vector<int> v = grayCode(n);
89150
printVector(v, n);

Diff for: src/longestSubstringWithoutRepeatingCharacters/longestSubstringWithoutRepeatingCharacters.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@
1515
#include <string>
1616
#include <map>
1717
using namespace std;
18-
18+
/*
19+
* Idea:
20+
*
21+
* Using a map store each char's index.
22+
*
23+
* So, we can be easy to know the when duplication and the previous duplicated char's index.
24+
*
25+
* Then we can take out the previous duplicated char, and keep tracking the maxiumn length.
26+
*
27+
*/
1928
int lengthOfLongestSubstring1(string s) {
2029
map<char, int> m;
2130
int maxLen = 0;
@@ -66,6 +75,6 @@ int main(int argc, char** argv)
6675
s = argv[1];
6776
cout << s << " : " << lengthOfLongestSubstring(s) << endl;
6877
}
69-
78+
7079
return 0;
7180
}

Diff for: src/wordSearch/wordSearch.cpp

+9-11
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,35 @@
3131
#include <string>
3232
using namespace std;
3333

34+
//Recursive backtracking algorithm
3435
bool exist(vector<vector<char> > &board, string word, int idx, int row, int col, vector< vector<int> > &mask) {
3536
int i = row;
3637
int j = col;
3738
if (board[i][j] == word[idx] && mask[i][j]==0 ) {
38-
mask[i][j]=1;
39+
mask[i][j]=1; //mark the current char is matched
3940
if (idx+1 == word.size()) return true;
40-
idx++;
41+
//checking the next char in `word` through the right, left, up, down four directions in the `board`.
42+
idx++;
4143
if (( i+1<board.size() && exist(board, word, idx, i+1, j, mask) ) ||
4244
( i>0 && exist(board, word, idx, i-1, j, mask) ) ||
4345
( j+1<board[i].size() && exist(board, word, idx, i, j+1, mask) ) ||
4446
( j>0 && exist(board, word, idx, i, j-1, mask) ) )
4547
{
4648
return true;
4749
}
48-
mask[i][j]=0;
50+
mask[i][j]=0; //cannot find any successful solution, clear the mark. (backtracking)
4951
}
50-
//if (board[i][j] != word[idx]) {
51-
// mask[i][j]=0;
52-
//}
52+
5353
return false;
5454
}
5555

5656
bool exist(vector<vector<char> > &board, string word) {
5757
if (board.size()<=0 || word.size()<=0) return false;
5858
int row = board.size();
5959
int col = board[0].size();
60-
vector< vector<int> > mask;
61-
for(int i=0; i<row; i++) {
62-
vector<int> v(col);
63-
mask.push_back(v);
64-
}
60+
//using a mask to mark which char has been selected.
61+
//do not use vector<bool>, it has big performance issue, could cause Time Limit Error
62+
vector< vector<int> > mask(row, vector<int> col);
6563

6664
for(int i=0; i<board.size(); i++) {
6765
for(int j=0; j<board[i].size(); j++){

0 commit comments

Comments
 (0)