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

求职Leetcode题目(12)

1.只出现一次的数字

异或运算满足交换律 a⊕b=b⊕a ,即以上运算结果与 nums 的元素顺序无关。代码如下:

class Solution {public int singleNumber(int[] nums) {int ans =0;for(int num:nums){ans^=num;}return ans;}
}

 2.只出现一次的数字II

这是今天滴滴的原题,要求时间复杂度为O(1)和空间复杂度为O(n) 

方法一:哈希表

思路与算法

我们可以使用哈希映射统计数组中每个元素的出现次数。对于哈希映射中的每个键值对,键表示一个元素,值表示其出现的次数。

在统计完成后,我们遍历哈希映射即可找出只出现一次的元素。

class Solution {public int singleNumber(int[] nums) {Map<Integer,Integer> freg =new HashMap<Integer,Integer>();for(int num:nums){freg.put(num,freg.getOrDefault(num,0)+1);}int ans =0;for(Map.Entry<Integer,Integer> entry:freg.entrySet()){int num =entry.getKey(),occ=entry.getValue();if(occ==1){ans =num;break;}}return ans;}
}

这个方法的效率显然不高,我们换一个思路试试看,而且时间复杂度不为O(1)

解法二:

如下图所示,考虑数字的二进制形式,对于出现三次的数字,各 二进制位 出现的次数都是 3 的倍数。
因此,统计所有数字的各二进制位中 1 的出现次数,并对 3 求余,结果则为只出现一次的数字。

3.随机链表复制

解题思路:

// Definition for a Node.
class Node {int val;Node next;public Node(int val) {this.val = val;this.next = null;}
}

 本题链表的节点定义如下:

// Definition for a Node.
class Node {int val;Node next, random;public Node(int val) {this.val = val;this.next = null;this.random = null;}
}

给定链表的头节点 head ,复制普通链表很简单,只需遍历链表,每轮建立新节点 + 构建前驱节点 pre 和当前节点 node 的引用指向即可。

本题链表的节点新增了 random 指针,指向链表中的 任意节点 或者 null 。这个 random 指针意味着在复制过程中,除了构建前驱节点和当前节点的引用指向 pre.next ,还要构建前驱节点和其随机节点的引用指向 pre.random 。

本题难点: 在复制链表的过程中构建新链表各节点的 random 引用指向。

class Solution {public Node copyRandomList(Node head) {Node cur = head;Node dum = new Node(0), pre = dum;while(cur != null) {Node node = new Node(cur.val); // 复制节点 curpre.next = node;               // 新链表的 前驱节点 -> 当前节点// pre.random = "???";         // 新链表的 「 前驱节点 -> 当前节点 」 无法确定cur = cur.next;                // 遍历下一节点pre = node;                    // 保存当前新节点}return dum.next;}
}

 可以对上面经典的复制链表的办法来解决这个问题

class Solution {public Node copyRandomList(Node head) {if(head == null) return null;Node cur = head;Map<Node, Node> map = new HashMap<>();// 3. 复制各节点,并建立 “原节点 -> 新节点” 的 Map 映射while(cur != null) {map.put(cur, new Node(cur.val));cur = cur.next;}cur = head;// 4. 构建新链表的 next 和 random 指向while(cur != null) {map.get(cur).next = map.get(cur.next);map.get(cur).random = map.get(cur.random);cur = cur.next;}// 5. 返回新链表的头节点return map.get(head);}
}

4.重排链表

 先了解怎么反转链表

示例:

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

思路分析:

对于题目给出的链表,简化如下:

class Solution {public void reorderList(ListNode head) {ListNode prev=null;ListNode cur =head;while(cur!=null){ListNode nextNode =cur.next;cur.next=prev;prev=cur;cur=nextNode;}return prev;}
}

还有一个关键步骤,求出链表的中间节点 :

题目要求的是如果有两个中间节点,则返回第二个中间节点。因此,对于该题目而言,快指针fast向前移动的条件是:fast!=null && fast.next != null。

 

 下面是完整解法:

完整代码 :

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode() {}*     ListNode(int val) { this.val = val; }*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }* }*/
public void reorderList(ListNode head) {if (head == null) {return;}// 获得中间节点ListNode mid = findMid(head);// 中间节点之后的部分进行反转ListNode head2 = mid.next;mid.next = null;head2 = reverseList(head2);// 合并ListNode head1 = head;mergeList(head1, head2);
}// LeetCode 876
private ListNode findMid(ListNode head){ListNode slow = head;ListNode fast = head;while (fast.next != null && fast.next.next != null) {slow = slow.next;fast= fast.next.next;}return slow;
}// LeetCode 206
private ListNode reverseList(ListNode head){ListNode prev = null;ListNode cur = head;while (cur != null) {ListNode nextNode = cur.next;cur.next = prev;prev =cur;cur = nextNode;}return prev;
}private void mergeList(ListNode head1, ListNode head2) {ListNode next1 = null;ListNode next2 = null;while (head1 != null && head2 != null) {next1 = head1.next;next2 = head2.next;head1.next = head2;head1 = next1;head2.next = head1;head2 = next2;}
}

相关文章:

  • Spring Boot技术:构建高效网上购物平台
  • 《黑神话:悟空》在全球爆火的原因是什么?
  • Ubuntu开机进入紧急模式处理
  • windows10 docker 推送本地镜像
  • SQL进阶技巧:如何获取状态一致的分组? | 最大、最小值法
  • JVM(HotSpot):字符串常量池(StringTable)
  • 在Robot Framework中Run Keyword If的用法
  • 汽车发动机控制存储芯片MR2A08A
  • Trilium Notes笔记本地化部署与简单使用指南打造个人知识库
  • 云计算Openstack Glance
  • 网络战时代的端点安全演变
  • Linux 下 C++ 操作串口并彻底释放控制权的总结
  • c#中字符串处理的技巧集合
  • socket网络编程
  • MySQL高阶2004-职员招聘人数
  • [NodeJS] 关于Buffer
  • 《Java8实战》-第四章读书笔记(引入流Stream)
  • 【Amaple教程】5. 插件
  • 【JavaScript】通过闭包创建具有私有属性的实例对象
  • C++类的相互关联
  • Elasticsearch 参考指南(升级前重新索引)
  • Essential Studio for ASP.NET Web Forms 2017 v2,新增自定义树形网格工具栏
  • Java应用性能调优
  • mockjs让前端开发独立于后端
  • Python中eval与exec的使用及区别
  • SegmentFault 技术周刊 Vol.27 - Git 学习宝典:程序员走江湖必备
  • Twitter赢在开放,三年创造奇迹
  • 分类模型——Logistics Regression
  • 构建二叉树进行数值数组的去重及优化
  • 关于springcloud Gateway中的限流
  • 来,膜拜下android roadmap,强大的执行力
  • 理解 C# 泛型接口中的协变与逆变(抗变)
  • 猫头鹰的深夜翻译:JDK9 NotNullOrElse方法
  • 前端之Sass/Scss实战笔记
  • 少走弯路,给Java 1~5 年程序员的建议
  • media数据库操作,可以进行增删改查,实现回收站,隐私照片功能 SharedPreferences存储地址:
  • CMake 入门1/5:基于阿里云 ECS搭建体验环境
  • ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
  • ​人工智能之父图灵诞辰纪念日,一起来看最受读者欢迎的AI技术好书
  • #stm32整理(一)flash读写
  • #window11设置系统变量#
  • $forceUpdate()函数
  • (delphi11最新学习资料) Object Pascal 学习笔记---第13章第6节 (嵌套的Finally代码块)
  • (附表设计)不是我吹!超级全面的权限系统设计方案面世了
  • (附源码)springboot猪场管理系统 毕业设计 160901
  • (全注解开发)学习Spring-MVC的第三天
  • (十)c52学习之旅-定时器实验
  • (转) ns2/nam与nam实现相关的文件
  • (转)linux自定义开机启动服务和chkconfig使用方法
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .Net Core 微服务之Consul(二)-集群搭建
  • .net core开源商城系统源码,支持可视化布局小程序
  • .NET 某和OA办公系统全局绕过漏洞分析
  • .NET/C# 中你可以在代码中写多个 Main 函数,然后按需要随时切换
  • .NET的数据绑定