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

seo网站推广怎样深圳网站设计公司行业

seo网站推广怎样,深圳网站设计公司行业,360浏览器怎么创建网页,做公司网站软件链表双指针经典习题 链表的分解删除排序链表中的重复元素2(重复元素彻底删除)方法一:分解链表方式二:快慢指针递归解法 链表的合并丑数2有序矩阵中第k小的元素查找和最小的k对数字两数相加两数相加2 回文单链表回文链表 迭代和递归…

链表双指针经典习题

  • 链表的分解
    • 删除排序链表中的重复元素2(重复元素彻底删除)
      • 方法一:分解链表
      • 方式二:快慢指针
      • 递归解法
  • 链表的合并
    • 丑数2
    • 有序矩阵中第k小的元素
    • 查找和最小的k对数字
    • 两数相加
    • 两数相加2
  • 回文单链表
    • 回文链表
  • 迭代和递归翻转单链表
    • 反转链表
      • 方一:迭代
      • 方二: 递归

链表的分解

删除排序链表中的重复元素2(重复元素彻底删除)

链接
在这里插入图片描述

方法一:分解链表

/*** 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; }* }*/
class Solution {//1.分解链表 方式public ListNode deleteDuplicates(ListNode head) {ListNode uniqueDummy = new ListNode(101);ListNode repeatDummy = new ListNode(101);ListNode res=uniqueDummy;ListNode cur=head;//遍历原链表while(cur!=null){//cur节点是重复节点if(cur.next!=null && cur.val==cur.next.val || cur.val==repeatDummy.val){repeatDummy.next=cur;repeatDummy=repeatDummy.next;}else{//cur不是重复节点uniqueDummy.next=cur;uniqueDummy=uniqueDummy.next;}cur=cur.next;}uniqueDummy.next=null;return res.next;}
}

方式二:快慢指针


class Solution {//快慢指针方法,快指针探路 慢指针更新答案链表public ListNode deleteDuplicates(ListNode head) {ListNode dummy = new ListNode(101);dummy.next=head;ListNode slow=dummy;ListNode preFast=dummy;ListNode fast=head;while(fast!=null){if(fast.next!=null && fast.val!=preFast.val && fast.val!=fast.next.val){//fast节点不重复slow.next=fast;slow=slow.next;}if(fast.next==null && fast.val!=preFast.val){slow.next=fast;slow=slow.next;}fast=fast.next;preFast=preFast.next;}slow.next=null;return dummy.next;}
}

递归解法

/*** 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; }* }*/
class Solution {//递归解法//定义:给你一条头结点是head的链表 删除重复元素后返回头结点public ListNode deleteDuplicates(ListNode head) {//如果 只有一个节点 或者没有节点 就不用删除if(head==null || head.next==null){return head;}//如果head节点与后面的节点不同if(head.val!=head.next.val){head.next= deleteDuplicates(head.next);return head;}//如果head节点与后面的节点相同while(head.next!=null && head.val==head.next.val){head=head.next;}return deleteDuplicates(head.next);}
}

链表的合并

丑数2

链接在这里插入图片描述

class Solution {public int nthUglyNumber(int n) {int p2=1,p3=1,p5=1;//指向ugly中的第一个丑数int val2=1,val3=1,val5=1;//表示三个链表的当前节点int p=1;//指向ugly的索引,便于更新uglyint[]ugly=new int[n+1];//ugly[i]:第i个丑数while(p<=n){//找出三个链表中当前节点的最小值int minVal=Math.min(Math.min(val2,val3),val5);ugly[p++]=minVal;//更新三个链表的当前节点if(minVal==val2){val2=2*ugly[p2++];}if(minVal==val3){val3=3*ugly[p3++];}if(minVal==val5){val5=5*ugly[p5++];}}return ugly[n];}
}

有序矩阵中第k小的元素

链接在这里插入图片描述

class Solution {//用优先级队列辅助//思路:合并多条链表public int kthSmallest(int[][] matrix, int k) {//队列里存{matrix[i][j],i,j} 以便找到后面的元素PriorityQueue<int[]> pq=new PriorityQueue<int[]>(new Comparator<int[]>() {@Overridepublic int compare(int[] o1, int[] o2) {return o1[0]-o2[0];}});//maxtrix每一行相当于一条链表//先把各链表的头结点入队列for(int i=0;i<matrix.length;i++){pq.add(new int[]{matrix[i][0],i,0});}int res=0;while(!pq.isEmpty()&&k>0){int[] pollEle=pq.poll();res=pollEle[0];int i=pollEle[1],j=pollEle[2];//把(i,j+1)的元素入队列if(j+1<matrix[i].length){pq.add(new int[]{matrix[i][j+1],i,j+1});}k--;}return res;}
}

查找和最小的k对数字

链接在这里插入图片描述

//优先级队列+合并链表
class Solution {public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {//队列里存{sum,i,j},sum是nums[i]+nums[j]PriorityQueue<int[]> pq=new PriorityQueue<int[]>(new Comparator<int[]>() {@Overridepublic int compare(int[] o1, int[] o2) {return o1[0]-o2[0];}});int sz1=nums1.length;int sz2=nums2.length;//有sz1条链表,先把各链表的头结点入队列for(int i=0;i<sz1;i++){pq.add(new int[]{nums1[i]+nums2[0],i,0});}List<List<Integer>> res=new LinkedList<>();while(!pq.isEmpty()&&k>0){int[]pollEle=pq.poll();int i=pollEle[1];int j=pollEle[2];LinkedList<Integer> ele=new LinkedList();ele.add(nums1[i]);ele.add(nums2[j]);res.add(ele);//把[i,j+1]入队列if(j+1<nums2.length){pq.add(new int[]{nums1[i]+nums2[j+1],i,j+1});}k--;}return res;}
}

两数相加

链接
在这里插入图片描述

/*** 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; }* }*/
class Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListNode p1=l1;ListNode p2=l2;ListNode dummy=new ListNode(-1);ListNode p=dummy;int carry=0,add=0;while(p1!=null&&p2!=null){add=p1.val+p2.val+carry;carry=add/10;//进位的add%=10;ListNode newNode=new ListNode(add);p.next=newNode;p=p.next;p1=p1.next;p2=p2.next;}while(p1!=null){add=p1.val+carry;carry=add/10;add%=10;ListNode newNode=new ListNode(add);p.next=newNode;p=p.next;p1=p1.next;}while(p2!=null){add=p2.val+carry;carry=add/10;add%=10;ListNode newNode=new ListNode(add);p.next=newNode;p=p.next;p2=p2.next;}if(carry!=0){//还有进位ListNode newNode=new ListNode(carry);p.next=newNode;p=p.next;}p.next=null;return dummy.next;}
}

两数相加2

链接在这里插入图片描述


class Solution {public ListNode addTwoNumbers(ListNode l1, ListNode l2) {Stack<Integer> stack=new Stack();//翻转l1链表while(l1!=null){stack.add(l1.val);l1=l1.next;}ListNode head1= new ListNode(stack.pop());ListNode p1=head1;while(!stack.isEmpty()){int cur=stack.pop();ListNode newNode=new ListNode(cur);p1.next=newNode;p1=p1.next;}p1.next=null;//翻转l2链表while(l2!=null){stack.add(l2.val);l2=l2.next;}ListNode head2= new ListNode(stack.pop());ListNode p2=head2;while(!stack.isEmpty()){int cur=stack.pop();ListNode newNode=new ListNode(cur);p2.next=newNode;p2=p2.next;}p2.next=null;//开始计算 跟两数相加1一样的p1=head1;p2=head2;int add=0,carry=0;while(p1!=null && p2!=null){add=p1.val+p2.val+carry;carry=add/10;add%=10;stack.add(add);p1=p1.next;p2=p2.next;}while(p1!=null){add=p1.val+carry;carry=add/10;add%=10;stack.add(add);p1=p1.next;}while(p2!=null){add=p2.val+carry;carry=add/10;add%=10;stack.add(add);p2=p2.next;}if(carry!=0){stack.add(carry);}ListNode dummy=new ListNode(-1);ListNode p=dummy;while(!stack.isEmpty()){int cur=stack.pop();ListNode newNode=new ListNode(cur);p.next=newNode;p=p.next;}p.next=null;return dummy.next;}
}

回文单链表

回文链表

链接
在这里插入图片描述
方法一:用栈来辅助

/*** 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; }* }*/
class Solution {public boolean isPalindrome(ListNode head) {Stack<Integer> stack=new Stack();ListNode cur=head;while(cur!=null){stack.add(cur.val);cur=cur.next;}cur=head;while(cur!=null){int val=cur.val;int stackVal=stack.pop();if(val!=stackVal){return false;}cur=cur.next;}return true;}
}

方法二:快慢指针

/*** 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; }* }*/
class Solution {//用快慢指针public boolean isPalindrome(ListNode head) {ListNode fast=head,slow=head;while(fast!=null && fast.next!=null){fast=fast.next.next;slow=slow.next;}if(fast!=null){//奇数个 slow需要+1slow=slow.next;}//从①head->slow-1 ②fast->slowListNode head2= reverse(slow); while(head2!=null){if(head.val!=head2.val){return false;}head=head.next;head2=head2.next;}return true;}//返回翻转后的链表ListNode reverse(ListNode head){if(head==null){return null;}if(head.next==null){//一个节点return head;}ListNode pre=head;ListNode cur=head.next;pre.next=null;while(cur!=null){ListNode next=cur.next;cur.next=pre;pre=cur;cur=next;}return pre;}
}

迭代和递归翻转单链表

反转链表

在这里插入图片描述

方一:迭代

/*** 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; }* }*/
class Solution {//迭代public ListNode reverseList(ListNode head) {if(head==null){return null;}ListNode pre=head,cur=head.next;pre.next=null;while(cur!=null){ListNode next=cur.next;cur.next=pre;pre=cur;cur=next;}return pre;}
}

方二: 递归

/*** 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; }* }*/
class Solution {//递归翻转//定义:翻转以head为头结点的链表后并返回头结点public ListNode reverseList(ListNode head) {//只有一个节点或者是没有节点 不用翻转直接返回if(head==null||head.next==null){return head;}//翻转head.next链表 然后head接在他的后面ListNode realHead=reverseList(head.next);ListNode tail=head.next;tail.next=head;head.next=null;return realHead;}
}

文章转载自:

http://Gxqcp6LK.crkmm.cn
http://Ng4WMIvu.crkmm.cn
http://OMPmXRXA.crkmm.cn
http://vuYbawv8.crkmm.cn
http://Wdcz4Lz6.crkmm.cn
http://CvxAEImb.crkmm.cn
http://Dy3IQb0j.crkmm.cn
http://xGToyY0O.crkmm.cn
http://TWpkk9ik.crkmm.cn
http://rBoPt6Xp.crkmm.cn
http://1MN2W87x.crkmm.cn
http://xA6ktVXW.crkmm.cn
http://Qgvn3VBs.crkmm.cn
http://QhbDPXzf.crkmm.cn
http://LBUwcw1b.crkmm.cn
http://n1pL1ec2.crkmm.cn
http://vbzKbjSe.crkmm.cn
http://pqseW3Yr.crkmm.cn
http://1cGOhAuE.crkmm.cn
http://ZVWv62q1.crkmm.cn
http://AvKazl5p.crkmm.cn
http://cTwmbNW8.crkmm.cn
http://oo0nPj5x.crkmm.cn
http://0dhKT3Yx.crkmm.cn
http://Fbgtqo22.crkmm.cn
http://vpi4MYJv.crkmm.cn
http://ZpMg7uyM.crkmm.cn
http://KXzu8W7q.crkmm.cn
http://lSUr2fTB.crkmm.cn
http://n53hgm5C.crkmm.cn
http://www.dtcms.com/wzjs/620435.html

相关文章:

  • 哪个网站建网页比较好备案查询工信部
  • 五寨网站建设安徽省建设工程信息网网
  • 怎么做视频解析网站吗河南省村镇建设处网站
  • 网页界面设计一般使用的分辨率东莞百度seo找哪里
  • discuz网站搬家教程网站建设的特征
  • 贵阳讯玛网站建设wordpress 图片墙
  • win7 网站系统怎么做友情链接购买
  • 刷单类网站开发wordpress zhong
  • 天津市津南区教育网站建设招标揭阳企业网页制作公司
  • 想自己做网站苏州专业做优化公司
  • 网站建设 腾云网络推广的方法
  • 网站横幅怎做西青网站文化建设
  • 河南网站建设37518企业网站改版方案
  • 网站制作哪些类型推广公司如何找客户
  • 夫妻性做受视频什么网站一站式服务平台登录
  • 网站建设 焦作照片展示网站模板
  • 软件开发流程管理系统长春seo公司长春网站设计
  • 怎么通过数据库做网站的登录免费的网站搭建
  • 苏州设置网站建设太原首页推广
  • 河南企业网站排名优化价格长春市建设技工学校网站
  • 网站建设管理制度九不准医疗网站项目策划
  • 网站建设去哪网页设计师证书报名官网
  • 机械厂网站模板英文网站做百度权重有意义吗
  • 微信crm系统seo是什么意思为什么要做seo
  • 西安网站建设哪家比较好发稿计划
  • 苏州城乡和住房建设局网站首页建立网站多少钱一年
  • openwrt做网站网站美工效果图怎么做
  • 移动网站适配做自己的网站的作用
  • 如何在搜索中找到自己做的网站广州网站建设找新际
  • 泰州网站制作网站建设应该计入什么费用