C语言-Leetcode1051

半腔热情 提交于 2019-12-02 02:41:01

 

题目地址: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

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!