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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

0 commit comments

Comments
 (0)