LeetCode 2016 85,124,355,126,306

85 Maximal Rectangle

// -----------------See Discuss------------------
// url:
//		https://discuss.leetcode.com/topic/6650/share-my-dp-solution
// Analysis
/*		The DP solution proceeds row by row, starting from the first row. 
		Let the maximal rectangle area at row i and column j be computed by [right(i,j) - left(i,j)]*height(i,j).
		All the 3 variables left, right, and height can be determined 
		by the information from previous row, and also information from the current row. 
		So it can be regarded as a DP solution. The transition equations are:
			left(i,j) = max(left(i-1,j), cur_left), cur_left can be determined from the current row
			right(i,j) = min(right(i-1,j), cur_right), cur_right can be determined from the current row
			height(i,j) = height(i-1,j) + 1, if matrix[i][j]=='1';
			height(i,j) = 0, if matrix[i][j]=='0'
*/
class Solution {
public:
    int maximalRectangle(vector<vector<char> >& matrix)
    {
        int m = matrix.size();
        if (m==0) return 0;
        int n=matrix[0].size();
        int maxA = 0;
        int left[n],right[n],height[n];
        fill_n(left,n,0);fill_n(right,n,n);fill_n(height,n,0);
        for(int i=0;i<m;i++)
        {
            int cur_left = 0, cur_right = n;
            for(int j=0;j<n;j++)
            {
                if (matrix[i][j]=='1') height[j]++;
                else height[j]=0;
            }
            for(int j=0;j<n;j++)
            {
                if(matrix[i][j]=='1') left[j]=max(left[j],cur_left);
                else
                {
                    left[j]=0;cur_left=j+1;
                }
            }
            for(int j=n-1;j>=0;j--)
            {
                if(matrix[i][j]=='1') right[j]=min(right[j],cur_right);
                else
                {
                    right[j]=n;cur_right=j;
                }
            }
            for(int j=0;j<n;j++)
                maxA=max(maxA,(right[j]-left[j])*height[j]);
        }
        return maxA;
    }
};

// -------------WA 1 --------------
// 41 / 66 test cases passed.
// WA reason
// 		初始化的问题, 当是矩形不是正方形的,0行和0列,要初值不是1,而是连续的一串1.
// WA point
/*		Input:
		["11"]
		Output:
		1
		Expected:
		2
*/

// -------------WA 2 --------------
/*		Input:
		["1"]
		Output:
		0
		Expected:
		1
*/

// -------------WA 3--------------------
/* 58 / 66 test cases passed.
		Input:
		["000011101","001111101","000111110"]
		Output:
		8
		Expected:
		9
	Reason:
		面积相同时,只记录了一种长宽情况,应有两种。
		应把左上位置也考虑进去。
*/

// -------------WA 4-------------
/* 61 / 66 test cases passed.
		Input:
		["0001010","0100000","0101001","0011001","1111110","1001011","0100101","1101110","1010101","1110000"]
		Output:
		4
		Expected:
		6
*/

// ----------------WA 5-------------------
/* 63 / 66 test cases passed.
		Input:
		["111111011111111","101101111111111","111111111111111","011111101110111","100111111110111","111111111111111","111011111110111","111100011111010","101100011110101","101111110111011","101111111111111","111011111111111","111000101111111","111111011111111","111111101111101"]
		Output:
		24
		Expected:
		30
Code
class Solution {
public:
    void printGrid(int row,int col,vector<vector<int> > horizon,vector<vector<int> > vertical)
    {
        cout<<"---------------------"<<endl;
        for(int i=0;i<row;i++)
        {
            for(int j=0;j<col;j++)
            {
                cout<<"( "<<horizon[i][j]<<" , "<<vertical[i][j]<<" )";
            }
            cout<<endl;
        }
        cout<<endl;
    }
    int maximalRectangle(vector<vector<char> >& matrix)
    {
        int row = matrix.size();
        if (row==0) return 0;
        int col =matrix[0].size();
        int ans = 0;
        vector<vector<int> >horizon(row+1,vector<int>(col+1,0));
        vector<vector<int> >vertical(row+1,vector<int>(col+1,0));
        horizon[0][0]=matrix[0][0]-'0';
        vertical[0][0]=horizon[0][0];
        ans=vertical[0][0];
        for(int i=1;i<row;i++)
        {
            horizon[i][0]=matrix[i][0]-'0';
            vertical[i][0]=horizon[i][0]*(vertical[i-1][0]+1);
            ans = max(ans,vertical[i][0]);
        }
        for(int j=1;j<col;j++)
        {
            vertical[0][j]=matrix[0][j]-'0';
            horizon[0][j]=vertical[0][j]*(horizon[0][j-1]+1);
            ans=max(ans,horizon[0][j]);
        }
        for(int i = 1;i<row;i++)
            for(int j=1;j<col;j++)
            {
                if (matrix[i][j]=='0') continue;
                int tmphorizon = horizon[i][j-1]+1;
                int tmpvertical = vertical[i-1][j]+1;
                if (tmphorizon>tmpvertical)
                {
                    horizon[i][j]=tmphorizon;
                    vertical[i][j]=1;
                    ans=max(ans,horizon[i][j]);
                }
                else
                {
                    horizon[i][j]=1;
                    vertical[i][j]=tmpvertical;
                    ans=max(ans,vertical[i][j]);
                }
            }
        //printGrid(row,col,horizon,vertical);
        for(int i=1;i<row;i++)
            for(int j=1;j<col;j++)
            {
                if (matrix[i][j]=='0') continue;

                if (matrix[i-1][j-1]!='0' &&matrix[i-1][j]!='0' && matrix[i][j-1]!='0')
                {
                    int tmpHorizon = min(horizon[i][j-1]+1,max(horizon[i-1][j-1]+1,horizon[i-1][j]));
                    int tmpVertical = min(max(vertical[i][j-1],vertical[i-1][j-1]+1),vertical[i-1][j]+1);
                    if (tmpHorizon*tmpVertical >= horizon[i][j]*vertical[i][j])
                    {
                        horizon[i][j]=tmpHorizon;
                        vertical[i][j]=tmpVertical;
                    }
                }
                ans=max(ans,horizon[i][j]*vertical[i][j]);
            }
        //printGrid(row,col,horizon,vertical);
        return ans;
    }
};
*/

124 Binary Tree Maximum Path Sum

/*
	My thoughs:
		the node value can be zero and negative
		1. 	设置一个函数f[node](代码中用的int maxLeafInlucdeRoot(TreeNode* root))
			从下往上,以node为尾且包含node的最大和。
				f[node]=max(node->val, node->val+ f[lchild],node->val+f[rchild])
				// node点自己;node和左儿子;node和右儿子
		2.	在计算f[node]的过程中,更新全局变量res,每次求最大值
				res=max(res, f[node], node->val + f[lchild] + f[rchild])
				// res自己;已经求出的f[node],即单路径; node自己值+左儿子最大+右儿子最大,即以node为根折叠的值
			
*/
class Solution {
public:
    int res = INT_MIN;
    int maxPathSum(TreeNode* root)
    {
        if(root==NULL) return 0;
        int rootMax = maxLeafInlucdeRoot(root);
        return res;
    }
    int maxLeafInlucdeRoot(TreeNode* root)
    // calculate the max value of one child including the root value
    {
        if (root == NULL) return 0;
        if (root->left == NULL && root->right == NULL)
        {
            res = max(res,root->val);
            return root->val;
        }
        int lchildMax = 0, rchildMax = 0;
        if (root->left!=NULL)
        {
            lchildMax = max(lchildMax,maxLeafInlucdeRoot(root->left));
        }
        if (root->right!=NULL)
        {
            rchildMax = max(rchildMax,maxLeafInlucdeRoot(root->right));
        }
        int rootMax = root->val;
        rootMax = max(rootMax, root->val + lchildMax);
        rootMax = max(rootMax, root->val + rchildMax);
        res=max(res,rootMax);
        res=max(res,root->val+lchildMax+rchildMax);
        return rootMax;
    }
};

355 Design Twitter

// ------------------------Seeing Discuss-----------------
// I just retyped the codes
// 我WA的原因:
//		1.没有把时间片加进去,要自动带一个time,每次有东西加进去要time++,并且给postTweet也加上time
//		2.让用户自己follow自己,在找top10时候,可以简单很多
// 		3.使用的map而不是vector,这样就不会出现像我总是RE的情况了。
class Twitter {
unordered_map<int, unordered_set<int>> following;	// follower, followee
unordered_map<int, map<int, int>> tweets;			// userId, [sn, tweetId]
long long sn;

public:
/** Initialize your data structure here. */
Twitter() {
	sn = 0;
}

/** Compose a new tweet. */
void postTweet(int userId, int tweetId) {
	follow(userId, userId);
	tweets[userId].insert(make_pair(sn++, tweetId));
}

/** Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. */
vector<int> getNewsFeed(int userId) {
	vector<int> res;
	map<int, int> top10;
	for (auto iter = following[userId].begin(); iter != following[userId].end(); ++iter) {
		int followee = *iter;
		for (auto iter = tweets[followee].begin(); iter != tweets[followee].end(); ++iter) {

			if (top10.size() > 0 && top10.begin()->first > iter->first && top10.size() > 10)
				break;
			top10.insert(make_pair(iter->first, iter->second));
			if (top10.size() > 10)
				top10.erase(top10.begin());
		}
	}

	for (auto iter = top10.rbegin(); iter != top10.rend(); ++iter)
		res.push_back(iter->second);
	
	return res;
}

/** Follower follows a followee. If the operation is invalid, it should be a no-op. */
void follow(int followerId, int followeeId) {
	following[followerId].insert(followeeId);
}

/** Follower unfollows a followee. If the operation is invalid, it should be a no-op. */
void unfollow(int followerId, int followeeId) {
	if(followerId != followeeId)
		following[followerId].erase(followeeId);
 }
};
// ------------------WA------------------
// 5 / 23 test cases passed.
// Code:
/*
class Twitter {
public:
    Twitter()
    {
        following.clear();
        tweets.clear();
        tweets.resize(1);
        following.resize(1);
    }

    void postTweet(int userId, int tweetId)
    {
        //cout<<"postTweet"<<endl;
        if (userId >= tweets.size())
        {
            tweets.resize(userId+1);
            following.resize(userId+1);
        }
        //cout<<"userId = "<<userId<<endl;
        //cout<<"tweets.size() = "<<tweets.size()<<endl;
        tweets[userId].push_back(tweetId);
        return ;
    }

    vector<int> getNewsFeed(int userId)
    {
        //cout<<"getNewsFeed"<<endl;
        vector<int> ans;ans.clear();
        if (userId>=tweets.size()) return ans;
        set<int> news;
        news.clear();
        set<int>::iterator it;
        for(int i=0;i<tweets[userId].size();i++)
        {
            //cout<<"int i=0;i<tweets[userId].size();i++"<<endl;
            //cout<<userId<<" "<<i<<endl;
            news.insert(tweets[userId][i]);
            //cout<<"insert"<<endl;
            if (news.size()>10)
            {
                it = news.end();it--;
                news.erase(it);
                //cout<<"erase"<<endl;
            }
        }
        for(set<int>::iterator it = following[userId].begin();it!=following[userId].end();it++)
        {
            //cout<<"following[userId].begin()"<<endl;
            int numTweets = tweets[*it].size();
            for(int j=0;j<numTweets;j++)
            {
                news.insert(tweets[*it][j]);
                if (news.size()>10)
                {
                    it = news.end();it--;
                    news.erase(it);
                }
            }
        }
        for(set<int>::reverse_iterator it = news.rbegin();it!=news.rend();it++)
        {
            //cout<<"reverse_iterator it = news.rbegin()"<<endl;
            ans.push_back(*it);
        }
        return ans;
    }

    void follow(int followerId, int followeeId)
    {
        //cout<<"follow"<<endl;
        int fMax=max(followerId,followeeId);
        if (fMax>=following.size())
        {
            tweets.resize(fMax+1);
            following.resize(fMax+1);
        }
        following[followerId].insert(followeeId);
    }

    void unfollow(int followerId, int followeeId)
    {
        //cout<<"unfollow"<<endl;
        set<int>::iterator it = following[followerId].find(followeeId);
        following[followerId].erase(it);
        return ;
    }
private:
    vector< set<int> > following;
    vector< vector<int> >tweets;
};
*/

126 Word Ladder II

// -------------------Seeing Discuss-------------
// 果然跟我想的一样,用的是双向广搜,不过我真是不想写了。。。
// Code:
class Solution {
public:
    vector<vector<string> > findLadders(string beginWord, string endWord, unordered_set<string> &dict)
    {
        vector<vector<string> > paths;
        vector<string> path(1,beginWord);
        if (beginWord == endWord) // corner case
        {
            paths.push_back(path);
            return paths;
        }
        unordered_set<string> forward, backward;
        forward.insert(beginWord);
        backward.insert(endWord);
        unordered_map<string, vector<string> >tree;

        bool reversed = false;
        //make sure the tree generating direction is consistent, since we have to start from the smaller set to accelerate;
        if(buildTree(forward,backward,dict,tree,reversed))
            getPath(beginWord,endWord,tree,path,paths);
        return paths;
    }
private:
    bool buildTree(unordered_set<string> &forward, unordered_set<string> &backward, unordered_set<string> &dict, unordered_map<string, vector<string> > &tree, bool reversed)
    {
        if(forward.empty()) return false;
        if(forward.size()>backward.size())
            return buildTree(backward,forward,dict,tree,!reversed);
        for(auto &word:forward) dict.erase(word);
        for(auto &word:backward) dict.erase(word);
        unordered_set<string> nextLevel;
        bool done = false;
        for(auto &it:forward)
        {
            string word = it;
            for(auto &c:word)
            {
                char c0=c;
                for(c='a';c<='z';c++)
                {
                    if(c!=c0)
                    {
                        if (backward.count(word))
                        {
                            done = true;
                            !reversed ? tree[it].push_back(word) : tree[word].push_back(it);

                        }
                        else if (!done && dict.count(word))
                        {
                            nextLevel.insert(word);
                            !reversed ? tree[it].push_back(word) : tree[word].push_back(it);
                        }

                    }
                }
                c=c0;
            }
        }
        return  done || buildTree(nextLevel, backward, dict, tree, reversed);
    }
    void getPath(string &beginWord, string &endWord, unordered_map<string, vector<string> > &tree, vector<string> &path, vector<vector<string> > &paths) //using reference can accelerate;
        {
            if (beginWord == endWord) paths.push_back(path); //till the end;
            else
            {
                for (auto &it: tree[beginWord])
                {
                    path.push_back(it);
                    getPath(it, endWord, tree, path, paths); //DFS retrieving the path;
                    path.pop_back();
                }
            }
        }
};
// ------------WA-------------
// 24 / 37 test cases passed.
/*WA REASON:
	我的判重方式,当路线内容有重叠时,就识别不出来,漏掉了。
	wa data point:
	Input:
		"cet"
		"ism"
		["kid","tag","pup","ail","tun","woo","erg","luz","brr","gay","sip","kay","per","val","mes","ohs","now","boa","cet","pal","bar","die","war","hay","eco","pub","lob","rue","fry","lit","rex","jan","cot","bid","ali","pay","col","gum","ger","row","won","dan","rum","fad","tut","sag","yip","sui","ark","has","zip","fez","own","ump","dis","ads","max","jaw","out","btu","ana","gap","cry","led","abe","box","ore","pig","fie","toy","fat","cal","lie","noh","sew","ono","tam","flu","mgm","ply","awe","pry","tit","tie","yet","too","tax","jim","san","pan","map","ski","ova","wed","non","wac","nut","why","bye","lye","oct","old","fin","feb","chi","sap","owl","log","tod","dot","bow","fob","for","joe","ivy","fan","age","fax","hip","jib","mel","hus","sob","ifs","tab","ara","dab","jag","jar","arm","lot","tom","sax","tex","yum","pei","wen","wry","ire","irk","far","mew","wit","doe","gas","rte","ian","pot","ask","wag","hag","amy","nag","ron","soy","gin","don","tug","fay","vic","boo","nam","ave","buy","sop","but","orb","fen","paw","his","sub","bob","yea","oft","inn","rod","yam","pew","web","hod","hun","gyp","wei","wis","rob","gad","pie","mon","dog","bib","rub","ere","dig","era","cat","fox","bee","mod","day","apr","vie","nev","jam","pam","new","aye","ani","and","ibm","yap","can","pyx","tar","kin","fog","hum","pip","cup","dye","lyx","jog","nun","par","wan","fey","bus","oak","bad","ats","set","qom","vat","eat","pus","rev","axe","ion","six","ila","lao","mom","mas","pro","few","opt","poe","art","ash","oar","cap","lop","may","shy","rid","bat","sum","rim","fee","bmw","sky","maj","hue","thy","ava","rap","den","fla","auk","cox","ibo","hey","saw","vim","sec","ltd","you","its","tat","dew","eva","tog","ram","let","see","zit","maw","nix","ate","gig","rep","owe","ind","hog","eve","sam","zoo","any","dow","cod","bed","vet","ham","sis","hex","via","fir","nod","mao","aug","mum","hoe","bah","hal","keg","hew","zed","tow","gog","ass","dem","who","bet","gos","son","ear","spy","kit","boy","due","sen","oaf","mix","hep","fur","ada","bin","nil","mia","ewe","hit","fix","sad","rib","eye","hop","haw","wax","mid","tad","ken","wad","rye","pap","bog","gut","ito","woe","our","ado","sin","mad","ray","hon","roy","dip","hen","iva","lug","asp","hui","yak","bay","poi","yep","bun","try","lad","elm","nat","wyo","gym","dug","toe","dee","wig","sly","rip","geo","cog","pas","zen","odd","nan","lay","pod","fit","hem","joy","bum","rio","yon","dec","leg","put","sue","dim","pet","yaw","nub","bit","bur","sid","sun","oil","red","doc","moe","caw","eel","dix","cub","end","gem","off","yew","hug","pop","tub","sgt","lid","pun","ton","sol","din","yup","jab","pea","bug","gag","mil","jig","hub","low","did","tin","get","gte","sox","lei","mig","fig","lon","use","ban","flo","nov","jut","bag","mir","sty","lap","two","ins","con","ant","net","tux","ode","stu","mug","cad","nap","gun","fop","tot","sow","sal","sic","ted","wot","del","imp","cob","way","ann","tan","mci","job","wet","ism","err","him","all","pad","hah","hie","aim","ike","jed","ego","mac","baa","min","com","ill","was","cab","ago","ina","big","ilk","gal","tap","duh","ola","ran","lab","top","gob","hot","ora","tia","kip","han","met","hut","she","sac","fed","goo","tee","ell","not","act","gil","rut","ala","ape","rig","cid","god","duo","lin","aid","gel","awl","lag","elf","liz","ref","aha","fib","oho","tho","her","nor","ace","adz","fun","ned","coo","win","tao","coy","van","man","pit","guy","foe","hid","mai","sup","jay","hob","mow","jot","are","pol","arc","lax","aft","alb","len","air","pug","pox","vow","got","meg","zoe","amp","ale","bud","gee","pin","dun","pat","ten","mob"]
	Output:
		[["cet","cat","can","ian","inn","ins","its","ito","ibo","ibm","ism"]]
	Expected:
		[["cet","get","gee","gte","ate","ats","its","ito","ibo","ibm","ism"],["cet","cat","can","ian","inn","ins","its","ito","ibo","ibm","ism"],["cet","cot","con","ion","inn","ins","its","ito","ibo","ibm","ism"]]
My thoughts:
		1. 	改变一下策略,先把单词都处理一遍;
			map<string,vector<string> >: <单词, 一步变成的单词>
			计算了两个单词的距离,若是1的话,则map[word1]和map[word2]都要更新
		2.	如果单词长度是1的话,就直接返回起止单词
		3.	用队列可以采用TLE的代码中的策略,定义一个Node,放单词和路线
code
class Node {
public:
    string word;
    vector<string> route;
    Node(string w,vector<string> r):word(w),route(r){};
};
class Solution {
public:
    bool calDiff(string x,string y)
    {
        int len = x.size();
        int cnt = 0;
        for(int i=0;i<len;i++)
        {
            if (x[i]!=y[i]) cnt++;
            if (cnt>1) return false;
        }
        return true;
    }
    vector<vector<string> > findLadders(string beginWord, string endWord, unordered_set<string> &wordList)
    {
        map<string,vector<string> > reachEnd;reachEnd.clear();
        vector<vector<string> > result;result.clear();
        if (beginWord.size()==1)
        {
            vector<string> tmp;tmp.push_back(beginWord);tmp.push_back(endWord);
            result.push_back(tmp); return result;
        }
        map<string,set<string> > roads;
        vector<string> dic;dic.clear();
        for(auto i = wordList.begin();i!=wordList.end();i++)
        {
            dic.push_back(*i);
        }
        for(int i=0;i<dic.size();i++)
        {
            for(int j=i+1;j<dic.size();j++)
            {
                if (calDiff(dic[i],dic[j]))
                {
                    roads[dic[i]].insert(dic[j]);
                    roads[dic[j]].insert(dic[i]);
                }
            }
        }
        set<string> flag;flag.clear();flag.insert(beginWord);
        vector<string> tmpRoute;tmpRoute.clear();tmpRoute.push_back(beginWord);
        Node node = Node(beginWord,tmpRoute);
        queue<Node> q;while(!q.empty()) q.pop();q.push(node);
        int resSize = INT_MAX;
        vector<string> setIter;
        while(!q.empty())
        {
            Node curNode = q.front();q.pop();
            setIter.clear();
            cout<<"curNode = "<<curNode.word<<endl;
            for(auto i=roads[curNode.word].begin();i!=roads[curNode.word].end();i++)
            {
                cout<<" i = "<<*i<<endl;
                for(int k = 0;k<curNode.route.size();k++)
                    cout<<curNode.route[k]<<" ";
                cout<<endl;
                if (reachEnd.find(*i)!=reachEnd.end())
                {
                    vector<string> rea = reachEnd[*i];
                    vector<string> tmp = curNode.route;
                    cout<<"resSize = "<<resSize<<endl;
                    cout<<"rea.size()= "<<rea.size()<<endl;
                    cout<<"tmp.size()= "<<tmp.size()<<endl;
                    if(rea.size()+tmp.size()<=resSize)
                    {
                        for(auto r=rea.begin();r!=rea.end();r++)
                        {
                            tmp.push_back(*r);
                        }
                        result.push_back(tmp);
                        cout<<"--------result---------"<<endl;
                    }
                    continue;
                }
                Node newNode = Node(*i,curNode.route);
                newNode.route.push_back(*i);
                if (newNode.word==endWord && newNode.route.size()<=resSize)
                {
                    resSize = newNode.route.size();
                    result.push_back(newNode.route);
                    vector<string> tmpEnd;
                    for(int r = 0;r<newNode.route.size();r++)
                    {
                        vector<string> tmp; tmp.clear();
                        cout<<newNode.route[r]<<endl;
                        for(int j = r;j<newNode.route.size();j++)
                        {
                            tmp.push_back(newNode.route[j]);
                        }
                        reachEnd[newNode.route[r]]=tmp;
                    }
                }
                else
                {
                    if (newNode.route.size()<resSize)
                    {
                        setIter.push_back(*i);
                        q.push(newNode);
                    }
                }
            }
            for(int j=0;j<setIter.size();j++)
            {
                roads[curNode.word].erase(setIter[j]);
            }
        }
        return result;
    }
};


*/

// ------------------------TLE---------------------
/* 19 / 37 test cases passed.
Code
class Node
{
public:
    string word;
    vector<string> route;
    Node(string w,vector<string> r):word(w),route(r){};
};
class Solution {
public:
    vector<vector<string> > findLadders(string beginWord, string endWord, unordered_set<string> &wordList)
    {
        vector<vector<string> > result;result.clear();
        queue<Node> q;
        while(!q.empty()) q.pop();
        if (beginWord == endWord) return result;
        int dicSize = wordList.size();
        if (dicSize==0) return result;
        int len =beginWord.size();
        vector<string> tmpRes;
        if (len ==1)
        {
            tmpRes.clear();
            tmpRes.push_back(beginWord);
            tmpRes.push_back(endWord);
            result.push_back(tmpRes);
            return result;
        }
        unordered_set<string>::iterator it;
        unordered_set<string> diction;diction.clear();
        set<string> used;used.clear();used.insert(beginWord);
        for(it=wordList.begin();it!=wordList.end();it++)
        {
            string s = *it;
            if (s.size()==len) diction.insert(s);
        }
        tmpRes.clear();tmpRes.push_back(beginWord);
        Node node = Node(beginWord,tmpRes);
        q.push(node);
        int resSize = INT_MAX;
        while(!q.empty())
        {
            Node curNode = q.front();q.pop();
            for(it = diction.begin();it!=diction.end();it++)
            {
                string s=*it;
                //if (used.find(s)!=used.end()) continue;
                int cnt = 0;
                for(int i=0;i<len;i++)
                {
                    if (s[i]!=curNode.word[i]) cnt++;
                    if (cnt>1) break;
                }
                if(cnt==1)
                {
                    Node newNode = Node(s,curNode.route);
                    newNode.route.push_back(s);
                    if (s==endWord)
                    {
                        if (newNode.route.size()<=resSize)
                            result.push_back(newNode.route);
                        if (resSize==INT_MAX) resSize=newNode.route.size();
                        //cout<<resSize<<endl;
                    }
                    else
                    {
                        //used.insert(s);
                        if(resSize>newNode.route.size())
                            q.push(newNode);
                    }
                   // for(int i=0;i<newNode.route.size();i++)
                       // cout<<newNode.route[i]<<" ";
                    //cout<<endl;
                }
            }
        }
        return result;
    }
};
*/

306 Additive Number

/* ------------------------Seeing Discuss--------
	果然跟我想的一样,就是朴素的回溯。。但是我真是懒得写了。。。
	url:
		https://discuss.leetcode.com/topic/29872/0ms-concise-c-solution-perfectly-handles-the-follow-up-and-leading-0s
	Analysis:
		use a helper function to add two strings.
		Choose first two number then recursively check.
		Note that the length of first two numbers can't be longer than half of the initial string, so the two loops in the first function will end when i>num.size()/2 and j>(num.size()-i)/2, this will actually save a lot of time.
		Update the case of heading 0s
			e.g. "100010" should return false
*/
class Solution {
public:
    bool isAdditiveNumber(string num)
    {
        for(int i=1;i<=num.size()/2;i++)
        {
            for(int j=1;j<=(num.size()-i)/2;j++)
            {
				// num.substr(i+j)
                if (check(num.substr(0,i),num.substr(i,j),num.substr(i+j)))
                    return true;
            }
        }
        return false;
    }
    bool check(string num1,string num2,string num)
    {
        if(num1.size()>1 && num1[0]=='0' || num2.size()>1 && num2[0]=='0') return false;
        string sum = add(num1,num2);
        if (num == sum) return true;
		// compare()用来看计算的sum和剩下的右边部分是否吻合
		// num.substr(0,sum.size())
        if (num.size()<sum.size() || sum.compare(num.substr(0,sum.size()))!=0) return false;
        else return check(num2,sum,num.substr(sum.size()));
    }
    string add(string n,string m)
    {
        string res;
        int i=n.size()-1,j=m.size()-1,carry = 0;
        while(i>=0 || j>=0)
        {
            int sum = carry +(i>=0?(n[i--]-'0'):0) + (j>=0?(m[j--]-'0'):0);
            res.push_back(sum%10+'0');
            carry = sum/10;
        }
        if (carry) res.push_back(carry+'0');
		// reverse 的使用
        reverse(res.begin(),res.end());
        return res;
    }
};




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值