题解 | #最长公共子数组#
最长公共子数组
https://www.nowcoder.com/practice/6032826d387c4a10ad3690cce5fdb015?tpId=196&tqId=39341&rp=1&ru=/exam/oj&qru=/exam/oj&sourceUrl=%2Fexam%2Foj%3Fdifficulty%3D3%26page%3D3%26pageSize%3D50%26search%3D%26tab%3D%25E7%25AE%2597%25E6%25B3%2595%25E7%25AF%2587%26topicId%3D196&difficulty=3&judgeStatus=undefined&tags=&title=
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param A int整型一维数组 * @param ALen int A数组长度 * @param B int整型一维数组 * @param BLen int B数组长度 * @return int整型 */ int longestCommonSubarry(int* A, int ALen, int* B, int BLen ) { // write code here int result = 0; int temp = 0; for (int i = 0; i < ALen; i++) { for (int j = 0; j < BLen; j++) { if (A[i] == B[j]) { temp++; for (int k = 1; k < (ALen < BLen ? ALen : BLen); k++) { if (A[i + k] == B[j + k]) { temp++; } else { break; } } result = temp > result ? temp : result; } temp = 0; } } return result; }
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param A int整型一维数组
* @param ALen int A数组长度
* @param B int整型一维数组
* @param BLen int B数组长度
* @return int整型
*/
int longestCommonSubarry(int* A, int ALen, int* B, int BLen ) {
// write code here
int result = 0;
int temp = 0;
for (int i = 0; i < ALen; i++) {
for (int j = 0; j < BLen; j++) {
if (A[i] == B[j]) {
temp++;
for (int k = 1; k < (ALen < BLen ? ALen : BLen); k++) {
if (A[i + k] == B[j + k]) {
temp++;
} else {
break;
}
}
result = temp > result ? temp : result;
}
temp = 0;
}
}
return result;
}