Skip to content

Commit a5d5ad0

Browse files
committed
added more annotations and trailing newline
1 parent 78c3a0a commit a5d5ad0

File tree

91 files changed

+130
-129
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+130
-129
lines changed

0009-palindrome-number.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class Solution {
1212
bool isPalindrome(int x) {
1313
if (x < 0) return false;
1414
// Reverse digits
15-
long y = 0;
16-
int x2 = x;
15+
long y = 0; // we have to use a long because some inputs
16+
int x2 = x; // exceed 2^31-1 when reversed
1717
while (x2 != 0) {
1818
y *= 10;
1919
y += x2 % 10;

0014-longest-common-prefix.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ char* longestCommonPrefix(char** strs, int strsSize) {
2222
for (unsigned char i = 0; i < len_shortest; i++) {
2323
for (char** p = strs; p != strsEnd; ++p ) {
2424
char* str = *p;
25-
// this means that the prefix ends here
25+
// not equal, prefix ends here
2626
if (str[i] != strs[0][i]) return prefix;
2727
}
2828
prefix[i] = strs[0][i];

0014-longest-common-prefix.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Solution {
2020
}
2121
for (unsigned char i = 0; i < len_shortest; i++) {
2222
for ( const auto& str : strs ) {
23-
// this means that the prefix ends here
23+
// not equal, prefix ends here
2424
if ( str[i] != strs.front()[i] ) return prefix;
2525
}
2626
prefix += strs.front()[i];

0020-valid-parentheses.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ class Solution {
1515
for (const char& c : s) {
1616
if (c == '(' || c == '[' || c == '{') { // opening parentheses
1717
stack.push_back(c);
18-
} else if (stack.empty() ||
18+
} else if (stack.empty() || // back() when deque is empty is UB
1919
stack.back() != (c == ')' ? '(' : // matching closing
2020
c == ']' ? '[' : // parentheses with
2121
c == '}' ? '{' : // open parentheses
2222
c)) {
2323
return false;
24-
} else {
24+
} else { // valid pair
2525
stack.pop_back();
2626
}
2727
}
28-
return stack.empty();
28+
return stack.empty();
2929
}
3030
};

0021-merge-two-sorted-lists.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
3434
}
3535
tail->next = list1 == NULL ? list2 : list1;
3636
return head;
37-
}
37+
}

0021-merge-two-sorted-lists.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ class Solution {
2323
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
2424
if (list1 == nullptr) return list2;
2525
if (list2 == nullptr) return list1;
26+
// start with whichever value is greater
2627
ListNode* head = list1->val > list2->val ? list2 : list1;
28+
// advance that list to the next element
2729
if (head == list1) list1 = list1->next;
2830
else list2 = list2->next;
2931
ListNode* tail = head;
3032
while (list1 != nullptr && list2 != nullptr) {
33+
// find smaller value and advance list
3134
if (list1->val > list2->val) {
3235
tail->next = list2;
3336
list2 = list2->next;
@@ -37,7 +40,8 @@ class Solution {
3740
}
3841
tail = tail->next;
3942
}
43+
// everything else is tacked on to the end
4044
tail->next = list1 == nullptr ? list2 : list1;
4145
return head;
4246
}
43-
};
47+
};

0028-find-the-index-of-the-first-occurrence-in-a-string.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ class Solution {
1717
}
1818
return -1;
1919
}
20-
};
20+
};

0058-length-of-last-word.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99

1010
class Solution:
1111
def lengthOfLastWord(self, s: str) -> int:
12-
return len(s.strip().split()[-1]) # split into words and get last element in list
12+
return len(s.strip().split()[-1]) # split into words and get last element in list

0083-remove-duplicates-from-sorted-list.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ class Solution {
3232
}
3333
return head;
3434
}
35-
};
35+
};

0125-valid-palindrome.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99

1010
class Solution:
1111
def isPalindrome(self, s: str) -> bool:
12-
# convert the input string to lowercase, then remove all non-alphanumeric
13-
s = ''.join(c for c in s.lower() if c.isalnum()) # characters
14-
return s == s[::-1] # whether the resulting string is equal to itself reversed
12+
# convert the input string to lowercase, then remove all non-alphanumeric chars
13+
s = ''.join(c for c in s.lower() if c.isalnum())
14+
return s == s[::-1] # whether the resulting string is equal to itself reversed

0141-linked-list-cycle.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ class Solution {
2828
}
2929
return false;
3030
}
31-
};
31+
};

0144-binary-tree-preorder-traversal.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ def t(root):
2323
t(root.left)
2424
t(root.right)
2525
t(root)
26-
return b
26+
return b

0168-excel-sheet-column-title.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ class Solution {
1818
}
1919
return col;
2020
}
21-
};
21+
};

0171-excel-sheet-column-number.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Submitted: November 27, 2024
66
Runtime: 0 ms (beats 100.00%)
77
Memory: 8.77 MB (beats 31.86%)
88
9-
See 0171-excel-sheet-column-number-alternative.cpp for an alternative solution.
9+
See `0171-excel-sheet-column-number-alternative.cpp` for an alternative solution.
1010
*/
1111

1212
class Solution {
@@ -15,8 +15,8 @@ class Solution {
1515
int res = 0;
1616
for ( const char& c : columnTitle ) {
1717
res *= 26;
18-
res += (int) c - 64;
18+
res += (int) c - 64; // 'A' == 65
1919
}
2020
return res;
2121
}
22-
};
22+
};

0175-combine-two-tables.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ Runtime: 399 ms (beats 83.54%)
99
# Write your MySQL query statement below
1010
SELECT firstName, lastName, city, state
1111
FROM Person
12-
LEFT JOIN Address -- we need to use LEFT JOIN so that `null` is produced if the
12+
LEFT JOIN Address -- LEFT JOIN means that `null` is produced if the
1313
USING (personId); -- address of a Person is not found in Address

0190-reverse-bits.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ class Solution {
1717
}
1818
return result;
1919
}
20-
};
20+
};

0191-number-of-1-bits.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ class Solution {
1818
}
1919
return res;
2020
}
21-
};
21+
};

0195-tenth-line.sh

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
: <<'###'
2-
195. Tenth Line
1+
# 195. Tenth Line
32

4-
Submitted: October 31, 2024
3+
# Submitted: October 31, 2024
54

6-
Runtime: 19 ms (beats 92.92%)
7-
Memory: 3.85 MB (beats 41.81%)
8-
###
5+
# Runtime: 19 ms (beats 92.92%)
6+
# Memory: 3.85 MB (beats 41.81%)
97

108
# Read from the file file.txt and output the tenth line to stdout.
11-
head -n 10 file.txt | tail -n +10
9+
head -n 10 file.txt | tail -n +10

0202-happy-number.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Solution {
1212
bool isHappy(int n) {
1313
int fast = sumDigits(sumDigits(n));
1414
int slow = sumDigits(n);
15-
while (fast != slow) { // Floyd's cycle detection algorithim
15+
while (fast != slow) { // cycle detection algorithim
1616
fast = sumDigits(sumDigits(fast));
1717
slow = sumDigits(slow);
1818
}

0206-reverse-linked-list.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Solution {
2828
head->next = head_next->next;
2929
head_next->next = new_head;
3030
new_head = head_next;
31-
} // remove node after old "head" and add to front
31+
} // remove node after original head and add to front
3232
return new_head;
3333
}
3434
};

0258-add-digits.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ class Solution {
2020
}
2121
return num;
2222
}
23-
};
23+
};

0278-first-bad-version.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/*
22
278. First Bad Version
33
4+
Submitted: November 5, 2024
5+
46
Runtime: 1301 ms (beats 5.33%)
57
Memory: 7.58 MB (beats 62.02%)
68
*/
@@ -13,7 +15,8 @@ Memory: 7.58 MB (beats 62.02%)
1315
class Solution {
1416
public:
1517
int firstBadVersion(int n) {
16-
while(isBadVersion(n)) n--;
17-
return n + 1;
18+
19+
while (isBadVersion(n)) n--; // go back by one until last good ver
20+
return n + 1; // ver after last good ver is first bad one
1821
}
19-
};
22+
};

0367-valid-perfect-square.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ class Solution {
99
public:
1010
bool isPerfectSquare(int num) {
1111
if (num == 1) return true;
12-
int max = num < 46341 ? num : 46341; // 46341^2 is over 2^31-1,
13-
for (int i = 0; i < max; ++i) { // the max value of a 32-bit
14-
if (i * i == num) return true; // unsigned integer
12+
int max = num < 46341 ? num : 46341; // 46341^2 > 2^31-1 (INT_MAX)
13+
for (int i = 0; i < max; ++i) {
14+
if (i * i == num) return true;
1515
}
1616
return false;
1717
}
18-
};
18+
};

0476-number-compliment.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class Solution {
1515
if (n == 0) return 1;
1616
int digits = 0;
1717
while (n != 0) {
18-
n >>= 1; // digits has the same number of `1` bits as
19-
digits <<= 1; // the number of digits total in `n` when you
20-
digits++; // remove leading zeroes
18+
n >>= 1; // number of 1 bits in digits is the
19+
digits <<= 1; // total number of digits in n without leading zeroes
20+
digits++;
2121
}
2222
return d ^ digits; // 1 ^ 1 = 0, 0 ^ 1 = 1
2323
}

0482-license-key-formatting.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ class Solution {
1212
string licenseKeyFormatting(string s, int k) {
1313
unsigned short i = 0;
1414
string result;
15-
// we go through the string backwards so that the first group can
16-
// contain less than `k` digits. it's easier to make the last group
17-
// different than the first group different.
15+
// go through the string backwards so that the first group can
16+
// contain less than `k` digits; easier to make the last group
17+
// different than the first group different
1818
for (auto it = s.crbegin(); it != s.crend(); ++it) {
1919
const char& c = *it;
2020
if (c == '-') { // ignore hyphens

0509-fibonacci-number.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ class Solution {
1212
int fib(int n) {
1313
return n < 2 ? n : (fib(n - 1) + fib(n - 2));
1414
}
15-
};
15+
};

0509-fibonacci-number.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ class Solution:
1111
def fib(self, n: int) -> int:
1212
if n == 0 or n == 1:
1313
return n
14-
return self.fib(n - 1) + self.fib(n - 2)
14+
return self.fib(n - 1) + self.fib(n - 2)

0520-detect-capital.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ class Solution {
2929
}
3030
return true;
3131
}
32-
};
32+
};

0535-encode-and-decode-tinyurl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ public String decode(String shortUrl) {
2727

2828
// Your Codec object will be instantiated and called as such:
2929
// Codec codec = new Codec();
30-
// codec.decode(codec.encode(url));
30+
// codec.decode(codec.encode(url));

0700-search-in-a-binary-search-tree.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ class Solution {
3131
return root->val == val ? root : nullptr;
3232
}
3333
}
34-
};
34+
};

0709-to-lower-case.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ char* toLowerCase(char* s) {
1212
if ('A' <= *p && *p <= 'Z') *p = *p + 32;
1313
}
1414
return s;
15-
}
15+
}

0709-to-lower-case.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class Solution {
1111
public:
1212
string toLowerCase(string s) {
1313
for (char& c : s) {
14-
if ('A' <= c && c <= 'Z') c = c + 32;
14+
if ('A' <= c && c <= 'Z') c = c + 32; // 32 = 'a' - 'A'
1515
}
1616
return s;
1717
}
18-
};
18+
};

0709-to-lower-case.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
class Solution:
1111
def __init__(self):
1212
# this is a literal joke
13-
self.toLowerCase = str.lower
13+
self.toLowerCase = str.lower

0771-jewels-and-stones.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ class Solution {
2020
}
2121
return res;
2222
}
23-
};
23+
};

0876-middle-of-the-linked-list.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ class Solution {
2828
}
2929
return slow;
3030
}
31-
};
31+
};

0977-squares-of-a-sorted-array.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ class Solution {
1616
std::sort(nums.begin(), nums.end());
1717
return nums;
1818
}
19-
};
19+
};
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
// See `0476-number-compliment.cpp`
1+
// See `0476-number-compliment.cpp`.

1108-defanging-an-ip-address.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99

1010
class Solution:
1111
def defangIPaddr(self, address: str) -> str:
12-
return address.replace('.', '[.]')
12+
return address.replace('.', '[.]')

1290-convert-binary-number-in-a-linked-list-to-integer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ class Solution {
2828
}
2929
return res;
3030
}
31-
};
31+
};

1313-decompress-run-length-encoded-list.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ int* decompressRLElist(int* nums, int numsSize, int* returnSize) {
1818
*returnSize += *(p);
1919
p += 2;
2020
}
21-
int* returnArray = malloc(*returnSize << 2);
21+
int* returnArray = malloc(*returnSize << 2); // 32 bits = 4 bytes
2222
p = nums;
2323
int* r = returnArray;
2424
while (p != end) {

1313-decompress-run-length-encoded-list.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ class Solution {
1919
}
2020
return res;
2121
}
22-
};
22+
};

0 commit comments

Comments
 (0)