0%

LeetCode 1209 - Remove All Adjacent Duplicates in String II

You are given a string s and an integer k, a k duplicate removal consists of choosing k adjacent and equal letters from s and removing them, causing the left and the right side of the deleted substring to concatenate together.

We repeatedly make k duplicate removals on s until we no longer can.

Return the final string after all such duplicate removals have been made. It is guaranteed that the answer is unique.

LeetCode 225 - Implement Stack using Queues

Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty).

Implement the MyStack class:

  • void push(int x) Pushes element x to the top of the stack.
  • int pop() Removes the element on the top of the stack and returns it.
  • int top() Returns the element on the top of the stack.
  • boolean empty() Returns true if the stack is empty, false otherwise.

Notes:

LeetCode 1679 - Max Number of K-Sum Pairs

You are given an integer array nums and an integer k.

In one operation, you can pick two numbers from the array whose sum equals k and remove them from the array.

Return the maximum number of operations you can perform on the array.

example

Input: nums = [1,2,3,4], k = 5
Output: 2
Explanation: Starting with nums = [1,2,3,4]:
- Remove numbers 1 and 4, then nums = [2,3]
- Remove numbers 2 and 3, then nums = []
There are no more pairs that sum up to 5, hence a total of 2 operations.
Input: nums = [3,1,3,4,3], k = 6
Output: 1
Explanation: Starting with nums = [3,1,3,4,3]:
- Remove the first two 3's, then nums = [1,4,3]
There are no more pairs that sum up to 6, hence a total of 1 operation.

How can we solve this problem?

這題就是要移除Array中2個elements加起來等於k的操作有幾次。
第一個解法,我們可以使用sorting以及two-pointer approach來解決。先將array排序,然後設置i為0,jn-1,直接使用iteration找出nums[i]+nums[j] = k的數,然後answer+1即可。

LeetCode 581 - Shortest Unsorted Continuous Subarray

Given an integer array nums, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be sorted in ascending order.

Return the shortest such subarray and output its length.

example:

Input: nums = [2,6,4,8,10,9,15]
Output: 5
Explanation: You need to sort [6, 4, 8, 10, 9] in ascending order to make the whole array sorted in ascending order.
Input: nums = [1,2,3,4]
Output: 0
Input: nums = [1]
Output: 0

How can we solve this problem?

這題比較難懂一點點,這裡先做一下題目的解釋。這題主要先問的是在輸入的Array裡面找到一個最小需要排序的Sub-array
從例子[2,6,4,8,10,9,15]中,我們可以很明顯的看到[6,4,8,10,9]並不是ascending order(順序),而這個sub-array要進行排序的話,所有elements都需要進行排序,所以,他的length是5
再舉另外一個例子[1,3,2,3,3],這個Array我們可以看到[3,2,3,3]並不是順序的,但是在這個sub-array裡面,只有[3,2]需要排序,所以,他的結果會是2

LeetCode 905 - Sort Array By Parity

Given an integer array nums, move all the even integers at the beginning of the array followed by all the odd integers.

Return any array that satisfies this condition.

example:

Input: nums = [3,1,2,4]
Output: [2,4,3,1]
Explanation: The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.
Input: nums = [0]
Output: [0]

How can we solve this problem?

這個問題很簡單,就是把偶數移動到Array的前面,基數移動到後面。我們這裡可以使用Two-pointer approach, i為尋找前面的基數,而j 為尋找後面的偶數,只要nums[i]為基數,nums[j]為偶數就進行交換。

LeetCode 844 - Backspace String Compare

Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character.

Note that after backspacing an empty text, the text will continue empty.

example:

Input: s = "ab#c", t = "ad#c"
Output: true
Explanation: Both s and t become "ac".
Input: s = "ab##", t = "c#d#"
Output: true
Explanation: Both s and t become "".
Input: s = "a#c", t = "b"
Output: false
Explanation: s becomes "c" while t becomes "b".

How can we solve this problem?

這題主要要什麼比較2個String移除於#前的字符後是否為相同的String,就相當於Backspace(#) 字符。 這題有2種解法:

LeetCode 669 - Trim a Binary Search Tree

Given the root of a binary search tree and the lowest and highest boundaries as low and high, trim the tree so that all its elements lies in [low, high]. Trimming the tree should not change the relative structure of the elements that will remain in the tree (i.e., any node’s descendant should remain a descendant). It can be proven that there is a unique answer.

LeetCode 700 - Search in a Binary Search Tree

You are given the root of a binary search tree (BST) and an integer val.

Find the node in the BST that the node’s value equals val and return the subtree rooted with that node. If such a node does not exist, return null.

example

Input: root = [4,2,7,1,3], val = 2
Output: [2,1,3]
Input: root = [4,2,7,1,3], val = 5
Output: []

How can we solve this problem?

在解決問題之前,我們需要知道什麼是Binary Search Tree。根據BST的定義:

LeetCode 1260 - Shift 2D Grid

Given a 2D grid of size m x n and an integer k. You need to shift the grid k times.

In one shift operation:

  • Element at grid[i][j] moves to grid[i][j + 1].
  • Element at grid[i][n - 1] moves to grid[i + 1][0].
  • Element at grid[m - 1][n - 1] moves to grid[0][0].

Return the 2D grid after applying shift operation k times.

example

LeetCode 59 - Spiral Matrix II

Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.

example

Input: n = 3
Output: [[1,2,3],[8,9,4],[7,6,5]]
Input: n = 1
Output: [[1]]

How can we solve this problem?

這題跟Spiral Matrix做法差不多,我們不難發現他的移動模式就是(右→下↓左←上),我們只需要通過幾個變數來限制移動的步數。另外,這題需要我們返回一個n2的array,所以,需要額外定義一個counter來記錄每次插入的數值為多少,因此,只要counter 到達n2的大小就知道是否完成插入所需的值。