码迷,mamicode.com
首页 > 编程语言 > 详细

LeetCode的medium题集合(C++实现)十五

时间:2015-05-28 12:42:36      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:c++   leetcode   递归   回溯   

1 Subsets
Given a set of distinct integers, nums, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,If nums = [1,2,3], a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
首先对该数据集进行排序,利用循环遍历得到n个有1个元素的子集,并且采用递归的方法向得到的子集中增加数据,得到元素更多的子集。

 void dst(int i,vector<int>& res, vector<int>& nums, vector<vector<int>>& result)
    {
        int len=nums.size();
        if(i>=len) return ;
        for(;i<len;i++)
        {
            res.push_back(nums[i]);
            result.push_back(res);
            dst(i+1,res,nums,result);
            res.pop_back();
        }
    }
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<vector<int> > result;
        vector<int> res;
        sort(nums.begin(),nums.end());
        int len=nums.size();
        for(int i=0;i<len;i++)
        {
            res.push_back(nums[i]);
            result.push_back(res);
            dst(i+1,res,nums,result);
            res.pop_back();
        }
        res.clear();
        result.push_back(res);
        return result;
    }

2 Word Search
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,Given board =
[
[“ABCE”],
[“SFCS”],
[“ADEE”]
]
word = “ABCCED”, -> returns true,
word = “SEE”, -> returns true,
word = “ABCB”, -> returns false.
该题类似迷宫问题,采用回溯的方法,分别向上下左右四个方向搜索下一个字母,采用一个额外的容器标记之前该字母是否已访问过。

 bool search(vector<vector<char>>& board, int i, int j, string word,vector<vector<bool> >& visited) 
   {  
      if(word.length()==0) 
      return true;
    //四个走向  
       vector<vector<int> >direction = { {-1,0},{1,0},{0,-1},{0,1}};  

    for (int k=0;k<direction.size();k++) {  
        int ii = i + direction[k][0];  
        int jj = j + direction[k][1];  
        if ( ii>=0&&ii<board.size()&&  
             jj>=0&&jj<board[i].size()&&  
             board[ii][jj]==word[0]&&  
             visited[ii][jj]==false) {  
            visited[ii][jj] = true;  
            if (word.length()==1 || search(board,ii,jj,word.substr(1),visited)) {  
                return true;  
            }  
            visited[ii][jj] = false;  
        }  
    }  
    return false;  
  }  
    bool exist(vector<vector<char>>& board, string word) {
       if(board.size()==0) return false;
       if(word.length()==0) return true;
       int m=board.size();
       int n=board[0].size();
       vector<vector<bool> > visited(m,vector<bool>(n,false));
       for(int i=0;i<m;i++)
       {
           for(int j=0;j<n;j++)
           {
               if(board[i][j]==word[0])
               {
                   visited[i][j]=true;
                   if(word.length()==1||search(board,i,j,word.substr(1),visited))
                   return true;
                   visited[i][j]=false;
               }
           }
       }
       return false;
    }

LeetCode的medium题集合(C++实现)十五

标签:c++   leetcode   递归   回溯   

原文地址:http://blog.csdn.net/zhulong890816/article/details/46119457

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!