算法篇-------回溯2

二进制手表(简单)

力扣题401
这一题还是用DFS(深度优先搜索), 但是是组合问题。

使用DFS如何解题呢?
在这里插入图片描述

class Solution {
public:
    //cur表示用的是当前bit位, 组合的核心就是:
    //当前的bit位用了, 后面的盒子只能填后面的bit位
    void DFS(int n, vector<string>& ans, vector<int>& book, int cur)
    {
        //表示盒子填满了
        if(n  == 0)
        {
           int hour = book[0] * 1 + book[1] * 2 + book[2] * 4 + book[3] * 8;
           int minute = book[4] * 1 +  book[5] * 2 +  book[6] * 4 + 
            book[7] * 8 +  book[8] * 16 +  book[9] * 32;

           if(hour < 12 && minute < 60)
           {
               string s;
               s += to_string(hour);
               s += ":";
               if(minute < 10)
                   s += "0";
               s += to_string(minute);
               ans.push_back(s);
           }

           return;
        }

        for(int i = cur; i < 10; i++)
        {
            if(book[i] == 0)
            {
                book[i] = 1;
                DFS(n - 1, ans, book, i + 1);
                book[i] = 0;
            }
        }
    }

    vector<string> readBinaryWatch(int turnedOn) 
    {
        vector<int> book(10, 0);
        vector<string> ans;
        DFS(turnedOn, ans, book,0);

        return ans;
    }
};

使用常规的解法,就是简单的遍历:
在这里插入图片描述

class Solution {
public:
    int getBitOne(int n)
    {
         int count = 0;
         while(n)
         {
             n = n & (n-1);
             count++;
         }
         return count;
    }

    vector<string> readBinaryWatch(int turnedOn) 
    {
        vector<string> ans;

        for(int i = 0; i < 12; i++)
        {
            for(int j = 0; j < 60; j++)
            {
                if(getBitOne(i) + getBitOne(j) == turnedOn)
                {
                    string s = to_string(i);
                    s += ":";
                    if(j < 10)
                       s += "0";
                    s += to_string(j);

                    ans.push_back(s);
                }
            }
        }

        return ans;
    }
};

力扣39. 组合总和(中等)

力扣39-----组合总和

在这里插入图片描述
而且的话,题目的默认条件是升序。

class Solution {
public:
    vector<vector<int>> ans;
    void DFS( vector<int> v, const vector<int>& candidates, int target, int cur, int curSum)
    {
        if(curSum >= target)
        {
              if(curSum == target)
                     ans.push_back(v);

              return;
        }

        for(int i = cur; i < candidates.size(); i++)
        {
            if(candidates[i] > target)
                     continue;
            
            //去重
            if(i - 1 >= 0 && candidates[i - 1] == candidates[i])
                         continue;
            
            v.push_back(candidates[i]);
            //这里的下标i是使用的位置, 然后还是可以使用的
            DFS(v, candidates, target, i, curSum + candidates[i]);
            v.pop_back();
        }
    }

    vector<vector<int>> combinationSum(vector<int>& candidates, int target) 
    {
        vector<int> v;
        DFS(v, candidates, target, 0, 0);
        return ans;    
    }
};

力扣1079,活字印刷

力扣题
在这里插入图片描述

class Solution {
public:
    int cnt = 0;

    void DFS(const string& tiles, vector<bool>& book)
    {
          
          for(int i = 0; i < tiles.size(); i++)
          {
              //这个是同一层的去重
              if(i - 1 >= 0 && tiles[i - 1] == tiles[i]
                && book[ i - 1] == false)
                     continue;

              if(book[i] == false)
              {
                  book[i] = true;
                  cnt++;
                  DFS(tiles, book);
                  book[i] = false;
              }
          }
    }

    int numTilePossibilities(string tiles) 
    {
        // 示例给的是排好序的, 实际上并不会
        sort(tiles.begin(), tiles.end());
        
        vector<bool> book(tiles.size(), 0);
        DFS(tiles, book);

        return cnt;
    }
};

力扣51,n皇后问题

l力扣51
在这里插入图片描述

class Solution {
public:
    void DFS(int n, int curRow, vector<pair<int, int>> ans,
             vector<vector<pair<int, int>>>& allSolution)
    {
          if(curRow == n )
          {
             allSolution.push_back(ans);
             return;
          }

          for(int i = 0; i < n; i++)
          {
              if(isValid(ans, curRow, i))
              {
                  ans.push_back({curRow, i});
                  DFS(n, curRow + 1, ans, allSolution);
                  ans.pop_back();
              }
          }

    }
    
    bool isValid(const vector<pair<int, int>>& ans, int row, int col)
    {
        for(auto e : ans)
        {
            if(e.second == col || e.first - e.second == row -col
            || e.first + e.second == row + col)
               return false;
        }   

        return true;
    }

    void  tansferResult(vector<vector<string>>& result, vector<vector<pair<int, int>>>& allSolution, int n)
    {
          for(auto& e1 : allSolution)
          {
               vector<string> ret(n, string(n, '.'));
               for(auto& e2 : e1)
               {
                   ret[e2.first][e2.second] = 'Q';
               }
               result.push_back(ret);
          }

    }

    vector<vector<string>> solveNQueens(int n) 
    {
        vector<pair<int, int>>  ans;
        vector<vector<pair<int, int>>> allSolution;
        DFS(n, 0, ans, allSolution);

        vector<vector<string>>  result;
        tansferResult(result, allSolution, n);

        return result;
     }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

通过全部用例

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值