Simon Lai's Blog
  • 首頁
  • 歸檔
  • 分類
  • 標籤
  • 關於

17. Letter Combinations of a Phone Number - Medium

前往題目 之前寫的文章 思路 Map數字與字母 backtracking,取到需要的數量就加到答案裡 Codeclass Solution { private ArrayList<String> res; private Map<Character, String> digitToChar = Map.of( '2', "abc",
2024-04-08
Leetcode > Medium
#Leetcode #心得 #String #Hash Table #Backtracking

11. Container With Most Water — Medium

前往題目 之前寫的文章 想法 雙指針 每次移動短的那邊 Greedy 思路 左右指針從array的開始與結尾 每個area都判斷一次是否大於maxArea 短的那邊移動,這樣才有機會取到更大的 直到左指針和右指針相撞結束 Codeclass Solution { public int maxArea(int[] height) { int max
2024-04-07
Leetcode > Medium
#Leetcode #心得 #Array #Two Pointers #Greedy

105. Construct Binary Tree from Preorder and Inorder Traversal — Medium

前往題目 之前寫過的文章 想法 觀察inorder和preorder的規律 思路 preorder的第一項一定是root,這是它的特性 inorder的每個父節點,左邊一定是left subtree,右邊一定是right subtree 把inorder的val->index資訊存下來 builder function每個循環都取得當前值,以及取得mid的index 根據mid來呼叫bu
2024-04-07
Leetcode > Medium
#Leetcode #心得 #Binary Tree #Array #Hash Table #Tree #Divide and Conquer

2017. Grid Game - Medium

前往題目 思路 計算第一列與第二列每一格的prefix sum 疊代一列 每一格都算出當前如果待在top可以拿到多少points,下去bottom可以拿到多少points robot2取top和bottom比較大的那個 結果是每次循環都盡可能取更小的robot2分數 Code Java版本 class Solution { public long gridGame(int[
2024-04-05
Leetcode > Medium
#Leetcode #心得 #Array #Matrix #Prefix Sum

62. Unique Paths - Medium

前往題目 之前寫的文章 思路這次自己寫出來了,只是用的是top-down而非bottom-up 第一行和第一列一定都是1,因為只能向右和向下走 每一格都是上與左一格的和 最後終點即是答案 Codetop-down class Solution { public int uniquePaths(int m, int n) { in
2024-04-03
Leetcode > Medium
#Leetcode #心得 #Math #Dynamic Programming #Combinatorics

217. Contains Duplicate - Easy

前往題目 之前寫的文章 思路 使用HashSet Codeclass Solution { public boolean containsDuplicate(int[] nums) { Set<Integer> set = new HashSet<>(); for (int num : nums) {
2024-04-03
Leetcode > Easy
#Leetcode #心得 #Array #Hash Table #Sorting

2215. Find the Difference of Two Arrays - Easy

前往題目 想法 用兩個set,各自裝nums1和nums2的元素,然後利用.contains來查看另一個是否也有,沒有的話就加到答案裡 思路同想法 Codeclass Solution { public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
2024-04-02
Leetcode > Easy
#Leetcode #心得 #Array #Hash Table

724. Find Pivot Index - Easy

前往題目 想法 算出每個點的左邊sum,以及右邊sum,最後再看是否有一樣的即可 思路同想法 Codeclass Solution { public int pivotIndex(int[] nums) { int[] sumLeft = new int[nums.length]; int[] sumRight = new int[n
2024-04-02
Leetcode > Easy
#Leetcode #心得 #String #Two Pointers #String Matching

448. Find All Numbers Disappeared in an Array - Easy

前往題目 想法 用個set或陣列之類的存,最後再看一遍缺了哪個 如果使用傳進來的nums[]不知道該怎麼做才能O(1)空間 思路Time: O(n)Space: O(1) 疊代所有陣列中的數字,把數字當作index,標記該index的數字為負數 遇到負數就跳過 最後疊代一次1~n看原本的陣列裡面哪個index的數字是正數,就是缺失的數字 Code簡易版 class Solution &#
2024-03-30
Leetcode > Easy
#Leetcode #心得 #Array #Hash Table

535. Encode and Decode TinyURL - Medium

前往題目 想法 直接產出隨機字串,然後把longUrl和code做連結 思路encode: 檢查是否該longUrl已經在資料庫,有的話回傳對應的code 沒有的話產生一個code,並且檢查是否有重複的,有的話重新生成 存到map裡 decode: 回傳對應的longUrl Codepublic class Codec { // shortUrl -> long
2024-03-29
Leetcode > Medium
#Leetcode #心得 #String #Hash Table #Design #Hash Function
1…1415161718…36

搜尋

Hexo Fluid