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

Commit ea4ea9e

Browse files
update 1, add 75, 136, 147, 2415
1 parent ad5ad4a commit ea4ea9e

File tree

7 files changed

+194
-0
lines changed

7 files changed

+194
-0
lines changed

‎0001-two-sum.cpp‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
1. Two Sum
3+
4+
Submitted: December 20, 2024
5+
6+
Runtime: 3 ms (beats 77.60%)
7+
Memory: 14.80MB (beats 24.95%)
8+
*/
9+
10+
class Solution {
11+
public:
12+
vector<int> twoSum(vector<int>& nums, int target) {
13+
unordered_map<int, int> map;
14+
for (int i = 0, n = nums.size(); i < n; ++i) {
15+
if (map.count(target - nums[i])) {
16+
return { i, map[target - nums[i]] };
17+
} else {
18+
map[nums[i]] = i;
19+
}
20+
}
21+
return {}; // this is only here so that compliation doesn't fail
22+
}
23+
};

‎0075-sort-colors.c‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
75. Sort Colors
3+
4+
Submitted: December 20, 2024
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 9.70 MB (beats 68.92%)
8+
*/
9+
10+
void sortColors(int* nums, int numsSize) {
11+
int count[3] = { 0, 0, 0 };
12+
for (int* p = nums, *end = (nums + numsSize); p != end; ++p) {
13+
count[*p]++;
14+
}
15+
int* p = nums;
16+
for (int i = 0; i < count[0]; ++i) {
17+
*p++ = 0;
18+
}
19+
for (int i = 0; i < count[1]; ++i) {
20+
*p++ = 1;
21+
}
22+
for (int i = 0; i < count[2]; ++i) {
23+
*p++ = 2;
24+
}
25+
}

‎0075-sort-colors.cpp‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
75. Sort Colors
3+
4+
Submitted: December 20, 2024
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 11.46 MB (beats 35.93%)
8+
*/
9+
10+
class Solution {
11+
public:
12+
// counting sort
13+
void sortColors(vector<int>& nums) {
14+
int count[3] = { 0, 0, 0 };
15+
for (const int i : nums) {
16+
count[i]++;
17+
}
18+
auto it = nums.begin();
19+
for (int i = 0; i < count[0]; ++i) {
20+
*it++ = 0;
21+
}
22+
for (int i = 0; i < count[1]; ++i) {
23+
*it++ = 1;
24+
}
25+
for (int i = 0; i < count[2]; ++i) {
26+
*it++ = 2;
27+
}
28+
}
29+
};

‎0136-single-number.cpp‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
136. Single Number
3+
4+
Submitted: December 20, 2024
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 20.71 MB (beats 23.16%)
8+
*/
9+
10+
class Solution {
11+
public:
12+
int singleNumber(vector<int>& nums) {
13+
int n = 0;
14+
for (const int i : nums) {
15+
n ^= i;
16+
}
17+
return n;
18+
}
19+
};

‎0136-single-number.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""
2+
136. Single Number
3+
4+
Runtime: 4 ms (beats 48.81%)
5+
Memory: 19.51 MB (beats 6.17%)
6+
"""
7+
8+
class Solution:
9+
def singleNumber(self, nums: List[int]) -> int:
10+
return functools.reduce(lambda x, y: x ^ y, nums)

‎0147-insertion-sort-list.cpp‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
147. Insertion Sort List
3+
4+
Submitted: December 20, 2024
5+
6+
Runtime: 43 ms (beats 5.75%)
7+
Memory: 14.59 MB (beats 65.76%)
8+
*/
9+
10+
/**
11+
* Definition for singly-linked list.
12+
* struct ListNode {
13+
* int val;
14+
* ListNode *next;
15+
* ListNode() : val(0), next(nullptr) {}
16+
* ListNode(int x) : val(x), next(nullptr) {}
17+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
18+
* };
19+
*/
20+
class Solution {
21+
public:
22+
ListNode* insertionSortList(ListNode* head) {
23+
ListNode* newHead = head;
24+
head = head->next;
25+
newHead->next = nullptr;
26+
while (head != nullptr) {
27+
cout << head->val;
28+
ListNode* node = head;
29+
head = head->next;
30+
ListNode* p = newHead;
31+
if (node->val < newHead->val) {
32+
node->next = newHead;
33+
newHead = node;
34+
}
35+
else {
36+
for (; p->next != nullptr && p->next->val < node->val; p = p->next) ;
37+
node->next = p->next;
38+
p->next = node;
39+
}
40+
}
41+
return newHead;
42+
}
43+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
2415. Reverse Odd Levels of Binary Tree
3+
4+
LeetCode Daily Question for December 20, 2024
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 79.82 MB (beats 57.48%)
8+
*/
9+
10+
/**
11+
* Definition for a binary tree node.
12+
* struct TreeNode {
13+
* int val;
14+
* TreeNode *left;
15+
* TreeNode *right;
16+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
17+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
18+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
19+
* };
20+
*/
21+
class Solution {
22+
public:
23+
TreeNode* reverseOddLevels(TreeNode* root) {
24+
if (root == nullptr) return root;
25+
deque<TreeNode*> queue;
26+
int level = 0;
27+
queue.push_back(root);
28+
while (!queue.empty()) {
29+
// level order traversal stuff
30+
for (int i = 0, n = queue.size(); i < n; ++i) {
31+
TreeNode* p = queue.back();
32+
queue.pop_back();
33+
if (p->left != nullptr) queue.push_front(p->left);
34+
if (p->right != nullptr) queue.push_front(p->right);
35+
}
36+
if (++level & 1) { // if level is odd
37+
// reverse node values
38+
for (int i = 0, n = queue.size(); i < n / 2; ++i) {
39+
swap(queue[i]->val, queue[n - i - 1]->val);
40+
}
41+
}
42+
}
43+
return root;
44+
}
45+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /