《劍指Offer》筆記題解思路技巧優化_Part_6

《劍指Offer》筆記&題解&思路&技巧&優化_Part_6

  • 😍😍😍 相知
  • 🙌🙌🙌 相識
  • 😢😢😢 開始刷題
    • 🟡1.LCR 168. 丑數—— 丑數
    • 🟢2. LCR 169. 招式拆解 II——第一個只出現一次的字符
    • 🔴3. LCR 170. 交易逆序對的總數——數組中的逆序對
    • 🟢4. LCR 171. 訓練計劃 V——兩個鏈表的第一個公共節點
    • 🟢5. LCR 172. 統計目標成績的出現次數——在排序數組中查找數字
    • 🟢6. LCR 173. 點名——0~ n-1中缺失的數字
    • 🟢7. LCR 174. 尋找二叉搜索樹中的目標節點——二叉搜索樹的第k大節點
    • 🟢8. LCR 175. 計算二叉樹的深度——二叉樹的深度
    • 🟢9. LCR 176. 判斷是否為平衡二叉樹——平衡二叉樹
    • 🟡10. LCR 177. 撞色搭配——數組中數字出現的次數I
    • 🟡11. LCR 178. 訓練計劃 VI——數組中數字出現的次數II

在這里插入圖片描述

😍😍😍 相知

刷題不要一上來就直接干,先看題,明白題說的什么意思,然后想一下用什么現成的算法和數據結構可以快速解決,如果還是無從下手,建議先去看視頻,不要直接翻評論或官方代碼實現,看完視頻,自己在idea中模擬敲幾遍代碼,如果跑通了,先別急著上leetcode黏貼,而是再回顧一下要點,然后確定自己完全懂了后,在leetcode中手敲,注意是手敲下來!!! 目前我就是采用的這種方法,雖然慢,但是可以維持一周忘不掉它,如果要想長期不忘,只能隔段時間就review一下了,就算是大牛,知道方法,長時間不碰,也不可能保證一次到位把代碼敲完一遍過!!!

這是我上一篇博客的,也希望大家多多關注!

  1. 《劍指Offer》筆記&題解&思路&技巧&優化 Java版本——新版leetcode_Part_1
  2. 《劍指Offer》筆記&題解&思路&技巧&優化 Java版本——新版leetcode_Part_2
  3. 《劍指Offer》筆記&題解&思路&技巧&優化 Java版本——新版leetcode_Part_3
  4. 《劍指Offer》筆記&題解&思路&技巧&優化 Java版本——新版leetcode_Part_4
  5. 《劍指Offer》筆記&題解&思路&技巧&優化 Java版本——新版leetcode_Part_5

🙌🙌🙌 相識

根據題型可將其分為這樣幾種類型:

  1. 結構概念類(數組,鏈表,棧,堆,隊列,樹)
  2. 搜索遍歷類(深度優先搜索,廣度優先搜索,二分遍歷)
  3. 雙指針定位類(快慢指針,指針碰撞,滑動窗口)
  4. 排序類(快速排序,歸并排序)
  5. 數學推理類(動態規劃,數學)

😢😢😢 開始刷題

🟡1.LCR 168. 丑數—— 丑數

題目跳轉:https://leetcode.cn/problems/chou-shu-lcof/description/

class Solution {public int nthUglyNumber(int n) {if (n <= 0)return -1;int[] dp = new int[n];dp[0] = 1;int id2 = 0, id3 = 0, id5 = 0;for (int i = 1; i < n; i++) {dp[i] = Math.min(dp[id2] * 2, Math.min(dp[id3] *3, dp[id5] * 5));// 這里不用else if的原因是有可能id2(3) * 2 == id3(2) * 3// 這種情況兩個指針都要后移if (dp[id2] * 2 == dp[i])id2 += 1;if (dp[id3] * 3 == dp[i])id3 += 1;if (dp[id5] * 5 == dp[i])id5 += 1; }return dp[n - 1];}
}

🟢2. LCR 169. 招式拆解 II——第一個只出現一次的字符

題目跳轉:https://leetcode.cn/problems/di-yi-ge-zhi-chu-xian-yi-ci-de-zi-fu-lcof/description/

class Solution {public char dismantlingAction(String arr) {if(arr.length()==0) return ' ';if(arr.length()==1) return arr.charAt(0);int [] array =new int[26];for(int i = 0;i<arr.length();i++){array[arr.charAt(i)-'a']++;}for(int i = 0;i<arr.length();i++){if(array[arr.charAt(i)-'a']==1)return arr.charAt(i);}return ' ';}
}

🔴3. LCR 170. 交易逆序對的總數——數組中的逆序對

題目跳轉:https://leetcode.cn/problems/shu-zu-zhong-de-ni-xu-dui-lcof/description/

冒泡排序

每檢查一次交換一次 就可以產生一次逆序對

歸并排序

在這里插入圖片描述

class Solution {public int reversePairs(int[] nums) {if(nums == null || nums.length <= 1){return 0;}return mergeSort(nums, 0, nums.length - 1);}int mergeSort(int[] nums, int left, int right){if(left >= right){return 0;}int mid = (right - left) / 2 + left;int x1 = mergeSort(nums, left, mid);int x2 = mergeSort(nums, mid + 1, right);int x3 = merge(nums, left, mid, mid+1, right);return x1 + x2 + x3;}int merge(int[] nums, int l1, int r1, int l2, int r2){int[] temp = new int[r2 - l1 + 1];int count = 0;int i = l1, j = l2, k = 0;while(i <= r1 && j <= r2){if(nums[i] > nums[j]){count = count + (l2 - i);temp[k++] = nums[j++];}else{temp[k++] = nums[i++];}}while(i <= r1) temp[k++] = nums[i++];while(j <= r2) temp[k++] = nums[j++];// 把臨時數組復制回原數組k = 0;for(i = l1; i <= r2; i++){nums[i] = temp[k++];}return count;}
}

🟢4. LCR 171. 訓練計劃 V——兩個鏈表的第一個公共節點

題目跳轉:https://leetcode.cn/problems/liang-ge-lian-biao-de-di-yi-ge-gong-gong-jie-dian-lcof/description/

/*** Definition for singly-linked list.* public class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/
class Solution {ListNode getIntersectionNode(ListNode headA, ListNode headB) {if(headA==null||headB==null)return null;ListNode tempA = headA;ListNode tempB = headB;boolean a = false;boolean b = false;while(tempA!=tempB){if(tempA.next==null){tempA = headB;if(a)return null;a = true;}else{tempA = tempA.next;}if(tempB.next==null){tempB = headA;if(b)return null;b = true;}else{tempB = tempB.next;}}return tempA;}
}

原來不會死循環

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {ListNode h1 = headA, h2 = headB;while (h1 != h2) {h1 = h1 == null ? headB : h1.next;h2 = h2 == null ? headA : h2.next;}return h1;  }

🟢5. LCR 172. 統計目標成績的出現次數——在排序數組中查找數字

題目跳轉:https://leetcode.cn/problems/zai-pai-xu-shu-zu-zhong-cha-zhao-shu-zi-lcof/description/

二分查找

class Solution {public int countTarget(int[] scores, int target) {int left = 0;int right = scores.length-1;int mid = 0;int conut = 0;while(left<right){mid = (right-left)/2+left;if(target <= scores[mid]) right = mid;else left = mid+1;}while(left<scores.length&&scores[left++]==target)conut++;return conut;}
}

🟢6. LCR 173. 點名——0~ n-1中缺失的數字

題目跳轉:https://leetcode.cn/problems/que-shi-de-shu-zi-lcof/description/

位運算

class Solution {public int takeAttendance(int[] records) {if(records[records.length-1]==records.length-1) return records.length;int result = 0;for(int i = 0;i<records.length;i++){result ^=records[i];result ^=i;}result^=records.length;return result;}
}

二分查找

class Solution {public int takeAttendance(int[] records) {if(records[records.length-1]==records.length-1) return records.length;int left = 0;int right =records.length-1;while(left<right){int mid = (right-left)/2 +left;if(records[mid]==mid){left = mid+1;}else{right = mid;}}return left;}
}

🟢7. LCR 174. 尋找二叉搜索樹中的目標節點——二叉搜索樹的第k大節點

題目跳轉:https://leetcode.cn/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof/description/
作為一個普通人,我來分析下這題。

  1. 假設,你花了點時間,練習了二叉樹的三種遍歷方式: a. 前序遍歷 b. 中序遍歷 c. 后續遍歷
  2. 你也學習了二叉搜索樹,深入研究了二叉樹搜索樹的特性,并且深刻知道二叉搜索樹的一個特性:通過中序遍歷所得到的序列,就是有序的。

好,有了以上兩點知識,我認為你必須能想到(如果想不到,以上兩點知識肯定沒有學扎實):中序遍歷二叉搜索樹,遍歷的同時,把遍歷到的節點存到一個可變數組里(Java的話,可以用ArrayList)。 思路轉化為代碼,如下:

class Solution {public int findTargetNode(TreeNode root, int cnt) {if(root==null)return -1;if(root.left==null&root.right==null)return root.val;ArrayList<Integer> list = new ArrayList<>();dfs(root,list);return list.get(list.size()-cnt);}public void dfs(TreeNode root,ArrayList<Integer> list){if(root==null)return;if(root.left==null&root.right==null){list.add(root.val);return ;}if(root.left!=null)dfs(root.left,list);list.add(root.val);if(root.right!=null)dfs(root.right,list);}
}
class Solution {public int findTargetNode(TreeNode root, int cnt) {if(root==null)return -1;if(root.left==null&root.right==null)return root.val;ArrayList<Integer> list = new ArrayList<>();Stack<TreeNode> stack = new Stack<>();stack.push(root);while(!stack.isEmpty()){TreeNode temp = stack.peek();if(temp==null){stack.pop();list.add(stack.pop().val);}else{stack.pop();if(temp.right!=null) stack.push(temp.right);stack.push(temp);stack.push(null);if(temp.left!=null)stack.push(temp.left);}}return list.get(list.size()-cnt);}
}

🟢8. LCR 175. 計算二叉樹的深度——二叉樹的深度

題目跳轉:https://leetcode.cn/problems/er-cha-shu-de-shen-du-lcof/description/

class Solution {int max = 0;public int calculateDepth(TreeNode root) {int deep = 0;max = dfs(root,deep);return max;}public int dfs(TreeNode root,int num){if(root==null)return num;if(root.left==null&root.right==null){return num+1;}if(root.left!=null) max = Math.max(max,dfs(root.left,num+1));if(root.right!=null) max = Math.max(max,dfs(root.right,num+1));return max;}
}
class Solution {public int calculateDepth(TreeNode root) {int max = 0;if(root ==null)return 0;if(root.left == null&&root.right == null)return 1;Queue<TreeNode> queue = new LinkedList<>();queue.add(root);queue.add(null);while(!queue.isEmpty()){TreeNode temp = queue.poll();if(temp==null){max++;if(!queue.isEmpty())queue.add(null);}else{if(temp.right!=null) queue.add(temp.right);if(temp.left!=null)queue.add(temp.left);}}return max;}
}

🟢9. LCR 176. 判斷是否為平衡二叉樹——平衡二叉樹

題目跳轉:https://leetcode.cn/problems/ping-heng-er-cha-shu-lcof/description/

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public boolean isBalanced(TreeNode root) {if(root==null) return true;if(root.left==null&&root.right==null)return true;if(Math.abs(getHigh(root.left)-getHigh(root.right))<=1){return isBalanced(root.left)&&isBalanced(root.right);}return false;}public int getHigh(TreeNode root){if(root==null) return 0;return Math.max(getHigh(root.left),getHigh(root.right))+1;}
}

🟡10. LCR 177. 撞色搭配——數組中數字出現的次數I

題目跳轉:https://leetcode.cn/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/description/

相同的數異或為0,不同的異或為1。0和任何數異或等于這個數本身。

所以,數組里面所有數異或 = 目標兩個數異或 。 由于這兩個數不同,所以異或結果必然不為0。

假設數組異或的二進制結果為10010,那么說明這兩個數從右向左數第2位是不同的

那么可以根據數組里面所有數的第二位為0或者1將數組劃分為2個。

這樣做可以將目標數必然分散在不同的數組中,而且相同的數必然落在同一個數組中。

這兩個數組里面的數各自進行異或,得到的結果就是答案

在這里插入圖片描述

class Solution {public int[] sockCollocation(int[] nums) {int x = 0; // 用于記錄 A B 的異或結果/** 得到A^B的結果 基于異或運算的以下幾個性質 1. 交換律 2. 結合律 3. 對于任何數x,都有x^x=0,x^0=x */for (int val : nums) x ^= val;// x & (-x)本身的作用是得到最低位的1,int flag = x & (-x); // 而我們所需要的做到的是:利用這個1來進行分組,也就是做到將A和B區分開// 前面已經知道,x是我們需要的結果數A和B相異或的結果,也就是說,x的二進制串上的任何一個1,都能成為區分A和B的條件// 因此我們只需要得到x上的任意一個1,就可以做到將A和B區分開來int res = 0; // 用于記錄A或B其中一者// 分組操作for (int val : nums) {// 根據二進制位上的那個“1”進行分組// 需要注意的是,分組的結果必然是相同的數在相同的組,且還有一個結果數// 因此每組的數再與res=0一路異或下去,最終會得到那個結果數A或B// 且由于異或運算具有自反性,因此只需得到其中一個數即可if ((flag & val) != 0) {res ^= val;}}// 利用先前的x進行異或運算得到另一個,即利用自反性return new int[] {res, x ^ res};}
}

🟡11. LCR 178. 訓練計劃 VI——數組中數字出現的次數II

題目跳轉:https://leetcode.cn/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-ii-lcof/description/

排序

class Solution {public int trainingPlan(int[] actions) {int n = actions.length;Arrays.sort(actions);for(int i = 0;i<n-1;i = i+3){if(actions[i]!=actions[i+2]){return actions[i];}}return  actions[n-1];}
}

哈希表

class Solution {public int singleNumber(int[] actions) {HashMap<Integer, Integer> map = new HashMap<>();for (int number : actions) map.put(number, map.getOrDefault(number, 0) + 1);for (int number : map.keySet()) if (map.get(number) == 1) return number;return 0;}
}
class Solution {public int singleNumber(int[] nums) {int[] res = new int[32];int m = 1;int sum = 0;for(int i = 0; i < 32; i++){for(int j = 0; j < nums.length; j++){if((nums[j] & m) != 0){res[i]++;}}res[i] = res[i] % 3;sum = sum + res[i] * m;m = m << 1;}return sum;}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/697780.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/697780.shtml
英文地址,請注明出處:http://en.pswp.cn/news/697780.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Kubernetes服務網絡Ingress網絡模型分析、安裝和高級用法

文章目錄 1、Ingres簡介2、Ingres網絡模型分析3、安裝Ingress4、使用4.1、搭建測試環境4.2、域名訪問4.3、路徑重寫&#xff08;高級用法&#xff09;4.4、流量限制&#xff08;高級用法&#xff09; 5、總結 1、Ingres簡介 Ingress翻譯過來是“入口”的意思&#xff0c;也就是…

切換分支時候IDEA提示:workspace associated with branch feature has been restored

切換分支時候IDEA提示&#xff1a;workspace associated with branch feature has been restored 這個消息是指與"feature"分支關聯的工作區已經恢復。在Git中&#xff0c;工作區是指你當前正在進行修改和編輯的文件和目錄。當你切換分支時&#xff0c;Git會自動將工…

配置docker 支持GPU方法(Nvidia GPU)

參考官方文檔&#xff1a; https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html 系統版本&#xff1a;ubuntu 23.04 執行腳本如下&#xff1a; 1.Configure the production repository: curl -fsSL https://nvidia.github.io/lib…

怎么把試卷圖片轉換成word?這4種方法一看就會

怎么把試卷圖片轉換成word&#xff1f;在數字化日益盛行的今天&#xff0c;我們常常會面臨將紙質試卷或圖片中的試卷內容轉化為Word文檔的需求。無論是為了對試卷內容進行編輯、修改&#xff0c;還是為了在線共享、遠程教學&#xff0c;將圖片轉換為Word文檔都成為了至關重要的…

集成TinyMCE富文本編輯器

若依的基礎上集成TinyMCE富文本編輯器 前端bootstrap TinyMCE官網鏈接 TinyMCE所需靜態資源下載鏈接 開源項目-若依鏈接 將TinyMCE靜態資源包放入項目中&#xff1b; 代碼引入css&#xff1a; <!-- 引入TinyMCE CSS --><link th:href"{/ajax/libs/tinymce/j…

金田金業: 美聯儲泡沫正在破裂! 崩潰對黃金非常有利

The Great Recession Blog作者大衛哈吉斯表示&#xff0c;美聯儲一直以來都將繼續收緊貨幣政策&#xff0c;直到出現問題&#xff0c;但市場現在已經陷入泡沫。 他指出&#xff0c;泡沫正在破裂&#xff0c;崩潰最終將對黃金非常有利。 正當投資者聚焦美聯儲何時會降息&#xf…

Springboot 使用升級小記-循環依賴

springboot 使用已經非常廣泛了&#xff0c;它的版本迭代速度也比較快&#xff0c;過一段時間版本差異就會比較大。 由于低版本依賴的 spring 版本有漏洞問題&#xff0c;這次我們是從 2.2.6 版本直接升級到 2.7.16&#xff0c;升級 3.0 的話擔心差異更大。 這時直接修改依賴…

Jmeter 學習目錄(0)

Jmeter 所有內容均以學習為主輸出內容&#xff0c;按照最小單位和基礎進行輸出。 如果有看不懂&#xff0c;或者有不明確的內容&#xff0c;歡迎大家留言說明。 Mac Jmeter下載安裝啟動(1) Jmeter 目錄介紹(2) Jmeter基礎發起一次請求(3)

代碼隨想錄三刷day07

提示&#xff1a;文章寫完后&#xff0c;目錄可以自動生成&#xff0c;如何生成可參考右邊的幫助文檔 文章目錄 前言一、力扣206. 反轉鏈表二、力扣24. 兩兩交換鏈表中的節點 前言 遞歸寫法和雙指針法實質上都是從前往后翻轉指針指向&#xff0c;其實還有另外一種與雙指針法不同…

SD-WAN:快速改造升級企業原有網絡架構

隨著企業信息化的推進&#xff0c;傳統網絡架構已難以滿足企業日益復雜和多樣化的組網互聯需求。企業在不斷提高對網絡的要求&#xff0c;包括各辦公點的互聯數據傳輸、資源共享、視頻會議、ERP、OA、郵箱系統、云服務等應用需求&#xff0c;以及對網絡運維工作的簡化和降低難度…

Spring Event 快速入門

請直接看原文 : Spring Event&#xff0c;賊好用的業務解耦神器&#xff01; (qq.com) -------------------------------------------------------------------------------------------------------------------------------- 前言 Spring Event 同步使用 Spring Event 異…

架構篇35:微服務架構最佳實踐 - 方法篇

文章目錄 服務粒度拆分方法基礎設施小結上一篇我們談了實施微服務需要避免踩的陷阱,簡單提煉為: 微服務拆分過細,過分強調“small”。微服務基礎設施不健全,忽略了“automated”。微服務并不輕量級,規模大了后,“lightweight”不再適應。針對這些問題,我們看看微服務最佳…

ADAS智能駕駛測試知多少?

當涉及ADAS&#xff08;Advanced Driver Assistance Systems&#xff09;智能駕駛的測試時&#xff0c;有一個完整的測試體系可以用來評估系統的性能和功能。 1. 傳感器測試 1.1 傳感器校準測試 描述&#xff1a;確保傳感器&#xff08;如雷達、攝像頭、激光雷達等&#xff09;…

【stm32】hal庫學習筆記-UART/USART串口通信(超詳細!)

【stm32】hal庫學習筆記-UART/USART串口通信 hal庫驅動函數 CubeMX圖形化配置 導入LCD.ioc RTC設置 時鐘樹配置 設置LSE為RTC時鐘源 USART設置 中斷設置 程序編寫 編寫主函數 /* USER CODE BEGIN 2 */lcd_init();lcd_show_str(10, 10, 16, "Demo12_1:USART1-CH340&q…

【PythonGIS】Python線矢量等距離取點/線等分取點點創建矢量面

不多說&#xff0c;這是之前項目需求的代碼&#xff0c;已經是去年的了一直沒來的及發&#xff0c;今天抽出來一丟丟的空擋發一下。主要就是利用線矢量等距離生成點矢量&#xff0c;或者直接將線矢量等分生成點矢量&#xff0c;這個需求其實極限一下就是線轉點了&#xff08;將…

Java中各種O(PO,BO,DTO,VO等) 是不是人為增加系統復雜度?

Java中各種O(PO,BO,DTO,VO等) 是不是人為增加系統復雜度&#xff1f; 在Java和其他編程語言的開發過程中&#xff0c;經常會用到幾個以"O"結尾的縮寫&#xff0c;比如PO,BO,DTO,VO等等&#xff0c;O在這里是Object的縮寫&#xff0c;不同的O代表了不同的數據類型&am…

onlyoffice7.5.1 實現填寫表單 word+html form雙向綁定功能

說明&#xff1a;目前官方已經更新wordhtml為8.0以前的&#xff0c;目前官方新版本8.0增加了pdf綁定&#xff0c;這個我考慮在以后研究努力實現。 onlyoffice雙向綁定form表單數據

Java基礎 - 13 Queue之DelayQueue、PriorityQueue、PriorityBlockingQueue講解

在Java的隊列世界里&#xff0c;有三位大佬&#xff0c;他們分別是DelayQueue、PriorityQueue和PriorityBlockingQueue。今天&#xff0c;讓我們一起揭開他們神秘的面紗&#xff0c;看看他們各自的特點和用途吧&#xff01; DelayQueue 首先&#xff0c;讓我們來認識一下Delay…

2.22 作業

順序表 運行結果 fun.c #include "fun.h" seq_p create_seq_list() {seq_p L (seq_p)malloc(sizeof(seq_list));if(LNULL){printf("空間申請失敗\n");return NULL;}L->len 0; bzero(L,sizeof(L->data)); return L; } int seq_empty(seq_p L) {i…

工廠方法模式Factory Method

1.模式定義 定義一個用于創建對象的接口&#xff0c;讓子類決定實例化哪一個類。Factory Method 使得一個類的實例化延遲到子類 2.使用場景 1.當你不知道改使用對象的確切類型的時候 2.當你希望為庫或框架提供擴展其內部組件的方法時 主要優點&#xff1a; 1.將具體產品和創建…