小white刷题记——LeetCodeHot100_22

思路:这道题的解题可以利用回溯的方法,具体过程如图所示一样,首先第一个字符肯定是 “(” ,然后确定回溯的终止条件,什么时候终止一次回溯呢?以N = 3为例,当字符长度为2*N的时候也就是6的时候,就是一次回溯的终止条件了,因为意味着,括号都添加完了,所以这个时候就需要把此时的字符串放进答案组中了。这里可以取两个方向,left、right,分别代表着字符串中添加“(”和“)”。

具体代码:

class Solution {
public:
    vector<string> result;
    string current;

    void backtracking(int n, int left, int right, string& cur,vector<string>& result) {
	    if (cur.length() == n*2)
	    {
	    	result.push_back(cur);
		    return ;
	    }
	    if (left < n) {
	    	cur.push_back('(');
	    	backtracking(n, left + 1, right, cur, result);
	    	cur.pop_back();
	    }
	    if (right < left) {
	    	cur.push_back(')');
	    	backtracking(n, left, right + 1, cur, result);
	    	cur.pop_back();
    	}
    }

    vector<string> generateParenthesis(int n) {
	    //backtracking();
	    backtracking(n, 0, 0, current, result);
	    return result;
    }
};

“ # 设置按钮的背景颜色 self.m_button1.SetBackgroundColour('#0a74f7') self.m_button1.SetForegroundColour('white') self.m_button2.SetBackgroundColour('#0a74f7') self.m_button2.SetForegroundColour('white') self.m_button3.SetBackgroundColour('#0a74f7') self.m_button3.SetForegroundColour('white') self.m_button4.SetBackgroundColour('#238E23') self.m_button4.SetForegroundColour('white') self.m_button5.SetBackgroundColour('#238E23') self.m_button5.SetForegroundColour('white') self.m_button6.SetBackgroundColour('#238E23') self.m_button6.SetForegroundColour('white') self.m_button7.SetBackgroundColour('#6F4242') self.m_button7.SetForegroundColour('white') self.m_button8.SetBackgroundColour('#6F4242') self.m_button8.SetForegroundColour('white') self.m_button9.SetBackgroundColour('#6F4242') self.m_button9.SetForegroundColour('white') self.m_button10.SetBackgroundColour('#8E6B23') self.m_button10.SetForegroundColour('white') self.m_button11.SetBackgroundColour('#8E6B23') self.m_button11.SetForegroundColour('white') self.m_button12.SetBackgroundColour('#8E6B23') self.m_button12.SetForegroundColour('white') self.m_button13.SetBackgroundColour('#8E6B23') self.m_button13.SetForegroundColour('white') self.m_button14.SetBackgroundColour('#545454') self.m_button14.SetForegroundColour('white') self.m_button15.SetBackgroundColour('#545454') self.m_button15.SetForegroundColour('white') self.m_button16.SetBackgroundColour('#545454') self.m_button16.SetForegroundColour('white') self.m_panel1.SetBackgroundColour('white') # 设置面板的背景颜色”逐行解释代码
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值