feat: populate note files with problem descriptions and code stubs

Add populate-notes.mjs that fetches problem descriptions and
Python/C++ code stubs from LeetCode's GraphQL API. Populated
all 197 NeetCode 150 note files with:
- Problem description (examples, constraints)
- Python code stub (function signature)
- C++ code stub (function signature + includes)

API responses cached in leetcode/.cache/leetcode/ for instant re-runs.
This commit is contained in:
2026-06-01 17:22:07 +08:00
parent e798e449bd
commit 1dec88aaf2
198 changed files with 10459 additions and 534 deletions
@@ -1,18 +1,59 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0007. Reverse Integer :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0007. Reverse Integer][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0007. Reverse Integer][0007. Reverse Integer]]
:END:
Given a signed 32-bit integer ~x~, return ~x~/ with its digits reversed/. If reversing ~x~ causes the value to go outside the signed 32-bit integer range ~[-2^{31}, 2^{31} - 1]~, then return ~0~.
*Assume the environment does not allow you to store 64-bit integers (signed or unsigned).*
*Example 1:*
#+begin_src
Input: x = 123
Output: 321
#+end_src
*Example 2:*
#+begin_src
Input: x = -123
Output: -321
#+end_src
*Example 3:*
#+begin_src
Input: x = 120
Output: 21
#+end_src
*Constraints:*
- ~-2^{31} <= x <= 2^{31} - 1~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def reverse(self, x: int) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int reverse(int x) {
}
};
#+end_src
@@ -1,18 +1,54 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0136. Single Number :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0136. Single Number][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0136. Single Number][0136. Single Number]]
:END:
Given a *non-empty* array of integers ~nums~, every element appears /twice/ except for one. Find that single one.
You must implement a solution with a linear runtime complexity and use only constant extra space.
*Example 1:*
*Input:* nums = [2,2,1]
*Output:* 1
*Example 2:*
*Input:* nums = [4,1,2,1,2]
*Output:* 4
*Example 3:*
*Input:* nums = [1]
*Output:* 1
*Constraints:*
- ~1 <= nums.length <= 3 * 10^{4}~
- ~-3 * 10^{4} <= nums[i] <= 3 * 10^{4}~
- Each element in the array appears twice except for one element which appears only once.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def singleNumber(self, nums: List[int]) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int singleNumber(vector<int>& nums) {
}
};
#+end_src
@@ -1,18 +1,68 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0190. Reverse Bits :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0190. Reverse Bits][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0190. Reverse Bits][0190. Reverse Bits]]
:END:
Reverse bits of a given 32 bits signed integer.
*Example 1:*
*Input:* n = 43261596
*Output:* 964176192
*Explanation:*
Integer
Binary
43261596
00000010100101000001111010011100
964176192
00111001011110000010100101000000
*Example 2:*
*Input:* n = 2147483644
*Output:* 1073741822
*Explanation:*
Integer
Binary
2147483644
01111111111111111111111111111100
1073741822
00111111111111111111111111111110
*Constraints:*
- ~0 <= n <= 2^{31} - 2~
- ~n~ is even.
*Follow up:* If this function is called many times, how would you optimize it?
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def reverseBits(self, n: int) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int reverseBits(int n) {
}
};
#+end_src
@@ -1,18 +1,62 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0191. Number of 1 Bits :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0191. Number of 1 Bits][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0191. Number of 1 Bits][0191. Number of 1 Bits]]
:END:
Given a positive integer ~n~, write a function that returns the number of set bits in its binary representation (also known as the Hamming weight).
*Example 1:*
*Input:* n = 11
*Output:* 3
*Explanation:*
The input binary string *1011* has a total of three set bits.
*Example 2:*
*Input:* n = 128
*Output:* 1
*Explanation:*
The input binary string *10000000* has a total of one set bit.
*Example 3:*
*Input:* n = 2147483645
*Output:* 30
*Explanation:*
The input binary string *1111111111111111111111111111101* has a total of thirty set bits.
*Constraints:*
- ~1 <= n <= 2^{31} - 1~
*Follow up:* If this function is called many times, how would you optimize it?
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def hammingWeight(self, n: int) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int hammingWeight(int n) {
}
};
#+end_src
@@ -1,18 +1,63 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0231. Power of Two :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0231. Power of Two][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0231. Power of Two][0231. Power of Two]]
:END:
Given an integer ~n~, return /~true~ if it is a power of two. Otherwise, return ~false~/.
An integer ~n~ is a power of two, if there exists an integer ~x~ such that ~n == 2^{x}~.
*Example 1:*
#+begin_src
Input: n = 1
Output: true
Explanation: 20 = 1
#+end_src
*Example 2:*
#+begin_src
Input: n = 16
Output: true
Explanation: 24 = 16
#+end_src
*Example 3:*
#+begin_src
Input: n = 3
Output: false
#+end_src
*Constraints:*
- ~-2^{31} <= n <= 2^{31} - 1~
*Follow up:* Could you solve it without loops/recursion?
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
bool isPowerOfTwo(int n) {
}
};
#+end_src
@@ -1,18 +1,64 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0260. Single Number III :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0260. Single Number III][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0260. Single Number III][0260. Single Number III]]
:END:
Given an integer array ~nums~, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in *any order*.
You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.
*Example 1:*
#+begin_src
Input: nums = [1,2,1,3,2,5]
Output: [3,5]
Explanation: [5, 3] is also a valid answer.
#+end_src
*Example 2:*
#+begin_src
Input: nums = [-1,0]
Output: [-1,0]
#+end_src
*Example 3:*
#+begin_src
Input: nums = [0,1]
Output: [1,0]
#+end_src
*Constraints:*
- ~2 <= nums.length <= 3 * 10^{4}~
- ~-2^{31} <= nums[i] <= 2^{31} - 1~
- Each integer in ~nums~ will appear twice, only two integers will appear once.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def singleNumber(self, nums: List[int]) -> List[int]:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
vector<int> singleNumber(vector<int>& nums) {
}
};
#+end_src
@@ -1,18 +1,68 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0268. Missing Number :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0268. Missing Number][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0268. Missing Number][0268. Missing Number]]
:END:
Given an array ~nums~ containing ~n~ distinct numbers in the range ~[0, n]~, return /the only number in the range that is missing from the array./
*Example 1:*
*Input:* nums = [3,0,1]
*Output:* 2
*Explanation:*
~n = 3~ since there are 3 numbers, so all numbers are in the range ~[0,3]~. 2 is the missing number in the range since it does not appear in ~nums~.
*Example 2:*
*Input:* nums = [0,1]
*Output:* 2
*Explanation:*
~n = 2~ since there are 2 numbers, so all numbers are in the range ~[0,2]~. 2 is the missing number in the range since it does not appear in ~nums~.
*Example 3:*
*Input:* nums = [9,6,4,2,3,5,7,0,1]
*Output:* 8
*Explanation:*
~n = 9~ since there are 9 numbers, so all numbers are in the range ~[0,9]~. 8 is the missing number in the range since it does not appear in ~nums~.
*Constraints:*
- ~n == nums.length~
- ~1 <= n <= 10^{4}~
- ~0 <= nums[i] <= n~
- All the numbers of ~nums~ are *unique*.
*Follow up:* Could you implement a solution using only ~O(1)~ extra space complexity and ~O(n)~ runtime complexity?
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def missingNumber(self, nums: List[int]) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int missingNumber(vector<int>& nums) {
}
};
#+end_src
@@ -1,18 +1,65 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0338. Counting Bits :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0338. Counting Bits][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0338. Counting Bits][0338. Counting Bits]]
:END:
Given an integer ~n~, return /an array /~ans~/ of length /~n + 1~/ such that for each /~i~/ /(~0 <= i <= n~)/, /~ans[i]~/ is the *number of */~1~/*'s* in the binary representation of /~i~.
*Example 1:*
#+begin_src
Input: n = 2
Output: [0,1,1]
Explanation:
0 --> 0
1 --> 1
2 --> 10
#+end_src
*Example 2:*
#+begin_src
Input: n = 5
Output: [0,1,1,2,1,2]
Explanation:
0 --> 0
1 --> 1
2 --> 10
3 --> 11
4 --> 100
5 --> 101
#+end_src
*Constraints:*
- ~0 <= n <= 10^{5}~
*Follow up:*
- It is very easy to come up with a solution with a runtime of ~O(n log n)~. Can you do it in linear time ~O(n)~ and possibly in a single pass?
- Can you do it without using any built-in function (i.e., like ~__builtin_popcount~ in C++)?
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def countBits(self, n: int) -> List[int]:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
vector<int> countBits(int n) {
}
};
#+end_src
@@ -1,18 +1,46 @@
#+PROPERTY: STUDY_DECK_02
* TODO 0371. Sum of Two Integers :medium:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*0371. Sum of Two Integers][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*0371. Sum of Two Integers][0371. Sum of Two Integers]]
:END:
Given two integers ~a~ and ~b~, return /the sum of the two integers without using the operators/ ~+~ /and/ ~-~.
*Example 1:*
#+begin_src
Input: a = 1, b = 2
Output: 3
#+end_src
*Example 2:*
#+begin_src
Input: a = 2, b = 3
Output: 5
#+end_src
*Constraints:*
- ~-1000 <= a, b <= 1000~
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def getSum(self, a: int, b: int) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int getSum(int a, int b) {
}
};
#+end_src
@@ -1,18 +1,64 @@
#+PROPERTY: STUDY_DECK_02
* TODO 2220. Minimum Bit Flips to Convert Number :easy:
:PROPERTIES:
:NEETCODE: [[file:../../roadmap.org::*2220. Minimum Bit Flips to Convert Number][Roadmap]]
:NEETCODE: [[file:../../roadmap.org::*2220. Minimum Bit Flips to Convert Number][2220. Minimum Bit Flips to Convert Number]]
:END:
A *bit flip* of a number ~x~ is choosing a bit in the binary representation of ~x~ and *flipping* it from either ~0~ to ~1~ or ~1~ to ~0~.
- For example, for ~x = 7~, the binary representation is ~111~ and we may choose any bit (including any leading zeros not shown) and flip it. We can flip the first bit from the right to get ~110~, flip the second bit from the right to get ~101~, flip the fifth bit from the right (a leading zero) to get ~10111~, etc.
Given two integers ~start~ and ~goal~, return/ the *minimum* number of *bit flips* to convert /~start~/ to /~goal~.
*Example 1:*
#+begin_src
Input: start = 10, goal = 7
Output: 3
Explanation: The binary representation of 10 and 7 are 1010 and 0111 respectively. We can convert 10 to 7 in 3 steps:
- Flip the first bit from the right: 1010 -> 1011.
- Flip the third bit from the right: 1011 -> 1111.
- Flip the fourth bit from the right: 1111 -> 0111.
It can be shown we cannot convert 10 to 7 in less than 3 steps. Hence, we return 3.
#+end_src
*Example 2:*
#+begin_src
Input: start = 3, goal = 4
Output: 3
Explanation: The binary representation of 3 and 4 are 011 and 100 respectively. We can convert 3 to 4 in 3 steps:
- Flip the first bit from the right: 011 -> 010.
- Flip the second bit from the right: 010 -> 000.
- Flip the third bit from the right: 000 -> 100.
It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return 3.
#+end_src
*Constraints:*
- ~0 <= start, goal <= 10^{9}~
*Note:* This question is the same as 461: Hamming Distance.
** TODO Approach
Write your approach here.
** TODO Python
#+begin_src python
class Solution:
def minBitFlips(self, start: int, goal: int) -> int:
#+end_src
** TODO C++
#+begin_src cpp
class Solution {
public:
int minBitFlips(int start, int goal) {
}
};
#+end_src