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

Java | Leetcode Java题解之第324题摆动排序II

题目:

题解:

class Solution {Random random = new Random();public void wiggleSort(int[] nums) {int n = nums.length;int x = (n + 1) / 2;int mid = x - 1;int target = findKthLargest(nums, n - mid);for (int k = 0, i = 0, j = n - 1; k <= j; k++) {if (nums[transAddress(k, n)] > target) {while (j > k && nums[transAddress(j, n)] > target) {j--;}swap(nums, transAddress(k, n), transAddress(j--, n));}if (nums[transAddress(k, n)] < target) {swap(nums, transAddress(k, n), transAddress(i++, n));}}}public int findKthLargest(int[] nums, int k) {int left = 0, right = nums.length - 1;while (left <= right) {int pivot = random.nextInt(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];}public int transAddress(int i, int n) {return (2 * n - 2 * i - 1) % (n | 1);}public int partitionAroundPivot(int left, int right, int pivot, int[] nums) {int pivotValue = nums[pivot];int newPivot = left;swap(nums, pivot, right);for (int i = left; i < right; ++i) {if (nums[i] > pivotValue) {swap(nums, i, newPivot++);}}swap(nums, right, newPivot);return newPivot;}public void swap(int[] a, int i, int j) {int temp = a[i];a[i] = a[j];a[j] = temp;}
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Mac安装nvm以及配置环境变量
  • Docker高级应用讲解
  • ForkJoin框架的解析
  • Spring boot框架指南
  • 04--Docker
  • 【c++】基础知识——快速入门c++
  • pikachu文件包含漏洞
  • Redisson中RAtomicLong的使用场景及例子
  • CSS技巧专栏:一日一例 19 -纯CSS实现超酷的水晶按钮特效
  • NETSDK1045 当前 .NET SDK 不支持将 .NET 8.0 设置为目标。请将 .NET 7.0 或更低
  • 福建聚鼎:开装饰画近几年赚钱不
  • 【学习笔记】A2X通信的协议(二)- A2X配置参数
  • 专栏汇总:神经网络篇(持续更新~)
  • SpinalHDL之仿真(八)
  • Rust中的特殊类型(引用、智能指针等)所占的内存大小
  • canvas 高仿 Apple Watch 表盘
  • classpath对获取配置文件的影响
  • ES6 ...操作符
  • JavaScript 一些 DOM 的知识点
  • SpringBoot几种定时任务的实现方式
  • Yii源码解读-服务定位器(Service Locator)
  • 等保2.0 | 几维安全发布等保检测、等保加固专版 加速企业等保合规
  • 搞机器学习要哪些技能
  • 记一次和乔布斯合作最难忘的经历
  • 京东美团研发面经
  • 使用阿里云发布分布式网站,开发时候应该注意什么?
  • 《TCP IP 详解卷1:协议》阅读笔记 - 第六章
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • !$boo在php中什么意思,php前戏
  • # Swust 12th acm 邀请赛# [ E ] 01 String [题解]
  • ()、[]、{}、(())、[[]]等各种括号的使用
  • (06)金属布线——为半导体注入生命的连接
  • (13)DroneCAN 适配器节点(一)
  • (ctrl.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MDd_DynamicDebug”不匹配值“
  • (C语言)求出1,2,5三个数不同个数组合为100的组合个数
  • (delphi11最新学习资料) Object Pascal 学习笔记---第13章第6节 (嵌套的Finally代码块)
  • (java版)排序算法----【冒泡,选择,插入,希尔,快速排序,归并排序,基数排序】超详细~~
  • (Java数据结构)ArrayList
  • (Redis使用系列) Springboot 在redis中使用BloomFilter布隆过滤器机制 六
  • (二)linux使用docker容器运行mysql
  • (力扣记录)235. 二叉搜索树的最近公共祖先
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • (提供数据集下载)基于大语言模型LangChain与ChatGLM3-6B本地知识库调优:数据集优化、参数调整、Prompt提示词优化实战
  • (一)Linux+Windows下安装ffmpeg
  • (转)ObjectiveC 深浅拷贝学习
  • (转)项目管理杂谈-我所期望的新人
  • (转)重识new
  • (转载)微软数据挖掘算法:Microsoft 时序算法(5)
  • .form文件_一篇文章学会文件上传
  • .Mobi域名介绍
  • .NET Core IdentityServer4实战-开篇介绍与规划
  • .Net Remoting(分离服务程序实现) - Part.3
  • .net web项目 调用webService
  • .NET 依赖注入和配置系统
  • .NET和.COM和.CN域名区别