This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3290/ | |
class Solution: | |
def middleNode(self, head: ListNode) -> ListNode: | |
slow = head | |
fast = head | |
while fast: | |
fast = fast.next | |
if not fast: | |
break |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/529/week-2/3291/ | |
class Solution: | |
def backspaceCompare(self, S: str, T: str) -> bool: | |
a = len(S) - 1 | |
b = len(T) - 1 | |
skipA = 0 | |
skipB = 0 | |
while a >= 0 or b >= 0: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/529/week-2/3292/ | |
class MinStack: | |
def __init__(self): | |
self.stack = [] | |
self.minStack = [] | |
def push(self, x: int) -> None: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3293/ | |
# Definition for a binary tree node. | |
# class TreeNode: | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
class Result: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/529/week-2/3297/ | |
import java.util.Collections | |
import java.util.PriorityQueue | |
class Solution { | |
fun lastStoneWeight(stones: IntArray): Int { | |
val pq = PriorityQueue<Int>(Collections.reverseOrder()) | |
for (s in stones) { | |
pq.add(s) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def findMaxLength(self, nums: List[int]) -> int: | |
history = {0: -1} | |
balance = 0 | |
res = 0 | |
for i in range(len(nums)): | |
balance += 1 if nums[i] else -1 | |
if balance in history: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/529/week-2/3299/ | |
class Solution { | |
fun stringShift(s: String, shift: Array<IntArray>): String { | |
val singleShift: Pair<Int, Int> = calculateSingleShift(shift) | |
return shiftCharacters(s, singleShift.first, singleShift.second % s.length) | |
} | |
fun shiftCharacters(s: String, direction: Int, n: Int): String { | |
if (n == 0) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/530/week-3/3300/ | |
class Solution { | |
fun productExceptSelf(nums: IntArray): IntArray { | |
var output = nums.copyOf() | |
for (i in nums.lastIndex - 1 downTo 0) { | |
output[i] *= output[i + 1] | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/530/week-3/3301/ | |
class Solution: | |
def checkValidString(self, s: str) -> bool: | |
dp = {} | |
return self.isValid(s, 0, 0, dp) | |
def isValid(self, s: str, i: int, c: int, dp: dict) -> bool: | |
if (i, c) in dp: | |
return dp[i, c] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/530/week-3/3302/ | |
class Solution { | |
fun numIslands(grid: Array<CharArray>): Int { | |
var result = 0 | |
for (row in grid.indices) { | |
for (col in grid[0].indices) { | |
if (grid[row][col] == '1') { | |
result += 1 | |
dfs(grid, row, col) | |
} |