当前位置: 首页 > 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;}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • HanLP和Jieba区别
  • 单元测试JUnit
  • eslint配置忽略目录和文件
  • 国内开源软件镜像站点参考
  • 【STL】String的底层构造
  • Executable Code Actions Elicit Better LLM Agents
  • 国球荣耀背后的笑与泪——陈梦夺冠现象有感
  • 银河麒麟V10 审计工具 auditd 内存泄漏问题
  • Stable Diffusion绘画 | 图生图-基础使用介绍—提示词反推
  • 监控员工电脑的软件有哪些?四款监控员工电脑的软件分享!
  • fatal error: concurrent map iteration and map write - 关于Go中并发访问Map的操作
  • android compose设置圆角不起作用
  • Visual Studio 和 VSCode 哪个好?
  • mac下载exe后不自动打开虚拟机
  • 全自动真空拌馅机 肠类肉丸类馅料搅拌机:
  • 【391天】每日项目总结系列128(2018.03.03)
  • 【Leetcode】104. 二叉树的最大深度
  • 2017年终总结、随想
  • 230. Kth Smallest Element in a BST
  • C++类中的特殊成员函数
  • CEF与代理
  • ESLint简单操作
  • JavaScript HTML DOM
  • javascript 总结(常用工具类的封装)
  • PHP变量
  • Redis提升并发能力 | 从0开始构建SpringCloud微服务(2)
  • TCP拥塞控制
  • 笨办法学C 练习34:动态数组
  • 编写高质量JavaScript代码之并发
  • 每天10道Java面试题,跟我走,offer有!
  • 写代码的正确姿势
  • 阿里云API、SDK和CLI应用实践方案
  • ​七周四次课(5月9日)iptables filter表案例、iptables nat表应用
  • #### golang中【堆】的使用及底层 ####
  • #鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行
  • #我与Java虚拟机的故事#连载16:打开Java世界大门的钥匙
  • (Git) gitignore基础使用
  • (LeetCode C++)盛最多水的容器
  • (二)PySpark3:SparkSQL编程
  • (分类)KNN算法- 参数调优
  • (力扣记录)235. 二叉搜索树的最近公共祖先
  • (转)3D模板阴影原理
  • (转)GCC在C语言中内嵌汇编 asm __volatile__
  • (转)利用PHP的debug_backtrace函数,实现PHP文件权限管理、动态加载 【反射】...
  • .NET CF命令行调试器MDbg入门(一)
  • .NET Core 网络数据采集 -- 使用AngleSharp做html解析
  • .NET MVC第三章、三种传值方式
  • .NET 某和OA办公系统全局绕过漏洞分析
  • .NET/C# 判断某个类是否是泛型类型或泛型接口的子类型
  • .net分布式压力测试工具(Beetle.DT)
  • .net利用SQLBulkCopy进行数据库之间的大批量数据传递
  • .net连接MySQL的方法
  • .NET中的Event与Delegates,从Publisher到Subscriber的衔接!
  • .project文件
  • .sh文件怎么运行_创建优化的Go镜像文件以及踩过的坑