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

购买域名的网站站长工具seo优化系统

购买域名的网站,站长工具seo优化系统,武汉中天建设集团有限公司,做婚庆找什么网站搜索二维矩阵II 方法:从右上角开始搜索 我们可以从矩阵的右上角开始进行搜索。如果当前元素 matrix[i][j] 等于 target,我们直接返回 true。如果 matrix[i][j] 大于 target,说明 target 只能出现在左边的列,所以我们将列指针向左…

搜索二维矩阵II

方法:从右上角开始搜索

  • 我们可以从矩阵的右上角开始进行搜索。
  • 如果当前元素 matrix[i][j] 等于 target,我们直接返回 true
  • 如果 matrix[i][j] 大于 target,说明 target 只能出现在左边的列,所以我们将列指针向左移动。
  • 如果 matrix[i][j] 小于 target,说明 target 只能出现在下方的行,所以我们将行指针向下移动。
  • 我们重复这个过程,直到找到目标元素或者行列指针越界。
class Solution {public boolean searchMatrix(int[][] matrix, int target) {if(matrix == null || matrix.length == 0 || matrix[0].length == 0){return false;}int m = matrix.length;//行数int n = matrix[0].length;//列数int row = 0;//从第一行开始int col = n-1;//从最后一列开始while(row < m && col >= 0){if(matrix[row][col] == target){return true;}else if(matrix[row][col] > target){col--;//当前元素大于目标,左移}else{row++;//当前元素小于目标,下移}}return false;}
}

 相交链表

双指针法

  1. 两个指针分别指向两个链表的头节点
    我们设立两个指针 pApB,分别指向链表 A 和链表 B 的头节点。

  2. 同时移动两个指针

    • 每次移动一个指针,如果当前指针指向的节点为空,则将其指向另一个链表的头节点。
    • 这样做的目的是:让两个指针在遍历完自己的链表后,能够到达另一个链表的头节点,最终相遇的地方就是交点。
  3. 相遇
    如果两个指针相遇,则返回相遇的节点;如果两个指针同时指向 null,则说明链表没有交点。

这种方法的核心思想就是“同步走,互相切换”,确保两个指针走过相同的路程,因此可以在 O(m + n) 时间复杂度内解决问题。

设「第一个公共节点」为 node ,「链表 headA」的节点数量为 a ,「链表 headB」的节点数量为 b ,「两链表的公共尾部」的节点数量为 c ,则有:

头节点 headA 到 node 前,共有 a−c 个节点;
头节点 headB 到 node 前,共有 b−c 个节点;

考虑构建两个节点指针 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 。
因此返回 A 即可。

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/
public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {ListNode pA = headA;ListNode pB = headB;while(pA != pB){pA = (pA == null) ? headB : pA.next;pB = (pB == null) ? headA : pB.next;}return pA;}
}

反转链表

双指针: 

/*** 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) {ListNode cur = head,pre = null;while(cur != null){ListNode tmp = cur.next;cur.next = pre;pre = cur;cur = tmp;}return pre;}
}

 回文链表

思路:

  1. 找到链表的中间节点:使用快慢指针,慢指针每次走一步,快指针每次走两步,最终快指针会指向链表的尾部,慢指针则会指向链表的中间节点。
  2. 反转后半部分链表:将链表的后半部分进行反转,反转后的链表与前半部分进行比较。
  3. 比较前后部分:比较反转后的后半部分与前半部分的节点值,如果相同,则该链表是回文链表。
/*** 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) {if(head == null || head.next == null){return true;//链表为空或只有一个元素}ListNode slow = head,fast = head;//快指针走两步,慢指针走一步,直到快指针到达末尾while(fast != null && fast.next != null){slow = slow.next;fast = fast.next.next;}//反转链表的后半部分ListNode secondHalf = reverse(slow);ListNode firstHalf = head;//比较前后部分的节点值while(secondHalf != null){if(firstHalf.val != secondHalf.val){return false;}firstHalf = firstHalf.next;secondHalf = secondHalf.next;}return true;}public ListNode reverse(ListNode head){ListNode prev = null,curr = head;while(curr != null){ListNode temp = curr.next;curr.next = prev;prev = curr;curr = temp;}return prev;}
}

环形链表

  • 初始化指针:使用两个指针,slowfastslow 每次走一步,fast 每次走两步。
  • 判断是否相遇:如果存在环,slowfast 会在环内相遇。如果没有环,fast 会指向 null,即链表的末尾。
  • 结束条件
    • 如果 slowfast 相遇,则说明链表有环,返回 true
    • 如果 fast 到达 null,则说明链表没有环,返回 false
/*** Definition for singly-linked list.* class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/
public class Solution {public boolean hasCycle(ListNode head) {if(head == null || head.next == null){return false;}ListNode slow = head,fast = head;while(fast != null && fast.next != null){slow = slow.next;fast = fast.next.next;if(slow == fast){return true;}}return false;}
}

环形链表ll

  • 使用快慢指针检查链表是否有环。
  • 如果有环,重新定位慢指针到头节点,同时让慢指针和快指针都以相同的速度(每次一步)向前走,直到它们相遇。
  • 相遇的节点就是入环的第一个节点。
/*** Definition for singly-linked list.* class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/
public class Solution {public ListNode detectCycle(ListNode head) {if(head == null || head.next == null){return null;}ListNode slow = head,fast = head;while(fast != null && fast.next != null){slow = slow.next;fast = fast.next.next;if(slow == fast){//找到环的起点ListNode pointer = head;while(pointer != slow){pointer = pointer.next;//pointer从头节点开始走slow = slow.next;//slow从相遇点开始走}return pointer;}}return null;}
}
http://www.khdw.cn/news/20190.html

相关文章:

  • 将网站收藏至ie收藏夹怎么做东莞营销型网站建设
  • 网站内容更新用什么热狗网站排名优化外包
  • 企业能建站吗制作网站需要什么技术
  • 配音秀做素材网站网络营销一般月薪多少
  • 做单页网站价格seo是哪个英文的简写
  • 网站建设怎么问问题南宁网站建设网站推广
  • 做家装的网站有什么区别seo中心
  • 哪个网站做外贸零售比较好呢网站播放视频速度优化
  • 官方网站建设公驻马店网站seo
  • 西宁做网站是什么宁德市区哪里好玩
  • 平面设计包括哪些方面的设计长清区seo网络优化软件
  • 购物网站最近浏览怎么做自动点击器app
  • 烟台 网站建设公关公司排名
  • 龙华营销型网站建设公司常用的关键词优化策略有哪些
  • 有什么兼职做设计的网站好产品营销推广
  • 久久做bilibili官网网站南宁网站建设优化服务
  • 海外直播seo优化培训学校
  • 乐陵网站制作搜索引擎优化结果
  • 盐城做网站网络公司电话?企业营销策略
  • 谷歌浏览器对做网站有什么好处google谷歌搜索主页
  • 做模特的网站seo咨询岳阳
  • 百度网站空间百度一下百度主页官网
  • 做公务员试题比较好的网站好搜搜索引擎
  • 电商网站设计实例google搜索首页
  • 郑州高端网站建设多少钱博客优化网站seo怎么写
  • 武陟外贸英文网站建设搜索软件排行榜前十名
  • 网站开发 入门教程网络营销的推广手段
  • 清远公司网站建设企业邮箱注册
  • 做外贸的有哪些网站有哪些公司网站建设推广
  • 自己做网站 微信给公司做网站的公司