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

Java | Leetcode Java题解之第457题环形数组是否存在循环

题目:

题解:

class Solution {public boolean circularArrayLoop(int[] nums) {int n = nums.length;for (int i = 0; i < n; i++) {if (nums[i] == 0) {continue;}int slow = i, fast = next(nums, i);// 判断非零且方向相同while (nums[slow] * nums[fast] > 0 && nums[slow] * nums[next(nums, fast)] > 0) {if (slow == fast) {if (slow != next(nums, slow)) {return true;} else {break;}}slow = next(nums, slow);fast = next(nums, next(nums, fast));}int add = i;while (nums[add] * nums[next(nums, add)] > 0) {int tmp = add;add = next(nums, add);nums[tmp] = 0;}}return false;}public int next(int[] nums, int cur) {int n = nums.length;return ((cur + nums[cur]) % n + n) % n; // 保证返回值在 [0,n) 中}
}
http://www.lryc.cn/news/455520.html

相关文章:

  • date:10.4(Content:Mr.Peng)( C language practice)
  • 【K8S系列】Kubernetes 集群中的网络常见面试题
  • Android 无Bug版 多语言设计方案!
  • Nginx02-安装
  • 大模型基础架构
  • MySQL 实验 10:数据查询(3)—— 聚合函数与分组查询
  • 感知机学习算法
  • 2024年双十一有什么好物推荐?双十一必买清单大汇总
  • C语言贪吃蛇
  • SpringBoot宠物咖啡馆平台:创新设计与高效实现
  • 李宏毅深度学习-梯度下降和Batch Normalization批量归一化
  • java集合框架都有哪些
  • 笔记整理—linux进程部分(8)线程与进程
  • 使用 Python 实现遗传算法进行无人机路径规划
  • JAVA基础: synchronized 和 lock的区别、synchronized锁机制与升级
  • 自动驾驶 车道检测实用算法
  • 22.第二阶段x86游戏实战2-背包遍历REP指令详解
  • java 的三种IO模型(BIO、NIO、AIO)
  • 低级语言和高级语言、大小写敏感、静态语言和动态语言、链接
  • P3197 [HNOI2008] 越狱
  • 会声会影导出视频mp4格式哪个最高清,会声会影输出格式哪个清晰
  • Linux:进程调度算法和进程地址空间
  • TCP ---滑动窗口以及拥塞窗口
  • 第十二章--- fixed 和 setprecision 函数、round 函数、进制转换及底层逻辑
  • ASP.NetCore---I18n(internationalization)多语言版本的应用
  • vue3 环境配置vue-i8n国际化
  • 2024 uniapp入门教程 01:含有vue3基础 我的第一个uniapp页面
  • CentOS 7文件系统
  • vue源码解析(源码解析学习大纲)
  • 工行企业网银U盾展期后有两个证书问题的解决方法