I test my code passed the case,but it give me a feedback not


  • 0
    H

    I test my code passed the case "Input:["aa"],"aa""
    but it give me the feedback
    Input: ["aa"], "aa"
    Output: false
    Expected: true
    Here is my code:

    class Solution {
    public:
        bool exist(vector<vector<char> > &board, string word) {
    		int a;
    		int hang = board.size();
    		int lie = board[hang-1].size();
    		int ** status;
    		int count = 1;
    		status = new int*[hang];
    		for(int i = 0;i<hang;i++){
    			status[i] = new int[lie];
    		}
    		SetZero(status,hang,lie);
    		for(int i = 0;i<board.size();i++){
    			for(int j = 0;j<board[i].size();j++){
    				if(board[i][j] == word[0]){
    					status[i][j] = 1;
    					if(SearchNext(board,i,j,status,word,count))
    						return true;					
    				}
    			}
    		}
    		return false;
        }
    	bool SearchNext(vector<vector<char> > board,int i,int j,int ** status,string word,int & count){
    		int hang = board.size();
    		int lie = board[hang-1].size();
    		if(word.length() == count)
    			return true;
    		map<int,int> usefulLocation;
    		GetUsefulLocation(board,i,j,status,usefulLocation);
    		if(usefulLocation.size() == 0){
    			SetZero(status,hang,lie);
    			return false;			
    		}
    		map<int,int>::iterator   it=usefulLocation.begin(); 
    		while(it!=usefulLocation.end()){
    			if(board[it->first][it->second] == word[count]){
    				status[it->first][it->second] = 1;
    				count = count+1;
    				if(SearchNext(board,it->first,it->second,status,word,count))
    					return true;
    			}
    			it++;
    		}
    		SetZero(status,hang,lie);
    		return false;
    
    	}
    	void GetUsefulLocation(vector<vector<char> > board,int i,int j,int ** status,map<int,int>& usefulLocation){
    		int hang = board.size();
    		int lie = board[hang-1].size();
    		if(i-1>0){
    			if(status[i-1][j] == 0)
    				usefulLocation.insert (pair<int,int>(i-1,j));
    		}
    		if(i+1<hang){
    			if(status[i+1][j] == 0)
    				usefulLocation.insert (pair<int,int>(i+1,j));
    		}
    		if(j-1<0){
    			if(status[i][j-1] == 0)
    				usefulLocation.insert (pair<int,int>(i,j-1));
    		}
    		if(j+1<lie){
    			if(status[i][j+1] == 0)
    				usefulLocation.insert (pair<int,int>(i,j+1));
    		}
    	}
    	void SetZero(int ** status,int hang,int lie){
    		for(int i = 0;i<hang;i++){
    			for(int j = 0;j<lie;j++){
    				status[i][j] = 0;
    			}
    		}
    	}
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.