力扣打卡——搜索二维矩阵、相交链表
240. 搜索二维矩阵 II - 力扣LeetCode思路直接从右边开始判断大于往下走小于就往左走class Solution { public boolean searchMatrix(int[][] matrix, int target) { int nmatrix.length; int mmatrix[0].length; //从右上角开始 int i0; int jm-1; while(i n j0){ if(matrix[i][j]target) return true; //往下走 if(target matrix[i][j]) { i; //往左走 }else { j--; } } return false; } }160. 相交链表 - 力扣LeetCode思路标准的长度差法完全正确分别计算两个链表的长度让长链表的指针先走长度差步然后两个指针一起走相遇点就是交点/** * 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) { if(headAnull || headB null){ return null; } ListNode nodeAheadA; ListNode nodeBheadB; int lenA0,lenB0; while(nodeA!null){ nodeAnodeA.next; lenA; } while(nodeB!null){ nodeBnodeB.next; lenB; } //始终认为 ab if(lenBlenA){ ListNode tmpheadA; headAheadB; headBtmp; int lenClenA; lenAlenB; lenBlenC; } int lenClenA-lenB; while(lenC0){ headAheadA.next; lenC--; } while(headA!null headB !null){ if(headAheadB){ return headA; } headAheadA.next; headBheadB.next; } return null } }
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2438546.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!