LeetCode——贪心算法

//LeetCode 455 Assign Cookies
	int findContentChildren(vector& g, vector& s) {
		//sort默认是从小到大排序
		sort(g.begin(), g.end(), greater());
		sort(s.begin(), s.end(), greater());

		int si = 0, gi = 0;
		int res = 0;
		while (gi < g.size() && si < s.size())
		{
			if (s[si] >= g[gi])
			{
				res++;
				si++;
				gi++;
			}
			else
				gi++;
		}
		return res;
	}
	//LeetCode  392  Is Subsuqence
	bool isSubsequence(string s, string t) {

		if (s.size() > t.size())
			return false;

		int si = 0, ti = 0;
		while (si < s.size() && ti < t.size())
		{
			if (s[si] == t[ti])
			{
				si++;
				ti++;
			}
			else
				ti++;
		}
		return si == s.size();
	}

你可能感兴趣的:(c++)