小white刷题记——LeetCodeHot100_30

文章介绍了如何使用回溯法解决一道LeetCode上的编程题目,涉及在字符串中查找由给定单词数组的所有单词组成的连续子串。首先尝试直觉解法,然后转向使用哈希表和回溯策略,通过维护两个哈希映射来跟踪单词出现的状态,有效地找出所有匹配的子串起始位置。
摘要由CSDN通过智能技术生成

思路:读完题最直接的思路就是,建立一个set集合,把words数组里可能组成的所有字符组合均存放在这个集合里,然后遍历一遍字符串s,就可以了。但是这种思路难点就在于words包含的单词个数不定。常规没办法求出所有可能的组合,但是如果用上回溯感觉没准应该能做出来,但是目前没有什么思路,所以按照其他大佬的思路写了一遍这道题。

学习思路:详细通俗的思路分析,多解法 - 串联所有单词的子串 - 力扣(LeetCode)

具体代码:

vector<int> findSubstring(string s, vector<string>& words) {
	    vector<int> res;
	    int wordNum = words.size();
	    int wordSize = words[0].size();
	    int wordLength = wordSize * wordNum;
	    if (words.size() == 0)
	    {
	    	return res;
	    }
	    unordered_map<string, int> m1;
	    for (int i = 0; i < wordNum; i++)
	    {
	    	m1[words[i]]++;
	    }
	    unordered_map<string, int> m2;
	    for (int i = 0; (i + wordLength) <= s.size(); i++)
	    {
	    	int j = 0;
	      	for (j = i ; j < (i + wordLength) ; j = j + wordSize)
	    	{
			    string tmps = s.substr(j, wordSize);
			    if (m1[tmps] == 0)
		    	{
			    	break;
			    }
		    	else
			    {
			    	m2[tmps]++;
			    	if (m1[tmps]<m2[tmps])
			    	{
				    	break;
			    	}
			    }
		    }
	    	if (j == (i + wordLength))
		    {
	        	res.push_back(i);
		    }
	    m2.clear();
	    }
	    return res;
    }

“ # 设置按钮的背景颜色 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、付费专栏及课程。

余额充值