当前位置: 首页 > news >正文

C语言 | Leetcode C语言题解之第324题摆动排序II

题目:

题解:

static inline void swap(int *a, int *b) {int c = *a;*a = *b;*b = c;
}static inline int partitionAroundPivot(int left, int right, int pivot, int *nums) {int pivotValue = nums[pivot];int newPivot = left;swap(&nums[pivot], &nums[right]);for (int i = left; i < right; ++i) {if (nums[i] > pivotValue) {swap(&nums[i], &nums[newPivot++]);}}swap(&nums[right], &nums[newPivot]);return newPivot;
}static int findKthLargest(int* nums, int numsSize, int k) {int left = 0, right = numsSize - 1;srand(time(0));while (left <= right) {int pivot = rand() % (right - left + 1) + left;int newPivot = partitionAroundPivot(left, right, pivot, nums);if (newPivot == k - 1) {return nums[newPivot];} else if (newPivot > k - 1) {right = newPivot - 1;} else { left = newPivot + 1;}}return nums[k - 1];
}static inline int transAddress(int i, int n) {return (2 * n - 2 * i - 1) % (n | 1);
}void wiggleSort(int* nums, int numsSize) {int x = (numsSize + 1) / 2;int mid = x - 1;int target = findKthLargest(nums, numsSize, numsSize - mid);for (int k = 0, i = 0, j = numsSize - 1; k <= j; k++) {if (nums[transAddress(k, numsSize)] > target) {while (j > k && nums[transAddress(j, numsSize)] > target) {j--;}swap(&nums[transAddress(k, numsSize)], &nums[transAddress(j--, numsSize)]);}if (nums[transAddress(k, numsSize)] < target) {swap(&nums[transAddress(k, numsSize)], &nums[transAddress(i++, numsSize)]);}}
}
http://www.lryc.cn/news/423987.html

相关文章:

  • Docker③_VMware虚拟机和Docker的备份与恢复
  • 【EMC专题】ESD抑制器简要介绍
  • 贷齐乐系统最新版SQL注入(绕过WAF可union select跨表查询)
  • 『大模型笔记』虚拟机(Virtual Machine,VM)与Docker对比!
  • 基于SpringBoot+Vue框架的租车管理系统
  • HAProxy基本配置及参数实操
  • go-zero中间件的使用
  • 六、ESP32-S3上使用MicroPython点亮WS2812智能LED灯珠并通过web控制改变灯珠颜色优化超时和线程
  • (el-Time-Picker)操作(不使用 ts):Element-plus 中 TimePicker 组件的使用及输出想要时间格式需求的解决过程
  • UIAbility组件基础(一)
  • 神经网络的数学原理
  • Java设计模式-抽象工厂模式-一次性理解透
  • day16-测试自动化之selenium的PO模式
  • Springboot+freemarker大段文本内容动态修改输出,所见即所得
  • Kali Linux网络问题解决与静态IP配置技巧
  • 网络状态码-经验笔记
  • c++ 实现 actor 框架
  • 应对猫咪掉毛挑战,希喂、小米热门宠物空气净化器实测功效PK
  • 0002 保险会计及其特殊性
  • ChatTTS:终极文本转语音工具,支持API!
  • VUE和Element Plus
  • Python学习笔记(五)
  • Linux企业级应用(一)构建企业级Linux应用平台:全面指南
  • LeetCode112 路径总和
  • TI AWR1843 毫米波雷达实物展示
  • 前端JS总结(下)之事件操作
  • 如何妙用哈希表来优化遍历查找过程?刷题感悟总结,c++实现
  • 【设计模式】漫谈设计模式
  • 第N5周:Pytorch文本分类入门
  • SpringBoot 自定义 starter