word break

完全没有思路,遂只能百度之。

class Solution {
public:
    bool wordBreak(string s, unordered_set<string> &dict) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        int slen = s.length();
        bool ret[slen + 1];
        fill(ret, ret + slen + 1, false);
        ret[0] = true;
        for (int i = 1; i <= slen; i++) {
            for (int j = i; j > 0; j--) {
                if (ret[j - 1] && dict.count(s.substr(j - 1, i - j + 1)) > 0) {
                    ret[i] = true;
                    break;
                }
            }
        }
        
        return ret[slen];
    }
};

在VS2010中,这段代码是不能跑的,因为不能使用变量赋给数组大小。但这段代码是可以accept通过的。

首先是set中的count函数,这个函数是用来判断set中是否包含要判断的元素的。

int main()
{
	set<int>myset;
	for(int i = 1; i < 5; ++i)
		myset.insert(i*3);
	for(int i = 0; i < 10; ++i)
	{
		cout<<i;
		if(myset.count(i) != 0)
			cout<<"is an element of myset.\n";
		else
			cout<<"is not an element of myset.\n";
	}
	return 0;
}

运行结果如下:



substr(string,i,length)

从一个字符串复制一个从指定位置开始,并具有指定长度的子字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值