Add comments to explain the solutions
This commit is contained in:
parent
44c713105b
commit
b0ffb4d67e
@ -27,12 +27,50 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
|
||||
vector<int> grayCode(int n) {
|
||||
/*
|
||||
* I designed the following stupid algorithm base on the blow observation
|
||||
*
|
||||
* I noticed I can use a `mirror-like` binary tree to figure out the gray code.
|
||||
*
|
||||
* For example:
|
||||
*
|
||||
* 0
|
||||
* __/ \__
|
||||
* 0 1
|
||||
* / \ / \
|
||||
* 0 1 1 0
|
||||
* So, the gray code as below: (top-down, from left to right)
|
||||
*
|
||||
* 0 0 0
|
||||
* 0 0 1
|
||||
* 0 1 1
|
||||
* 0 1 0
|
||||
*
|
||||
* 0
|
||||
* _____/ \_____
|
||||
* 0 1
|
||||
* __/ \__ __/ \__
|
||||
* 0 1 1 0
|
||||
* / \ / \ / \ / \
|
||||
* 0 1 1 0 0 1 1 0
|
||||
*
|
||||
* So, the gray code as below:
|
||||
*
|
||||
* 0 0 0 0
|
||||
* 0 0 0 1
|
||||
* 0 0 1 1
|
||||
* 0 0 1 0
|
||||
* 0 1 1 0
|
||||
* 0 1 1 1
|
||||
* 0 1 0 1
|
||||
* 0 1 0 0
|
||||
*/
|
||||
vector<int> grayCode01(int n) {
|
||||
vector<int> v;
|
||||
//n = 1<<n;
|
||||
|
||||
@ -56,6 +94,29 @@ vector<int> grayCode(int n) {
|
||||
return v;
|
||||
}
|
||||
|
||||
/*
|
||||
* Actually, there is a better way.
|
||||
* The mathematical way is: (num >> 1) ^ num;
|
||||
* Please refer to http://en.wikipedia.org/wiki/Gray_code
|
||||
*/
|
||||
vector<int> grayCode02(int n) {
|
||||
vector<int> ret;
|
||||
int size = 1 << n;
|
||||
for(int i = 0; i < size; ++i) {
|
||||
ret.push_back((i >> 1)^i);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
//random invoker
|
||||
vector<int> grayCode(int n) {
|
||||
srand(time(0));
|
||||
if (rand()%2){
|
||||
return grayCode01(n);
|
||||
}
|
||||
return grayCode02(n);
|
||||
}
|
||||
|
||||
void printBits(int n, int len){
|
||||
for(int i=len-1; i>=0; i--) {
|
||||
if (n & (1<<i)) {
|
||||
@ -69,7 +130,7 @@ void printBits(int n, int len){
|
||||
void printVector(vector<int>& v, int bit_len)
|
||||
{
|
||||
vector<int>::iterator it;
|
||||
|
||||
|
||||
for(it=v.begin(); it!=v.end(); ++it){
|
||||
//bitset<bit_len> bin(*it);
|
||||
printBits(*it, bit_len);
|
||||
@ -83,7 +144,7 @@ int main(int argc, char** argv)
|
||||
{
|
||||
int n = 2;
|
||||
if (argc>1){
|
||||
n = atoi(argv[1]);
|
||||
n = atoi(argv[1]);
|
||||
}
|
||||
vector<int> v = grayCode(n);
|
||||
printVector(v, n);
|
||||
|
@ -15,7 +15,16 @@
|
||||
#include <string>
|
||||
#include <map>
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
* Idea:
|
||||
*
|
||||
* Using a map store each char's index.
|
||||
*
|
||||
* So, we can be easy to know the when duplication and the previous duplicated char's index.
|
||||
*
|
||||
* Then we can take out the previous duplicated char, and keep tracking the maxiumn length.
|
||||
*
|
||||
*/
|
||||
int lengthOfLongestSubstring1(string s) {
|
||||
map<char, int> m;
|
||||
int maxLen = 0;
|
||||
@ -66,6 +75,6 @@ int main(int argc, char** argv)
|
||||
s = argv[1];
|
||||
cout << s << " : " << lengthOfLongestSubstring(s) << endl;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -31,13 +31,15 @@
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
//Recursive backtracking algorithm
|
||||
bool exist(vector<vector<char> > &board, string word, int idx, int row, int col, vector< vector<int> > &mask) {
|
||||
int i = row;
|
||||
int j = col;
|
||||
if (board[i][j] == word[idx] && mask[i][j]==0 ) {
|
||||
mask[i][j]=1;
|
||||
mask[i][j]=1; //mark the current char is matched
|
||||
if (idx+1 == word.size()) return true;
|
||||
idx++;
|
||||
//checking the next char in `word` through the right, left, up, down four directions in the `board`.
|
||||
idx++;
|
||||
if (( i+1<board.size() && exist(board, word, idx, i+1, j, mask) ) ||
|
||||
( i>0 && exist(board, word, idx, i-1, j, mask) ) ||
|
||||
( j+1<board[i].size() && exist(board, word, idx, i, j+1, mask) ) ||
|
||||
@ -45,11 +47,9 @@ bool exist(vector<vector<char> > &board, string word, int idx, int row, int col,
|
||||
{
|
||||
return true;
|
||||
}
|
||||
mask[i][j]=0;
|
||||
mask[i][j]=0; //cannot find any successful solution, clear the mark. (backtracking)
|
||||
}
|
||||
//if (board[i][j] != word[idx]) {
|
||||
// mask[i][j]=0;
|
||||
//}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -57,11 +57,9 @@ bool exist(vector<vector<char> > &board, string word) {
|
||||
if (board.size()<=0 || word.size()<=0) return false;
|
||||
int row = board.size();
|
||||
int col = board[0].size();
|
||||
vector< vector<int> > mask;
|
||||
for(int i=0; i<row; i++) {
|
||||
vector<int> v(col);
|
||||
mask.push_back(v);
|
||||
}
|
||||
//using a mask to mark which char has been selected.
|
||||
//do not use vector<bool>, it has big performance issue, could cause Time Limit Error
|
||||
vector< vector<int> > mask(row, vector<int> col);
|
||||
|
||||
for(int i=0; i<board.size(); i++) {
|
||||
for(int j=0; j<board[i].size(); j++){
|
||||
|
Loading…
x
Reference in New Issue
Block a user