LeetCode训练
生活随笔
收集整理的這篇文章主要介紹了
LeetCode训练
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
缺失的正整數
/*** 0~n-1中缺失的數字*/ public class MissingNum {public int missingNumber(int[] nums) {int i = 0;int j = nums.length - 1;while (i < j) {int m = i + ((j - i) >> 2);if (nums[m] == m) i = m + 1;else j = m - 1;}return nums[i] == i ? nums[i] + 1 : nums[i] - 1;}public static void main(String[] args) {int[] arr = {0, 1, 2, 3, 4, 5, 6, 7, 9};MissingNum missingNum = new MissingNum();System.out.println(missingNum.missingNumber(arr));} }數組中重復的數字
//leetcode 指 Offer 03. 數組中重復的數字public int findRepeatNumber(int[] nums) {HashMap<Integer, Integer> map = new HashMap<>();int res = nums[0];for (int num : nums) {if (map.get(num) == null) {map.put(num, 1);} else {res = num;break;}}return res;}盛水最多的容器
//leetcode-11 盛最多水的容器public int maxArea(int[] height) {int left = 0;int right = height.length - 1;int maxArea = 0;/* while (left < right) {int width = right - left;int deep = Math.min(height[left], height[right]);maxArea = Math.max(maxArea, width*deep);if (height[left] < height[right]) {left++;} else {right--;}}return maxArea;*/while (left < right) {maxArea = height[left] < height[right] ?Math.max(maxArea, (right - left) * height[left++]) :Math.max(maxArea, (right - left) * height[right--]);}return maxArea;}?
二分查找
//二分查找:初始值left=0,right=length-1,搜索閉區間[left,right]; while循環條件left<=right;public static int binarySearch(int[] nums, int target) {int left = 0;int right = nums.length - 1;while (left <= right) {int mid = left + (right - left) / 2;if (nums[mid] == target) {return mid;} else if (nums[mid] < target) {left = mid + 1;} else if (nums[mid] > target) {right = mid - 1;}}//return left;return nums[left] == target ? left : -1;}//左邊界查找:public static int leftBoundarySearch(int[] nums, int target) {int left = 0;int right = nums.length;while (left < right) {int mid = left + (right - left) / 2;if (nums[mid] == target) {right = mid;} else if (nums[mid] < target) {left = mid + 1;} else if (nums[mid] > target) {right = mid;}}//return left;if (nums.length == left) {return -1;}return nums[left] == target ? left : -1;}//右邊界查找:public static int rightBoundarySearch(int[] nums, int target) {int left = 0;int right = nums.length;while (left < right) {int mid = left + (right - left) / 2;if (nums[mid] == target) {left = mid + 1;} else if (nums[mid] < target) {left = mid + 1;} else if (nums[mid] > target) {right = mid;}}//return left - 1;if (left == 0) {return -1;}return nums[left - 1] == target ? left - 1 : -1;}合并兩個有序鏈表
//合并兩個有序鏈表public static ListNode mergeTwoLists1(ListNode l1, ListNode l2) {ListNode res = new ListNode(0);ListNode head = res;//指向頭結點的引用while (l1 != null && l2 != null) {if (l1.value < l2.value) {res.next = l1;l1 = l1.next;} else {res.next = l2;l2 = l2.next;}res = res.next;}if (l2 == null) {res.next = l1;} else {res.next = l2;}return head.next;}找到兩個單鏈表相交的起始結點(判斷鏈表是否相交)
//找到兩個單鏈表相交的起始結點(判斷鏈表是否相交)//走到盡頭見不到你,于是走過你來時的路,等到相遇時才發現,你也走過我來時的路//我先走我的路,再走你的路,你先走你的路,再走我的路,這樣咱倆走的路程就一樣了,速度一樣,那么肯定在咱倆兩條路的交叉口相遇,并一起走向終點。這思路,給跪了!public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {if (headA == null || headB == null) {return null;}ListNode A = headA;ListNode B = headB;// 直到兩人相遇while (A != B) {if (A != null) {// 如果A沒走到盡頭就一直走下去A = A.next;} else {// 直到盡頭也沒遇見B,所以去往B走過的路A = headB;}if (B != null) {B = B.next;} else {B = headA;}}// 返回A\B第一次相遇的地方return B;}求單鏈表是否有環(快、慢指針(龜兔賽跑思想,如果出現環形,那么必然相遇),快指針每次移動2步,慢指針每次移動一步
//求單鏈表是否有環(快、慢指針(龜兔賽跑思想,如果出現環形,那么必然相遇),快指針每次移動2步,慢指針每次移動一步public static boolean hasCycle(ListNode head) {if(head == null || head.next == null){return false;}ListNode slow = head;ListNode fast = head.next;while (slow != fast){if (fast == null||fast.next == null){return false;}slow = slow.next;fast =fast.next.next;}//哈希表方案:每次遍歷到一個節點時,判斷該節點此前是否被訪問過。 /* Set<ListNode> seen = new HashSet<>();while (head!=null){if (!seen.add(head)){return true;}head = head.next;}return false;*/return true;}?
總結
以上是生活随笔為你收集整理的LeetCode训练的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 天翼网关服务器无响应,教你使用天翼网关软
- 下一篇: eslint 无法格式化ts_vscod