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 199-224#61

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
javadev merged 3 commits intoLeetCode-in-Python:mainfromThanhNIT:tasks-199-209
Sep 15, 2025
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
30 changes: 30 additions & 0 deletionsREADME.md
View file
Open in desktop

Large diffs are not rendered by default.

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
# #Medium #Top_100_Liked_Questions #Depth_First_Search #Breadth_First_Search #Tree #Binary_Tree
# #LeetCode_75_Binary_Tree/BFS #Data_Structure_II_Day_16_Tree #Level_2_Day_15_Tree
# #Top_Interview_150_Binary_Tree_BFS #2025_09_14_Time_0_ms_(100.00%)_Space_17.76_MB_(60.72%)

from typing import List, Optional

class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right


class Solution:
def rightSideView(self, root: Optional[TreeNode]) -> List[int]:
result = []
self._recurse(root, 0, result)
return result

def _recurse(self, node: Optional[TreeNode], level: int, result: List[int]):
if node is not None:
if len(result) < level + 1:
result.append(node.val)
self._recurse(node.right, level + 1, result)
self._recurse(node.left, level + 1, result)

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
import unittest
from Solution0199 import Solution, TreeNode

class SolutionTest(unittest.TestCase):
def test_rightSideView(self):
left = TreeNode(2)
left.right = TreeNode(5)
right = TreeNode(3)
right.right = TreeNode(4)
root = TreeNode(1, left, right)
self.assertEqual(Solution().rightSideView(root), [1, 3, 4])

def test_rightSideView2(self):
root = TreeNode(1)
root.right = TreeNode(3)
self.assertEqual(Solution().rightSideView(root), [1, 3])

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
199\. Binary Tree Right Side View

Medium

Given the `root` of a binary tree, imagine yourself standing on the **right side** of it, return _the values of the nodes you can see ordered from top to bottom_.

**Example 1:**

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

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

**Output:** [1,3,4]

**Example 2:**

**Input:** root = [1,null,3]

**Output:** [1,3]

**Example 3:**

**Input:** root = []

**Output:** []

**Constraints:**

* The number of nodes in the tree is in the range `[0, 100]`.
* `-100 <= Node.val <= 100`
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
# #Medium #Bit_Manipulation #Algorithm_II_Day_19_Bit_Manipulation
# #Top_Interview_150_Bit_Manipulation #2025_09_14_Time_1_ms_(82.83%)_Space_18.01_MB_(19.55%)

class Solution:
def __init__(self):
# Precomputed masks for different bit positions
self.masks = [
0,
0x80000000,
0xC0000000,
0xE0000000,
0xF0000000,
0xF8000000,
0xFC000000,
0xFE000000,
0xFF000000,
0xFF800000,
0xFFC00000,
0xFFE00000,
0xFFF00000,
0xFFF80000,
0xFFFC0000,
0xFFFE0000,
0xFFFF0000,
0xFFFF8000,
0xFFFFC000,
0xFFFFE000,
0xFFFFF000,
0xFFFFF800,
0xFFFFFC00,
0xFFFFFE00,
0xFFFFFF00,
0xFFFFFF80,
0xFFFFFFC0,
0xFFFFFFE0,
0xFFFFFFF0,
0xFFFFFFF8,
0xFFFFFFFC,
0xFFFFFFFE
]

def rangeBitwiseAnd(self, left: int, right: int) -> int:
if left == right:
return left
return right & self.masks[self._numberOfLeadingZeros(left ^ right)]

def _numberOfLeadingZeros(self, n: int) -> int:
"""Count leading zeros in 32-bit integer"""
if n == 0:
return 32
count = 0
if n < 0:
return 0
while n > 0:
n = n >> 1
count += 1
return 32 - count

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
import unittest
from Solution0201 import Solution

class SolutionTest(unittest.TestCase):
def test_rangeBitwiseAnd(self):
self.assertEqual(Solution().rangeBitwiseAnd(5, 7), 4)

def test_rangeBitwiseAnd2(self):
self.assertEqual(Solution().rangeBitwiseAnd(0, 0), 0)

def test_rangeBitwiseAnd3(self):
self.assertEqual(Solution().rangeBitwiseAnd(1, 2147483647), 0)

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
201\. Bitwise AND of Numbers Range

Medium

Given two integers `left` and `right` that represent the range `[left, right]`, return _the bitwise AND of all numbers in this range, inclusive_.

**Example 1:**

**Input:** left = 5, right = 7

**Output:** 4

**Example 2:**

**Input:** left = 0, right = 0

**Output:** 0

**Example 3:**

**Input:** left = 1, right = 2147483647

**Output:** 0

**Constraints:**

* <code>0 <= left <= right <= 2<sup>31</sup> - 1</code>
22 changes: 22 additions & 0 deletionssrc/main/python/g0201_0300/s0202_happy_number/Solution0202.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
# #Easy #Top_Interview_Questions #Hash_Table #Math #Two_Pointers #Algorithm_II_Day_21_Others
# #Programming_Skills_I_Day_4_Loop #Level_2_Day_1_Implementation/Simulation
# #Top_Interview_150_Hashmap #2025_09_15_Time_0_ms_(100.00%)_Space_17.57_MB_(97.85%)

class Solution:
def isHappy(self, n: int) -> bool:
a = n
rem = 0
sum_val = 0
if a == 1 or a == 7:
return True
elif 1 < a < 10:
return False
else:
while a != 0:
rem = a % 10
sum_val += rem * rem
a //= 10
if sum_val != 1:
return self.isHappy(sum_val)
else:
return True
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
import unittest
from Solution0202 import Solution

class SolutionTest(unittest.TestCase):
def test_isHappy(self):
self.assertTrue(Solution().isHappy(19))

def test_isHappy2(self):
self.assertFalse(Solution().isHappy(2))

39 changes: 39 additions & 0 deletionssrc/main/python/g0201_0300/s0202_happy_number/readme.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
202\. Happy Number

Easy

Write an algorithm to determine if a number `n` is happy.

A **happy number** is a number defined by the following process:

* Starting with any positive integer, replace the number by the sum of the squares of its digits.
* Repeat the process until the number equals 1 (where it will stay), or it **loops endlessly in a cycle** which does not include 1.
* Those numbers for which this process **ends in 1** are happy.

Return `true` _if_ `n` _is a happy number, and_ `false` _if not_.

**Example 1:**

**Input:** n = 19

**Output:** true

**Explanation:**

1<sup>2</sup> + 9<sup>2</sup> = 82

8<sup>2</sup> + 2<sup>2</sup> = 68

6<sup>2</sup> + 8<sup>2</sup> = 100

1<sup>2</sup> + 0<sup>2</sup> + 0<sup>2</sup> = 1

**Example 2:**

**Input:** n = 2

**Output:** false

**Constraints:**

* <code>1 <= n <= 2<sup>31</sup> - 1</code>
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
# #Easy #String #Hash_Table #Level_1_Day_2_String #Top_Interview_150_Hashmap
# #2025_09_14_Time_3_ms_(95.32%)_Space_18.38_MB_(7.18%)

from typing import List

class Solution:
def isIsomorphic(self, s: str, t: str) -> bool:
map_array = [0] * 128
str_chars = list(s)
tar_chars = list(t)
n = len(str_chars)
for i in range(n):
if map_array[ord(tar_chars[i])] == 0:
if self._search(map_array, ord(str_chars[i]), ord(tar_chars[i])) != -1:
return False
map_array[ord(tar_chars[i])] = ord(str_chars[i])
else:
if map_array[ord(tar_chars[i])] != ord(str_chars[i]):
return False
return True

def _search(self, map_array: List[int], target: int, skip: int) -> int:
for i in range(128):
if i == skip:
continue
if map_array[i] != 0 and map_array[i] == target:
return i
return -1
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
import unittest
from Solution0205 import Solution

class SolutionTest(unittest.TestCase):
def test_isIsomorphic(self):
self.assertTrue(Solution().isIsomorphic("egg", "add"))

def test_isIsomorphic2(self):
self.assertFalse(Solution().isIsomorphic("foo", "bar"))

def test_isIsomorphic3(self):
self.assertTrue(Solution().isIsomorphic("paper", "title"))

33 changes: 33 additions & 0 deletionssrc/main/python/g0201_0300/s0205_isomorphic_strings/readme.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
205\. Isomorphic Strings

Easy

Given two strings `s` and `t`, _determine if they are isomorphic_.

Two strings `s` and `t` are isomorphic if the characters in `s` can be replaced to get `t`.

All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.

**Example 1:**

**Input:** s = "egg", t = "add"

**Output:** true

**Example 2:**

**Input:** s = "foo", t = "bar"

**Output:** false

**Example 3:**

**Input:** s = "paper", t = "title"

**Output:** true

**Constraints:**

* <code>1 <= s.length <= 5 * 10<sup>4</sup></code>
* `t.length == s.length`
* `s` and `t` consist of any valid ascii character.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
# #Medium #Array #Binary_Search #Prefix_Sum #Sliding_Window #Algorithm_II_Day_5_Sliding_Window
# #Binary_Search_II_Day_1 #Top_Interview_150_Sliding_Window
# #2025_09_14_Time_7_ms_(91.69%)_Space_28.40_MB_(10.62%)

from typing import List

class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
min_res=float('inf')
n=len(nums)
l=0
r=0
curr=0
while r<n:

curr+=nums[r]
while curr>=target:
curr-=nums[l]
min_res=min(min_res,r-l+1)
l+=1
r+=1


return min_res if min_res!=float('inf') else 0
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
import unittest
from Solution0209 import Solution

class SolutionTest(unittest.TestCase):
def test_minSubArrayLen(self):
self.assertEqual(Solution().minSubArrayLen(7, [2, 3, 1, 2, 4, 3]), 2)

def test_minSubArrayLen2(self):
self.assertEqual(Solution().minSubArrayLen(4, [1, 4, 4]), 1)

def test_minSubArrayLen3(self):
self.assertEqual(Solution().minSubArrayLen(11, [1, 1, 1, 1, 1, 1, 1, 1]), 0)

Loading

[8]ページ先頭

©2009-2025 Movatter.jp