Leetcode#200. Number of Islands
ProblemGiven an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.
Example 1:
12345678Input: grid = [ ["1","1","1","1","0"], ["1","1","0","1","0"] ...
Leetcode#5. Longest Palindromic Substring
ProblemGiven a string s, return the longest
palindromic
substring
in
1s
.
Example 1:
1234Input: s = "babad"Output: "bab"Explanation: "aba" is also a valid answer.
Example 2:
123Input: s = "cbbd"Output: "bb"
Constraints:
1 <= s.length <= 1000
s consist of only digits and English letters.
Solve123456789101112131415161718192021class Solution: def longestPalindrome(self, s: str) -> str: res = "" for i in range ...
Leetcode#124. Binary Tree Maximum Path Sum
ProblemA path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root.
The path sum of a path is the sum of the node’s values in the path.
Given the root of a binary tree, return the maximum path sum of any non-empty path.
Example 1:
!https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg
1234Input: root = [1,2,3]Output: 6 ...
Leetcode#841. Keys and Rooms
ProblemThere are n rooms labeled from 0 to n - 1 and all the rooms are locked except for room 0. Your goal is to visit all the rooms. However, you cannot enter a locked room without having its key.
When you visit a room, you may find a set of distinct keys in it. Each key has a number on it, denoting which room it unlocks, and you can take all of them with you to unlock the other rooms.
Given an array rooms where rooms[i] is the set of keys that you can obtain if you visited room i, return true ...
Leetcode#392. Is Subsequence
ProblemGiven two strings s and t, return true if s is a subsequence of t, or false otherwise.
A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).
Example 1:
123Input: s = "abc", t = "ahbgdc"Output: true
Example 2:
123Input: s = "axc ...
Leetcode#1493. Longest Subarray of 1's After Deleting One Element
ProblemGiven a binary array nums, you should delete one element from it.
Return the size of the longest non-empty subarray containing only 1‘s in the resulting array. Return 0 if there is no such subarray.
Example 1:
1234Input: nums = [1,1,0,1]Output: 3Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
Example 2:
1234Input: nums = [0,1,1,1,0,1,1,0,1]Output: 5Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest suba ...
Leetcode#1456. Maximum Number of Vowels in a Substring of Given Length
ProblemGiven a string s and an integer k, return the maximum number of vowel letters in any substring of s with length k.
Vowel letters in English are 'a', 'e', 'i', 'o', and 'u'.
Example 1:
1234Input: s = "abciiidef", k = 3Output: 3Explanation: The substring "iii" contains 3 vowel letters.
Example 2:
1234Input: s = "aeiou", k = 2Output: 2Explanation: Any substring of length 2 contains 2 vowels.
Example 3:
1234Input: s = &qu ...
Leetcode#643. Maximum Average Subarray I
ProblemYou are given an integer array nums consisting of n elements, and an integer k.
Find a contiguous subarray whose length is equal to k that has the maximum average value and return this value. Any answer with a calculation error less than 10-5 will be accepted.
Example 1:
1234Input: nums = [1,12,-5,-6,50,3], k = 4Output: 12.75000Explanation: Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75
Example 2:
123Input: nums = [5], k = 1Output: 5.00000
Constraints:
n == nums.length
1 < ...
Leetcode#283. Move Zeroes
ProblemGiven an integer array nums, move all 0‘s to the end of it while maintaining the relative order of the non-zero elements.
Note that you must do this in-place without making a copy of the array.
Example 1:
123Input: nums = [0,1,0,3,12]Output: [1,3,12,0,0]
Example 2:
123Input: nums = [0]Output: [0]
Constraints:
1 <= nums.length <= 10^4
2^31 <= nums[i] <= 2^31 - 1
Follow up:
Could you minimize the total number of operations done?
Solve123456789101112class Solution: def mov ...
Leetcode#1768. Merge Strings Alternately
ProblemYou are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string.
Return the merged string.
Example 1:
1234567Input: word1 = "abc", word2 = "pqr"Output: "apbqcr"Explanation: The merged string will be merged as so:word1: a b cword2: p q rmerged: a p b q c r
Example 2:
1234567Input: word1 = & ...