[面试算法] 动态规划

LCS(最长公共子序列)

int LCS(string s1, string s2){
	int len1 = s1.size(), len2 = s2.size();
	vector> lcs(len1 + 1, vector(len2 + 1));
	for (int i = 0; i <= len1; i++)
		lcs[i][0] = 0;
	for (int i = 0; i <= len2; i++){
		lcs[0][i] = 0;
	}
	for (int i = 1; i <= len1; i++){
		for (int j = 1; j <= len2; j++){
			if (s1[i-1] == s2[j-1]){
				lcs[i][j] = lcs[i - 1][j - 1] + 1;
			}
			else{
				lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1]);
			}
		}
	}
	return lcs[len1][len2];
}

LCS(最长公共子串)

string LCS(string s1, string s2){
	int len1 = s1.size(), len2 = s2.size();
	vector> lcs(len1 + 1, vector(len2 + 1, 0));
	for (int i = 0; i <= len1; i++)
		lcs[i][0] = 0;
	for (int i = 0; i <= len2; i++){
		lcs[0][i] = 0;
	}
	int last = 0, len = 0;
	for (int i = 1; i <= len1; i++){
		for (int j = 1; j <= len2; j++){
			if (s1[i - 1] == s2[j - 1]){ //注意矩阵和字符串索引的对应
				lcs[i][j] = lcs[i - 1][j - 1] + 1;
			}
			else{
				lcs[i][j] = 0;
			}
			if (lcs[i][j] > len){
				len = lcs[i][j];
				last = i;
			}
		}
	}
	return s1.substr(last - len, len);
}

LIS(最长递增子序列)

int lengthOfLIS(vector& nums) {
	int maxlen = 0, len = nums.size();
	vector dp(len, 1);
	for (int i = 0; i < len; i++) {
		for (int j = 0; j < i; j++) {
			if (nums[i] > nums[j])
				dp[i] = max(dp[i], dp[j] + 1);
		}
		maxlen = max(maxlen, dp[i]);
	}
	return maxlen;
}

字符串编辑距离(字符串相似度)

int minDistance(string word1, string word2) {
	int len1 = word1.size(), len2 = word2.size();
	vector> dist(len1 + 1, vector(len2 + 1, 0));
	for (int i = 1; i <= len1; i++){
		dist[i][0] = i;
	}
	for (int i = 1; i <= len2; i++){
		dist[0][i] = i;
	}
	int f;
	for (int i = 1; i <= len1; i++){
		for (int j = 1; j <= len2; j++){
			if (word1[i - 1] == word2[j - 1])
				f = 0;
			else
				f = 1;
			dist[i][j] = min(dist[i - 1][j] + 1, dist[i][j - 1] + 1);
			dist[i][j] = min(dist[i][j], dist[i - 1][j - 1] + f);
		}
	}
	return dist[len1][len2];
}

交替字符串:

bool isInterleave(string s1, string s2, string s3) {
	int len1 = s1.size(), len2 = s2.size();
	if (len1 + len2 != s3.size())
		return false;
	vector> dp(len1 + 1, vector(len2 + 1, false));
	dp[0][0] = true;
	for (int i = 1; i <= len1; i++){
		dp[i][0] = dp[i - 1][0] && (s1[i - 1] == s3[i - 1]);
	}
	for (int j = 1; j <= len2; j++){
		dp[0][j] = dp[0][j - 1] && (s2[j - 1] == s3[j - 1]);
	}
	for (int i = 1; i <= len1; i++){
		for (int j = 1; j <= len2; j++){
			dp[i][j] = (dp[i][j - 1] && s2[j - 1] == s3[i + j - 1]) || (dp[i - 1][j] && s1[i - 1] == s3[i + j - 1]);
		}
	}
	return dp[len1][len2];
}

子序列数目(Distinct Subsequences)

int numDistinct(string s, string t) {
	int len1 = s.size(), len2 = t.size();
	vector> dist(len1 + 1, vector(len2 + 1, 0));
	for (int i = 0; i <= len1; i++){
		dist[i][0] = 1;
	}
	for (int i = 1; i <= len1; i++){
		for (int j = 1; j <= len2; j++){
			dist[i][j] = dist[i - 1][j];
			if (s[i - 1] == t[j - 1]){
				dist[i][j] += dist[i - 1][j - 1];
			}
		}
	}
	return dist[len1][len2];
}
GitHub-Leetcode: https://github.com/wenwu313/LeetCode

你可能感兴趣的:(Leetcode,面试算法,LeetCode)