Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added tasks 50, 52, 69, 149#66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletionsREADME.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -385,6 +385,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
| 0007 |[Reverse Integer](src/main/python/g0001_0100/s0007_reverse_integer/Solution0007.py)| Medium | Top_Interview_Questions, Math, Big_O_Time_O(log10(x))_Space_O(1) | 37 | 69.51
| 0009 |[Palindrome Number](src/main/python/g0001_0100/s0009_palindrome_number/Solution0009.py)| Easy | Math, Big_O_Time_O(log10(x))_Space_O(1) | 4 | 80.28
| 0172 |[Factorial Trailing Zeroes](src/main/python/g0101_0200/s0172_factorial_trailing_zeroes/Solution0172.py)| Medium | Top_Interview_Questions, Math | 0 | 100.00
| 0050 |[Pow(x, n)](src/main/python/g0001_0100/s0050_powx_n/Solution0050.py)| Medium | Top_Interview_Questions, Math, Recursion | 0 | 100.00

#### Udemy Strings

Expand DownExpand Up@@ -721,6 +722,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
| 0077 |[Combinations](src/main/python/g0001_0100/s0077_combinations/Solution0077.py)| Medium | Backtracking | 91 | 95.48
| 0046 |[Permutations](src/main/python/g0001_0100/s0046_permutations/Solution0046.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking, Big_O_Time_O(n\*n!)_Space_O(n+n!) | 0 | 100.00
| 0039 |[Combination Sum](src/main/python/g0001_0100/s0039_combination_sum/Solution0039.py)| Medium | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(2^n)_Space_O(n+2^n) | 4 | 90.51
| 0052 |[N-Queens II](src/main/python/g0001_0100/s0052_n_queens_ii/Solution0052.py)| Hard | Backtracking | 3 | 96.02
| 0022 |[Generate Parentheses](src/main/python/g0001_0100/s0022_generate_parentheses/Solution0022.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Dynamic_Programming, Backtracking, Big_O_Time_O(2^n)_Space_O(n) | 0 | 100.00
| 0079 |[Word Search](src/main/python/g0001_0100/s0079_word_search/Solution0079.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Backtracking, Big_O_Time_O(4^(m\*n))_Space_O(m\*n) | 623 | 94.85

Expand DownExpand Up@@ -779,6 +781,9 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
| 0009 |[Palindrome Number](src/main/python/g0001_0100/s0009_palindrome_number/Solution0009.py)| Easy | Math, Big_O_Time_O(log10(x))_Space_O(1) | 4 | 80.28
| 0066 |[Plus One](src/main/python/g0001_0100/s0066_plus_one/Solution0066.py)| Easy | Top_Interview_Questions, Array, Math | 0 | 100.00
| 0172 |[Factorial Trailing Zeroes](src/main/python/g0101_0200/s0172_factorial_trailing_zeroes/Solution0172.py)| Medium | Top_Interview_Questions, Math | 0 | 100.00
| 0069 |[Sqrt(x)](src/main/python/g0001_0100/s0069_sqrtx/Solution0069.py)| Easy | Top_Interview_Questions, Math, Binary_Search | 0 | 100.00
| 0050 |[Pow(x, n)](src/main/python/g0001_0100/s0050_powx_n/Solution0050.py)| Medium | Top_Interview_Questions, Math, Recursion | 0 | 100.00
| 0149 |[Max Points on a Line](src/main/python/g0101_0200/s0149_max_points_on_a_line/Solution0149.py)| Hard | Top_Interview_Questions, Array, Hash_Table, Math, Geometry | 59 | 34.87

#### Top Interview 150 1D DP

Expand DownExpand Up@@ -1254,6 +1259,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0202 |[Happy Number](src/main/python/g0201_0300/s0202_happy_number/Solution0202.py)| Easy | Top_Interview_Questions, Hash_Table, Math, Two_Pointers | 0 | 100.00
| 0149 |[Max Points on a Line](src/main/python/g0101_0200/s0149_max_points_on_a_line/Solution0149.py)| Hard | Top_Interview_Questions, Array, Hash_Table, Math, Geometry | 59 | 34.87

### Binary Search I

Expand All@@ -1277,6 +1283,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.

| <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- --> | <!-- -->
|-|-|-|-|-|-
| 0069 |[Sqrt(x)](src/main/python/g0001_0100/s0069_sqrtx/Solution0069.py)| Easy | Top_Interview_Questions, Math, Binary_Search | 0 | 100.00

#### Day 5

Expand DownExpand Up@@ -1826,6 +1833,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
| 0152 |[Maximum Product Subarray](src/main/python/g0101_0200/s0152_maximum_product_subarray/Solution0152.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Dynamic_Programming_I_Day_6, Level_2_Day_13_Dynamic_Programming, Udemy_Dynamic_Programming, Big_O_Time_O(N)_Space_O(1) | 3 | 98.51
| 0151 |[Reverse Words in a String](src/main/python/g0101_0200/s0151_reverse_words_in_a_string/Solution0151.py)| Medium | String, Two_Pointers, LeetCode_75_Array/String, Udemy_Strings, Top_Interview_150_Array/String | 0 | 100.00
| 0150 |[Evaluate Reverse Polish Notation](src/main/python/g0101_0200/s0150_evaluate_reverse_polish_notation/Solution0150.py)| Medium | Top_Interview_Questions, Array, Math, Stack, Programming_Skills_II_Day_3, Top_Interview_150_Stack | 0 | 100.00
| 0149 |[Max Points on a Line](src/main/python/g0101_0200/s0149_max_points_on_a_line/Solution0149.py)| Hard | Top_Interview_Questions, Array, Hash_Table, Math, Geometry, Algorithm_II_Day_21_Others, Top_Interview_150_Math | 59 | 34.87
| 0148 |[Sort List](src/main/python/g0101_0200/s0148_sort_list/Solution0148.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Sorting, Two_Pointers, Linked_List, Divide_and_Conquer, Merge_Sort, Level_2_Day_4_Linked_List, Top_Interview_150_Divide_and_Conquer, Big_O_Time_O(log(N))_Space_O(log(N)) | 186 | 43.67
| 0146 |[LRU Cache](src/main/python/g0101_0200/s0146_lru_cache/LRUCache.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Hash_Table, Design, Linked_List, Doubly_Linked_List, Udemy_Linked_List, Top_Interview_150_Linked_List, Big_O_Time_O(1)_Space_O(capacity) | 113 | 84.08
| 0142 |[Linked List Cycle II](src/main/python/g0101_0200/s0142_linked_list_cycle_ii/Solution0142.py)| Medium | Top_100_Liked_Questions, Hash_Table, Two_Pointers, Linked_List, Data_Structure_II_Day_10_Linked_List, Level_1_Day_4_Linked_List, Udemy_Linked_List, Big_O_Time_O(N)_Space_O(1) | 38 | 94.15
Expand DownExpand Up@@ -1879,6 +1887,7 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
| 0072 |[Edit Distance](src/main/python/g0001_0100/s0072_edit_distance/Solution0072.py)| Medium | Top_100_Liked_Questions, String, Dynamic_Programming, LeetCode_75_DP/Multidimensional, Algorithm_II_Day_18_Dynamic_Programming, Dynamic_Programming_I_Day_19, Udemy_Dynamic_Programming, Top_Interview_150_Multidimensional_DP, Big_O_Time_O(n^2)_Space_O(n2) | 23 | 98.84
| 0071 |[Simplify Path](src/main/python/g0001_0100/s0071_simplify_path/Solution0071.py)| Medium | String, Stack, Top_Interview_150_Stack | 0 | 100.00
| 0070 |[Climbing Stairs](src/main/python/g0001_0100/s0070_climbing_stairs/Solution0070.py)| Easy | Top_100_Liked_Questions, Top_Interview_Questions, Dynamic_Programming, Math, Memoization, Algorithm_I_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_2, Level_1_Day_10_Dynamic_Programming, Udemy_Dynamic_Programming, Top_Interview_150_1D_DP, Big_O_Time_O(n)_Space_O(n) | 0 | 100.00
| 0069 |[Sqrt(x)](src/main/python/g0001_0100/s0069_sqrtx/Solution0069.py)| Easy | Top_Interview_Questions, Math, Binary_Search, Binary_Search_I_Day_4, Top_Interview_150_Math | 0 | 100.00
| 0068 |[Text Justification](src/main/python/g0001_0100/s0068_text_justification/Solution0068.py)| Hard | Array, String, Simulation, Top_Interview_150_Array/String | 0 | 100.00
| 0067 |[Add Binary](src/main/python/g0001_0100/s0067_add_binary/Solution0067.py)| Easy | String, Math, Bit_Manipulation, Simulation, Programming_Skills_II_Day_5, Top_Interview_150_Bit_Manipulation | 0 | 100.00
| 0066 |[Plus One](src/main/python/g0001_0100/s0066_plus_one/Solution0066.py)| Easy | Top_Interview_Questions, Array, Math, Programming_Skills_II_Day_3, Udemy_Arrays, Top_Interview_150_Math | 0 | 100.00
Expand All@@ -1892,7 +1901,9 @@ Python-based LeetCode algorithm problem solutions, regularly updated.
| 0055 |[Jump Game](src/main/python/g0001_0100/s0055_jump_game/Solution0055.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Greedy, Algorithm_II_Day_12_Dynamic_Programming, Dynamic_Programming_I_Day_4, Udemy_Arrays, Top_Interview_150_Array/String, Big_O_Time_O(n)_Space_O(1) | 12 | 85.38
| 0054 |[Spiral Matrix](src/main/python/g0001_0100/s0054_spiral_matrix/Solution0054.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Matrix, Simulation, Programming_Skills_II_Day_8, Level_2_Day_1_Implementation/Simulation, Udemy_2D_Arrays/Matrix, Top_Interview_150_Matrix | 0 | 100.00
| 0053 |[Maximum Subarray](src/main/python/g0001_0100/s0053_maximum_subarray/Solution0053.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Dynamic_Programming, Divide_and_Conquer, Data_Structure_I_Day_1_Array, Dynamic_Programming_I_Day_5, Udemy_Famous_Algorithm, Top_Interview_150_Kadane's_Algorithm, Big_O_Time_O(n)_Space_O(1) | 50 | 77.23
| 0052 |[N-Queens II](src/main/python/g0001_0100/s0052_n_queens_ii/Solution0052.py)| Hard | Backtracking, Top_Interview_150_Backtracking | 3 | 96.02
| 0051 |[N-Queens](src/main/python/g0001_0100/s0051_n_queens/Solution0051.py)| Hard | Top_100_Liked_Questions, Array, Backtracking, Big_O_Time_O(N!)_Space_O(N) | 6 | 98.12
| 0050 |[Pow(x, n)](src/main/python/g0001_0100/s0050_powx_n/Solution0050.py)| Medium | Top_Interview_Questions, Math, Recursion, Udemy_Integers, Top_Interview_150_Math | 0 | 100.00
| 0049 |[Group Anagrams](src/main/python/g0001_0100/s0049_group_anagrams/Solution0049.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, String, Hash_Table, Sorting, Data_Structure_II_Day_8_String, Programming_Skills_II_Day_11, Udemy_Strings, Top_Interview_150_Hashmap, Big_O_Time_O(n\*k_log_k)_Space_O(n) | 11 | 82.75
| 0048 |[Rotate Image](src/main/python/g0001_0100/s0048_rotate_image/Solution0048.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Math, Matrix, Data_Structure_II_Day_3_Array, Programming_Skills_II_Day_7, Udemy_2D_Arrays/Matrix, Top_Interview_150_Matrix, Big_O_Time_O(n^2)_Space_O(1) | 0 | 100.00
| 0046 |[Permutations](src/main/python/g0001_0100/s0046_permutations/Solution0046.py)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, Array, Backtracking, Algorithm_I_Day_11_Recursion_Backtracking, Level_2_Day_20_Brute_Force/Backtracking, Udemy_Backtracking/Recursion, Top_Interview_150_Backtracking, Big_O_Time_O(n\*n!)_Space_O(n+n!) | 0 | 100.00
Expand Down
19 changes: 19 additions & 0 deletionssrc/main/python/g0001_0100/s0050_powx_n/Solution0050.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
# #Medium #Top_Interview_Questions #Math #Recursion #Udemy_Integers #Top_Interview_150_Math
# #2025_09_20_Time_0_ms_(100.00%)_Space_17.80_MB_(50.14%)

class Solution:
def myPow(self, x: float, n: int) -> float:
nn = n
res = 1.0
if n < 0:
nn = -nn
while nn > 0:
if nn % 2 == 1:
nn -= 1
res *= x
else:
x *= x
nn //= 2
if n < 0:
return 1.0 / res
return res
12 changes: 12 additions & 0 deletionssrc/main/python/g0001_0100/s0050_powx_n/Solution0050_test.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
import unittest
from Solution0050 import Solution

class SolutionTest(unittest.TestCase):
def test_myPow(self):
self.assertEqual(Solution().myPow(2.00000, 10), 1024.00000)

def test_myPow2(self):
self.assertEqual(Solution().myPow(2.10000, 3), 9.261000000000001)

def test_myPow3(self):
self.assertEqual(Solution().myPow(2.00000, -2), 0.25000)
31 changes: 31 additions & 0 deletionssrc/main/python/g0001_0100/s0050_powx_n/readme.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
50\. Pow(x, n)

Medium

Implement [pow(x, n)](http://www.cplusplus.com/reference/valarray/pow/), which calculates `x` raised to the power `n` (i.e., <code>x<sup>n</sup></code>).

**Example 1:**

**Input:** x = 2.00000, n = 10

**Output:** 1024.00000

**Example 2:**

**Input:** x = 2.10000, n = 3

**Output:** 9.26100

**Example 3:**

**Input:** x = 2.00000, n = -2

**Output:** 0.25000

**Explanation:** 2<sup>\-2</sup> = 1/2<sup>2</sup> = 1/4 = 0.25

**Constraints:**

* `-100.0 < x < 100.0`
* <code>-2<sup>31</sup> <= n <= 2<sup>31</sup>-1</code>
* <code>-10<sup>4</sup> <= x<sup>n</sup> <= 10<sup>4</sup></code>
21 changes: 21 additions & 0 deletionssrc/main/python/g0001_0100/s0052_n_queens_ii/Solution0052.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
# #Hard #Backtracking #Top_Interview_150_Backtracking
# #2025_09_20_Time_3_ms_(96.02%)_Space_17.86_MB_(43.80%)

class Solution:
def totalNQueens(self, n: int) -> int:
row = [False] * n
col = [False] * n
diagonal = [False] * (2 * n - 1)
antiDiagonal = [False] * (2 * n - 1)
return self._totalNQueens(n, 0, row, col, diagonal, antiDiagonal)

def _totalNQueens(self, n, r, row, col, diagonal, antiDiagonal):
if r == n:
return 1
count = 0
for c in range(n):
if not row[r] and not col[c] and not diagonal[r + c] and not antiDiagonal[r - c + n - 1]:
row[r] = col[c] = diagonal[r + c] = antiDiagonal[r - c + n - 1] = True
count += self._totalNQueens(n, r + 1, row, col, diagonal, antiDiagonal)
row[r] = col[c] = diagonal[r + c] = antiDiagonal[r - c + n - 1] = False
return count
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
import unittest
from Solution0052 import Solution

class SolutionTest(unittest.TestCase):
def test_totalNQueens(self):
self.assertEqual(Solution().totalNQueens(4), 2)

def test_totalNQueens2(self):
self.assertEqual(Solution().totalNQueens(1), 1)
27 changes: 27 additions & 0 deletionssrc/main/python/g0001_0100/s0052_n_queens_ii/readme.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
52\. N-Queens II

Hard

The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.

Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/11/13/queens.jpg)

**Input:** n = 4

**Output:** 2

**Explanation:** There are two distinct solutions to the 4-queens puzzle as shown.

**Example 2:**

**Input:** n = 1

**Output:** 1

**Constraints:**

* `1 <= n <= 9`
17 changes: 17 additions & 0 deletionssrc/main/python/g0001_0100/s0069_sqrtx/Solution0069.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
# #Easy #Top_Interview_Questions #Math #Binary_Search #Binary_Search_I_Day_4
# #Top_Interview_150_Math #2025_09_20_Time_0_ms_(100.00%)_Space_17.75_MB_(51.52%)

class Solution:
def mySqrt(self, x: int) -> int:
if x == 0:
return 0
left, right = 1, x
while left <= right:
mid = (left + right) // 2
if mid * mid == x:
return mid
elif mid * mid < x:
left = mid + 1
else:
right = mid - 1
return right
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
import unittest
from Solution0069 import Solution

class SolutionTest(unittest.TestCase):
def test_mySqrt(self):
self.assertEqual(Solution().mySqrt(4), 2)

def test_mySqrt2(self):
self.assertEqual(Solution().mySqrt(8), 2)
27 changes: 27 additions & 0 deletionssrc/main/python/g0001_0100/s0069_sqrtx/readme.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
69\. Sqrt(x)

Easy

Given a non-negative integer `x`, compute and return _the square root of_ `x`.

Since the return type is an integer, the decimal digits are **truncated**, and only **the integer part** of the result is returned.

**Note:** You are not allowed to use any built-in exponent function or operator, such as `pow(x, 0.5)` or `x ** 0.5`.

**Example 1:**

**Input:** x = 4

**Output:** 2

**Example 2:**

**Input:** x = 8

**Output:** 2

**Explanation:** The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.

**Constraints:**

* <code>0 <= x <= 2<sup>31</sup> - 1</code>
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
# #Hard #Top_Interview_Questions #Array #Hash_Table #Math #Geometry #Algorithm_II_Day_21_Others
# #Top_Interview_150_Math #2025_09_20_Time_59_ms_(34.87%)_Space_17.88_MB_(73.72%)

from typing import List

class Solution:
def maxPoints(self, points: List[List[int]]) -> int:
result = min(len(points), 2)
n = len(points)
for i in range(n):
hashmap = {}
for j in range(n):
x1, y1 = points[i]
x2, y2 = points[j]
if x2 - x1 == 0:
m = float("inf")
else:
m = (y2 - y1) / (x2 - x1)
if m in hashmap:
hashmap[m].add(i)
hashmap[m].add(j)
else:
hashmap[m] = set([i, j])
result = max(len(hashmap[m]), result)

return result
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
import unittest
from Solution0149 import Solution

class SolutionTest(unittest.TestCase):
def test_maxPoints(self):
input_points = [[1, 1], [2, 2], [3, 3]]
self.assertEqual(Solution().maxPoints(input_points), 3)

def test_maxPoints2(self):
input_points = [[1, 1], [3, 2], [5, 3], [4, 1], [2, 3], [1, 4]]
self.assertEqual(Solution().maxPoints(input_points), 4)
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
149\. Max Points on a Line

Hard

Given an array of `points` where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>]</code> represents a point on the **X-Y** plane, return _the maximum number of points that lie on the same straight line_.

**Example 1:**

![](https://assets.leetcode.com/uploads/2021/02/25/plane1.jpg)

**Input:** points = [[1,1],[2,2],[3,3]]

**Output:** 3

**Example 2:**

![](https://assets.leetcode.com/uploads/2021/02/25/plane2.jpg)

**Input:** points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]

**Output:** 4

**Constraints:**

* `1 <= points.length <= 300`
* `points[i].length == 2`
* <code>-10<sup>4</sup> <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>4</sup></code>
* All the `points` are **unique**.

[8]ページ先頭

©2009-2025 Movatter.jp