题目:
最长和谐子序列
解题思路:
赋予两个变量,按照题意模拟过程,找出符合要求的数组,与重复元素子数组进行比较,确定长度值最大的子数组。
代码:
int cmp (const void a , const void b){
return (int) a > (int) b;
}
int findLHS(int* nums, int numsSize){
int i, j;
int count = 0;
qsort(nums , numsSize , sizeof(int) , cmp);
for (i = 0 ; i < numsSize; i++){
for (j = i+1 ; j < numsSize ; j++){
if (nums[j]-nums[i] <2){
continue;
}
else{
break;
}
}
if(nums[j-1]-nums[i]){
count=fmax(j-i,count);
}
}
return count;
}
链接:
借助变量解题
Leetcode 题解 - 双变量法
参与讨论
还没有评论呢!