力扣hot100題(1)

目錄

  • 1、兩數之和
  • 2、移動零
  • 3、相交鏈表
  • 4、有效的括號
  • 5、反轉鏈表
  • 6、回文鏈表
  • 7、環形鏈表
  • 8、環形鏈表II
  • 9、合并兩個有序鏈表
  • 10、二叉樹的中序遍歷

1、兩數之和

1. 兩數之和 - 力扣(LeetCode)在這里插入圖片描述

方法1:

class Solution {public int[] twoSum(int[] nums, int target) {int[] ret = new int[2];for(int i=0;i<nums.length;i++){for(int j=i+1;j<nums.length;j++) {if(nums[i]+nums[j]==target) {ret[0] = i;ret[1] = j;return ret;}                }}return ret;}
}

方法2:

在這里插入圖片描述

class Solution {public int[] twoSum(int[] nums, int target) {int[] ret = new int[2];HashMap<Integer,Integer> hash = new HashMap<>();// key:nums[i],value:ifor (int i = 0; i < nums.length; i++) {if (hash.containsKey(target - nums[i])) {ret[0] = i;ret[1] = hash.get(target - nums[i]);return ret;}hash.put(nums[i], i);}return ret;}
}

2、移動零

283. 移動零 - 力扣(LeetCode)

在這里插入圖片描述

class Solution {public void moveZeroes(int[] nums) {int dest = -1;for (int cur = 0; cur < nums.length; cur++) {if (nums[cur] != 0) {dest++;int tmp = nums[cur];nums[cur] = nums[dest];nums[dest] = tmp;}}}
}

3、相交鏈表

160. 相交鏈表 - 力扣(LeetCode)

在這里插入圖片描述

public class Solution {public ListNode getIntersectionNode(ListNode headA, ListNode headB) {ListNode cur1 = headA;ListNode cur2 = headB;int count1 = 0;//統計A結點個數int count2 = 0;//統計B結點個數while (cur1 != null) {count1++;cur1 = cur1.next;}while (cur2 != null) {count2++;cur2 = cur2.next;}if (count1 > count2) {for (int i = 0; i < count1 - count2; i++) {headA = headA.next;}}if (count1 < count2) {for (int i = 0; i < count2 - count1; i++) {headB = headB.next;}}while (headA != headB) {headA = headA.next;headB = headB.next;}return headA;}
}

4、有效的括號

20. 有效的括號 - 力扣(LeetCode)

在這里插入圖片描述

class Solution {public boolean isValid(String s) {Stack<Character> stack = new Stack<>();for (int i = 0; i < s.length(); i++) {char ch = s.charAt(i);if (ch == '(' || ch == '[' || ch == '{') {//如果是左括號,入棧stack.push(ch);} else {//如果是右括號if (stack.empty()) {//判斷棧是否為空return false;} else {char tmp = stack.peek();if (ch == ')' && tmp == '('|| ch == ']' && tmp == '['|| ch == '}' && tmp == '{') {stack.pop();} else {return false;}}}}return stack.empty();}
}

5、反轉鏈表

206. 反轉鏈表 - 力扣(LeetCode)

在這里插入圖片描述

方法1:

class Solution {public ListNode reverseList(ListNode head) {if(head==null || head.next==null){return head;}        //1.先反轉后面的鏈表ListNode newHead = reverseList(head.next);//返回反轉后的頭head.next.next = head;head.next = null;return newHead;}
}

方法2:

class Solution {public ListNode reverseList(ListNode head) {if (head == null) {return head;}ListNode cur = head.next;head.next = null;while (cur != null) {ListNode curNext = cur.next;cur.next = head;head = cur;cur = curNext;}return head;}
}

6、回文鏈表

234. 回文鏈表 - 力扣(LeetCode)

在這里插入圖片描述

class Solution {public boolean isPalindrome(ListNode head) {if (head == null) {return false;}ListNode fast = head;ListNode slow = head;//找到中間結點while (fast != null && fast.next != null) {fast = fast.next.next;slow = slow.next;}ListNode Newhead = reverseList(slow);ListNode tmp1 = head;ListNode tmp2 = Newhead;while (tmp2 != null) {//if (tmp2.val != tmp1.val) {return false;} else {tmp1 = tmp1.next;tmp2 = tmp2.next;}}return true;}//翻轉鏈表public ListNode reverseList(ListNode head) {if (head == null) {return head;}ListNode cur = head.next;head.next = null;while (cur != null) {ListNode curNext = cur.next;cur.next = head;head = cur;cur = curNext;}return head;}
}

7、環形鏈表

141. 環形鏈表 - 力扣(LeetCode)

在這里插入圖片描述

public class Solution {public boolean hasCycle(ListNode head) {if (head == null) {return false;}ListNode fast = head.next;ListNode slow = head;while (fast != null && fast.next != null) {fast = fast.next.next;slow = slow.next;if (fast == slow) {return true;}}return false;}
}

8、環形鏈表II

142. 環形鏈表 II - 力扣(LeetCode)

在這里插入圖片描述

public class Solution {public ListNode detectCycle(ListNode head) {if (head == null) {return null;}ListNode fast = head;ListNode slow = head;while (fast != null && fast.next != null) {fast = fast.next.next;slow = slow.next;if (fast == slow) {// 兩人相遇,說明鏈表成環while (head != slow) {slow = slow.next;head = head.next;}//return head;}}return null;}
}

9、合并兩個有序鏈表

21. 合并兩個有序鏈表 - 力扣(LeetCode)

在這里插入圖片描述

方法1:

class Solution {public ListNode mergeTwoLists(ListNode list1, ListNode list2) {if (list1 == null) {return list2;}if (list2 == null) {return list1;}// 1.比大小if (list1.val < list2.val) {list1.next = mergeTwoLists(list1.next, list2);return list1;} else {list2.next = mergeTwoLists(list2.next, list1);return list2;}}
}

方法2:

class Solution {public ListNode mergeTwoLists(ListNode list1, ListNode list2) {ListNode l1 = list1;ListNode l2 = list2;if (l1 == null) {return l2;}if (l2 == null) {return l1;}ListNode newHead = null;ListNode newTail = null;while (l1 != null && l2 != null) {if (l1.val <= l2.val) {//if (newTail == null) {newHead = newTail = l1;l1 = l1.next;} else {newTail.next = l1;newTail = l1;l1 = l1.next;}} else {if (newTail == null) {newHead = newTail = l2;l2 = l2.next;} else {newTail.next = l2;newTail = l2;l2 = l2.next;}}}//if (l1 == null) {newTail.next = l2;} else {newTail.next = l1;}return newHead;}
}

10、二叉樹的中序遍歷

94. 二叉樹的中序遍歷 - 力扣(LeetCode)

在這里插入圖片描述

class Solution {List<Integer> ret = new LinkedList<Integer>();public List<Integer> inorderTraversal(TreeNode root) {        if (root == null) {return ret;}dfs(root, ret);return ret;}public void dfs(TreeNode root, List<Integer> ret) {if (root == null) {return;}dfs(root.left, ret);ret.add(root.val);dfs(root.right, ret);}
}

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

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

相關文章

C++的回顧與學習之C++入門基礎

目錄 1、C入門 1&#xff09;C關鍵字 2&#xff09;命名空間 3&#xff09;C中的輸入輸出 4&#xff09;缺省參數 5&#xff09;函數重載 6&#xff09;引用 引用和指針的不同點&#xff1a; 7&#xff09;auto關鍵字 8&#xff09;內聯函數 9&#xff09;指針空值nu…

【使用Android Studio調試手機app時候手機老掉線問題】

如果你各種方式都嘗試失敗了&#xff0c; 請看這里 連接時候通過logcat查看你手機Android的平臺去SDK下載所有對應的平臺SDK重新連接嘗試

二叉樹題解——驗證二叉搜索樹【LeetCode】前序遍歷

98. 驗證二叉搜索樹 &#x1f50d; 題目目標 判斷一棵二叉樹是否為有效的二叉搜索樹&#xff08;BST&#xff09;&#xff0c;定義如下&#xff1a; 左子樹所有節點 < 根節點 右子樹所有節點 > 根節點 且左右子樹也必須是二叉搜索樹 一、算法邏輯&#xff08;逐步通…

Javaweb - 10.3 Servlet 生命周期

目錄 生命周期簡介 生命周期測試 load-on-startup 補充&#xff1a;defaultServlet Servlet 的繼承結構 1. 頂級的 Servlet 接口 2. 抽線的類 GenericServlet 3. HttpServlet 抽象類 4. 自定義 Servlet 補充&#xff1a; 完&#xff01; 生命周期簡介 什么是生命周…

RSA數字簽名方案的C語言實現(帶測試)

RSA 算法的 C語言實現通常比較復雜&#xff0c;但已經有許多密碼算法庫實現了 RSA 算法&#xff0c;例如OpenSSL、Libgcrypt? 和 Botan ?等。我們可以在這些庫的基礎上進行配置或移植&#xff0c;從而快速實現密碼算法。但這些庫主要面向大量設備進行優化&#xff0c;如通用計…

創客匠人視角:知識變現與創始人 IP 打造的破局之道

當知識付費從流量紅利期進入精耕細作階段&#xff0c;為何專業能力強的內容創作者反而難以變現&#xff1f;創客匠人通過 1500 案例陪跑發現&#xff1a;缺乏 IP 思維的知識輸出如同霧中航行&#xff0c;而創始人 IP 打造正是連接知識價值與商業變現的核心橋梁。一、定位重構&…

結構分析設計軟件 SCIA Engineer 25.0 x64

詳情 Nemetschek SCIA Engineer是一家從事多項目編程、分析和軟件設計的公司。該軟件具有廣泛的不同功能。該軟件可用于以簡單的方式設計建筑物、工業工廠和橋梁。 Nemetschek SCIA Engineer軟件的特點和功能&#xff1a; BIM模型人 使用網格和故事 3D風 自由負載 互聯網…

怎么處理[TOO_MANY_REQUESTS/12/disk usage exceeded flood-stage watermark

這個錯誤說明 Elasticsearch 的磁盤空間嚴重不足&#xff0c;已觸及最高級別&#xff08;flood-stage&#xff09;的水位線。作為自我保護機制&#xff0c;Elasticsearch ?自動將受影響的索引設置為只讀模式 (read-only-allow-delete)?&#xff0c;從而阻止寫入操作&#xff…

pytorch學習-11卷積神經網絡(高級篇)

2.線性模型 3.梯度下降算法 4.反向傳播(用pytorch算梯度) 5.用pytorch實現線性回歸 6.logistic回歸 7.處理多維特征的輸入 8.加載數據集 9.多分類問題 10.卷積神經網絡(基礎篇) 11.卷積神經網絡&#xff08;高級篇&#xff09;_嗶哩嗶哩_bilibili 11.1 GoogleNet Google…

ubuntu 安裝QT

在 Ubuntu 系統上安裝 Qt 可以通過以下步驟完成&#xff0c;以下是詳細的安裝指南 &#xff1a; 1. 安裝前的準備工作 在開始安裝 Qt 之前&#xff0c;需要確保你的 Ubuntu 系統已經更新到最新版本&#xff0c;并且安裝了一些必要的依賴。 1.1 更新系統 首先&#xff0c;打…

CppCon 2018 學習:RAPID PROTOTYPING OF GRAPHICS SHADERS IN

這段內容在講**著色器&#xff08;Shader&#xff09;**的基礎概念&#xff0c;尤其是它在現代 GPU&#xff08;圖形處理單元&#xff09;中的作用。以下是逐條解釋與理解&#xff1a; “Depicting depth perception in 3D models or illustrations by varying levels of darkn…

Angular v20版本正式發布

過去幾年對 Angular 來說很具變革性,我們推出了像 Signals 這樣的反應性功能和 Zoneless 應用的強大能力。我們希望這些功能可以幫助 Angular 社區構建下一代的 Web 應用,實現快速上市和強大的性能。 我們的旅程才剛剛開始!Angular v20 是最新的發布版本,我們花費了無數個小…

Oracle如何使用序列 Oracle序列使用教程

Oracle序列&#xff08;sequence&#xff09;是一種數據庫項&#xff0c;能夠生成一個整數序列。通常用于填充數字類型的主鍵列。 Oracle序列 Oracle序列使用教程&#xff1a; 1、創建序列&#xff1a; CREATE SEQUENCE sequence_name[START WITH start_num][INCREMENT BY incr…

深入探索 Vanna:讓數據庫交互更智能

深入探索 Vanna&#xff1a;讓數據庫交互更智能 在數字化時代&#xff0c;與數據庫進行高效交互是許多開發者、數據分析師和企業面臨的挑戰。傳統的 SQL 查詢編寫不僅需要對數據庫結構有深入的了解&#xff0c;還需要花費大量的時間和精力來調試和優化。Vanna&#xff0c;一個…

C#上位機之網口通信與協議!

文章目錄前言一、網口通信概念二、使用網口通信準備三、使用步驟前言 C#上位機之網口通信與協議&#xff01; 一、網口通信概念 定義 &#xff1a;Socket 可以理解為一個通信端點&#xff0c;它提供了應用程序與網絡之間的接口&#xff0c;使得應用程序能夠在網絡上發送和接收…

Android Studio 創建類時如何自動添加類注釋

打開IDEA或AS&#xff0c;點擊菜單欄File——Settings——Editor——File and Code Templates。 點擊右邊Tab頁的Includes&#xff0c;選擇File Header&#xff0c;修改類頭模版&#xff0c;如圖&#xff1a; 記得選中Project&#xff0c;否則默認是整個AS都會進行設置

C++11:shared_ptr的設計哲學(原理+源碼):內存安全和性能的架構權衡

0.簡介 在C編程世界中&#xff0c;內存管理是一把雙刃劍&#xff0c;手動管理帶來了極致的內存控制能力&#xff0c;但也帶來了像內存泄漏&#xff0c;野指針等問題&#xff1b;自動垃圾回收雖然安全&#xff0c;但卻會帶來一定的性能損耗。本文將介紹C11引入shared_ptr&#…

Mysql EXPLAIN 執行計劃

EXPLAIN SELECT SQl。。。。界面filtered儲引擎返回的數據在經過服務器層 WHERE 條件過濾后&#xff0c;剩余數據占總行數的百分比估計值rows * filtered/100 越接近100%效率越高rowspossible_keys 可能選擇的索引key最終決定選擇的行partitions問了哪些分區select_type查詢…

力扣刷題記錄【1】146.LRU緩存

前言&#xff1a; 請你設計并實現一個滿足 LRU (最近最少使用) 緩存 約束的數據結構。 實現 LRUCache 類&#xff1a; LRUCache(int capacity) 以 正整數 作為容量 capacity 初始化 LRU 緩存int get(int key) 如果關鍵字 key 存在于緩存中&#xff0c;則返回關鍵字的值&…

西門子S7-1200 PLC主流通信方法及應用

一、通信基礎 1. 網絡術語與設備 - 關鍵設備&#xff1a;交換機、路由器、網關等。 - 物理接口&#xff1a;RS-485&#xff08;支持多點通信&#xff09;、RS-232C&#xff08;點對點串行通信&#xff09;。 2. OSI參考模型 - 核心框架&#xff1a;理解協議分層&…