题目地址:https://leetcode-cn.com/problems/height-checker/
思路:将乱序数组冒泡排序后,对比两数组有多少处不同
代码:
int heightChecker(int* heights, int heightsSize) { int sort[heightsSize]; memcpy(sort, heights, heightsSize*sizeof(heights[0])); for (int i = 0; i < heightsSize; i++) { for (int j = i + 1; j < heightsSize; j++) { if (sort[i] > sort[j]) { int temp = sort[i]; sort[i] = sort[j]; sort[j] = temp; } } } int count = 0; for (int i = 0; i < heightsSize; i++) { if(heights[i] != sort[i]){ count++; } } return count; }
收获:复制数组使用memcpy函数https://www.runoob.com/cprogramming/c-function-memcpy.html