剑指offer(七):双指针
题目一:删除链表的节点
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head.val == val) return head.next;
ListNode pre = head, cur = head.next;
while(cur != null && cur.val != val) {
pre = cur;
cur = cur.next;
}
if(cur != null) pre.next = cur.next;
return head;
}
}
题目二:调整数组顺序使奇数位于偶数前面
考虑定义双指针 i ,j分列数组左右两端,循环执行
- 指针 i从左向右寻找偶数;
- 指针 j从右向左寻找奇数;
- 将偶数 nums[i]和 奇数 nums[j]交换。
public static int[] exchange(int[] nums) {
int i = 0, j = nums.length-1,tmp;
while (i<j){
//一定要卡i<j这个条件
while (i<j&&nums[i]%2==1)i++;
while (i<j&&nums[j]%2==0)j--;
int tem = nums[i];
nums[i] = nums[j];
nums[j] = tem;
}
return nums;
}
题目三:链表中倒数第 k 个节点
方法一:遍历链表得到长度再进行遍历
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode cur = head;
ListNode get = head;
int i = 0;
while (cur!=null){
i++;
cur = cur.next;
}
int flag = i-k;
while (get!=null&&flag>0){
flag--;
get = get.next;
}
return get;
}
方法二:双指针方法
前指针先走k步,后指针从头节点开始,当前指针遍历到尾节点时说明已经找到
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode former = head, latter = head;
for(int i = 0; i < k; i++) {
//如果考虑越界要加上这个判定条件
if(former == null) return null;
former = former.next;
}
while(former != null) {
former = former.next;
latter = latter.next;
}
return latter;
}
}
题目四:合并两个排序的链表
//自己的笨方法
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1==null&&l2==null)return null;
ListNode frist = l1;
ListNode second = l2;
ListNode head = new ListNode(0);
ListNode cur = head;
while (frist!=null||second!=null){
if (frist!=null&&second!=null){
int tem = Math.min(frist.val,second.val);
cur.next = new ListNode(tem);
if (tem==frist.val)frist = frist.next;
else second=second.next;
}else if (second!=null){
cur.next = new ListNode(second.val);
second = second.next;
}else{
cur.next = new ListNode(frist.val);
frist = frist.next;
}
//千万记得在此处更新cur
cur = cur.next;
}
return head.next;
}
}
//lecode方法
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dum = new ListNode(0), cur = dum;
while(l1 != null && l2 != null) {
if(l1.val < l2.val) {
cur.next = l1;
l1 = l1.next;
}
else {
cur.next = l2;
l2 = l2.next;
}
cur = cur.next;
}
cur.next = l1 != null ? l1 : l2;
return dum.next;
}
}
自己操作遇到的问题:
- 创建了新的链表来操作,增加了空间复杂度;
- 忘记更新目前的cur;
- 不必将l1,l2赋值,空间将进一步减小
- 由于初始状态合并链表中无节点,因此循环第一轮时无法将节点添加到合并链表中。解决方案:初始化一个辅助节点 dum作为合并链表的伪头节点,将各节点添加至 dum之后。这样做的话不必进行复杂的安全性校验。
题目五: 两个链表的第一个公共节点
//这个题自己想的思路完全不对
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode A = headA,B = headB;
//判断两个节点是公共节点,则其在公共节点处一定有相同的内存地址,判断该内存地址即可
while (A!=B){
//三目运算符的运用代码又轻便不少
A = A!=null?A.next:headB;
B = B!=null?B.next:headA;
}
return A;
}
思想:
考虑构建两个节点指针 A
, B
分别指向两链表头节点 headA
, headB
,做如下操作:
- 指针 A 先遍历完链表 headA ,再开始遍历链表 headB ,当走到 node 时,共走步数为:a + (b - c)
- 指针 B 先遍历完链表 headB ,再开始遍历链表 headA ,当走到 node 时,共走步数为:b + (a - c)
如下式所示,此时指针 A
, B
重合,并有两种情况:
a+(b−c)=b+(a−c)**
若两链表有公共尾部 (即 c > 0) :指针 A , B 同时指向「第一个公共节点」node
若两链表无公共尾部 (即 c = 0) :指针 A , B 同时指向 null
题目六:和为s和的两个数字
class Solution {
public int[] twoSum(int[] nums, int target) {
int i = 0, j = nums.length - 1;
while(i < j) {
int s = nums[i] + nums[j];
//左指针向右移动
if(s < target) i++;
//右指针向左移动
else if(s > target) j--;
else return new int[] { nums[i], nums[j] };
}
return new int[0];
}
}
使用 双指针法 将空间复杂度降低至 O(1)
- 初始化: 双指针 i, j分别指向数组 nums的左右两端 (俗称对撞双指针)。
- 循环搜索: 当双指针相遇时跳出;
题目七:翻转单词顺序
方法一:分割+倒序遍历
class Solution {
public String reverseWords(String s) {
String[] strs = s.trim().split(" "); // 删除首尾空格,分割字符串
StringBuilder res = new StringBuilder();
for (int i = strs.length - 1; i >= 0; i--) { // 倒序遍历单词列表
if(strs[i].equals("")) continue; // 遇到空单词则跳过
res.append(strs[i] + " "); // 将单词拼接至 StringBuilder
}
return res.toString().trim(); // 转化为字符串,删除尾部空格,并返回
}
}
方法二:双指针
class Solution {
public String reverseWords(String s) {
s = s.trim(); // 删除首尾空格
int j = s.length() - 1, i = j;
StringBuilder res = new StringBuilder();
while (i >= 0) {
while (i >= 0 && s.charAt(i) != ' ') i--; // 搜索首个空格
res.append(s.substring(i + 1, j + 1) + " "); // 添加单词
while (i >= 0 && s.charAt(i) == ' ') i--; // 跳过单词间空格
j = i; // j 指向下个单词的尾字符
}
return res.toString().trim(); // 转化为字符串并返回
}
}