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

回顾前面刷过的算法(4)

今天回顾一下下面三个算法,涉及到了动态规划、合并链表、位运算,好吧,让我们再次手敲一遍

//乘积最大子数组//思路: 维护三个变量,imax最大前缀乘积 imin最小前缀乘积  max最大连续乘积//由于元素有正负,imax和imin需要互换,所以需要单独维护一个max用于记录最大连续乘积public int maxProduct(int[] nums) {if (nums == null || nums.length == 0) {return -1;}if (nums.length == 1) {return nums[0];}int imax = 1, imin = 1, max = Integer.MIN_VALUE;for (int i = 0; i < nums.length; i++) {if (nums[i] < 0) {int temp = imax;imax = imin;imin = temp;}imax = Math.max(nums[i], imax * nums[i]);imin = Math.min(nums[i], imin * nums[i]);max = Math.max(max, imax);}return max;}//排序链表//思路: 先将链表分成多条长度为1(length)的子链表,然后合并两条长度为1的有序子链表,//接着把链表分为多条长度为2(length)的子链表,然后合并两条长度为2的有序子链表,//重复以上步骤,直到length大于等于链表的长度结束public ListNode sortList(ListNode head) {if (head == null) {return null;}int length = 0;ListNode curr = head;while (curr != null) {length++;curr = curr.next;}ListNode dummyHead = new ListNode(0, head);for (int subLength = 1; subLength < length; subLength = subLength * 2) {ListNode prev = dummyHead;curr = dummyHead.next;while (curr != null) {ListNode head1 = curr, head2;for (int i = 1; i < subLength && curr != null && curr.next != null; i++) {curr = curr.next;}head2 = curr.next;curr.next = null;curr = head2;for (int i = 1; i < subLength && curr != null && curr.next != null; i++) {curr = curr.next;}ListNode dailyListHead = null;if (curr != null) {dailyListHead = curr.next;curr.next = null;}prev.next = merged(head1, head2);while (prev.next != null) {prev = prev.next;}curr = dailyListHead;}}return dummyHead.next;}private ListNode merged(ListNode head1, ListNode head2) {ListNode dummyHead = new ListNode(0);ListNode temp = dummyHead, temp1 = head1, temp2 = head2;while (temp1 != null && temp2 != null) {if (temp1.val >= temp2.val) {temp.next = temp2;temp2 = temp2.next;} else {temp.next = temp1;temp1 = temp1.next;}temp = temp.next;}if (temp1 != null) {temp.next = temp1;} else if (temp2 != null) {temp.next = temp2;}return dummyHead.next;}//只出现一次的数字//思路: 利用异或运算进行求解,异或运算性质,0异或任何数都等于本身,任何数与本身异或都等于0public int singleNumber(int[] nums) {int single = 0;for (int i = 0; i < nums.length; i++) {single ^= nums[i];}return single;}
http://www.lryc.cn/news/416495.html

相关文章:

  • SourceTree配置多个不同Remote地址的仓库
  • 【Golang 面试 - 进阶题】每日 3 题(十三)
  • 自定义线程池(二)
  • 【Linux】常见指令
  • uniapp自定义网格布局用于选择金额、输入框焦点事件以及点击逻辑实战
  • 中小学创客室培养学生全面发展
  • AI Agent智能体落地应用测试,一句话即可操控它执行工作
  • 免费的SD-WAN服务
  • gradle安装及配置
  • C-sharp-console-gui-framework:C#控制台应用程序的GUI框架
  • 一文搞懂后端面试之MySQL MVCC【中间件 | 数据库 | MySQL | 隔离级别 | Read View】
  • Mysql执行计划(上)
  • 使用Python+moviepy截取音频片段
  • Java学习Day19
  • 8.达梦数据库常用SQL
  • 深入理解接口测试:实用指南与最佳实践(四)IHRM管理系统实战-项目分析
  • 程序编译及链接
  • route 命令介绍及使用方法
  • 力扣热题100_二叉树_226_翻转二叉树
  • Java SpringBoot 集成 MinIO 资料
  • 鸿蒙系统开发【加解密算法库框架】安全
  • C语言——二维数组和字符数组
  • Python 爬虫入门(九):Scrapy安装及使用「详细介绍」
  • 扩展addr2line程序的功能,group_add2line() 脚本的实现
  • idea中修改项目名称
  • Flink开发语言使用Java还是Scala合适?
  • C++STL专题 vector底层实现
  • 【Linux】装机常用配置
  • oracle库PASSWORD_VERSIONS 对应的加密方式
  • 分享一个基于微信小程序的乡村医疗上门服务预约平台(源码、调试、LW、开题、PPT)