feat: add solutions for various problems including maximum 69 number, majority element, and rotate list
This commit is contained in:
parent
c3b6b812fd
commit
b9082d599a
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
**/target/
|
7
1323_maximum_69_number/Cargo.lock
generated
Normal file
7
1323_maximum_69_number/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 4
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "maximum_69_number"
|
||||||
|
version = "0.1.0"
|
6
1323_maximum_69_number/Cargo.toml
Normal file
6
1323_maximum_69_number/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "maximum_69_number"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
21
1323_maximum_69_number/src/main.rs
Normal file
21
1323_maximum_69_number/src/main.rs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
fn main() {}
|
||||||
|
|
||||||
|
struct Solution;
|
||||||
|
|
||||||
|
impl Solution {
|
||||||
|
pub fn maximum69_number(num: i32) -> i32 {
|
||||||
|
let mut num_chars = num.to_string().chars().collect::<Vec<char>>();
|
||||||
|
for (i, c) in num_chars.clone().iter().enumerate() {
|
||||||
|
if *c == '6' {
|
||||||
|
num_chars[i] = '9';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
num_chars
|
||||||
|
.into_iter()
|
||||||
|
.collect::<String>()
|
||||||
|
.parse::<i32>()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
24
169_majority_element/main.cpp
Normal file
24
169_majority_element/main.cpp
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int majorityElement(vector<int>& nums) {
|
||||||
|
// result can be any number
|
||||||
|
int count = 0, result = 0;
|
||||||
|
|
||||||
|
for (auto num : nums) {
|
||||||
|
if (count == 0) {
|
||||||
|
result = num;
|
||||||
|
count++;
|
||||||
|
} else {
|
||||||
|
if (result == num)
|
||||||
|
count++;
|
||||||
|
else
|
||||||
|
count--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
18
171_excel_sheet_column_number/main.cpp
Normal file
18
171_excel_sheet_column_number/main.cpp
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int titleToNumber(string columnTitle) {
|
||||||
|
reverse(columnTitle.begin(), columnTitle.end());
|
||||||
|
|
||||||
|
int result = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < columnTitle.length(); i++) {
|
||||||
|
const char c = columnTitle[i];
|
||||||
|
result += (c - 'A' + 1) * (int)pow(26, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
4
175_combine_two_tables/query.sql
Normal file
4
175_combine_two_tables/query.sql
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
SELECT p.firstName, p.lastName, a.city, a.state
|
||||||
|
FROM Person AS p
|
||||||
|
LEFT JOIN Address AS a
|
||||||
|
ON p.personId = a.personId;
|
8
181_employees_earning_more_than_their_managers/query.sql
Normal file
8
181_employees_earning_more_than_their_managers/query.sql
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
SELECT
|
||||||
|
e.name AS "Employee"
|
||||||
|
FROM
|
||||||
|
Employee e
|
||||||
|
INNER JOIN
|
||||||
|
Employee m ON e.managerId = m.id
|
||||||
|
WHERE
|
||||||
|
e.salary > m.salary;
|
8
182_duplicates_email/query.sql
Normal file
8
182_duplicates_email/query.sql
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
SELECT
|
||||||
|
p.email AS "Email"
|
||||||
|
FROM
|
||||||
|
Person p
|
||||||
|
GROUP BY
|
||||||
|
p.email
|
||||||
|
HAVING
|
||||||
|
COUNT(*) > 1;
|
8
183_customers_who_never_order/query.sql
Normal file
8
183_customers_who_never_order/query.sql
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
SELECT
|
||||||
|
*
|
||||||
|
FROM
|
||||||
|
Customers c
|
||||||
|
LEFT JOIN
|
||||||
|
Orders o ON c.id = o.customerId
|
||||||
|
WHERE
|
||||||
|
o.customerId IS NULL;
|
17
184_department_highest_salary/query.sql
Normal file
17
184_department_highest_salary/query.sql
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
SELECT
|
||||||
|
d.name AS "Department",
|
||||||
|
e.name AS "Employee",
|
||||||
|
e.salary AS "Salary"
|
||||||
|
FROM
|
||||||
|
Employee e
|
||||||
|
INNER JOIN
|
||||||
|
Department d ON e.departmentId = d.id
|
||||||
|
WHERE
|
||||||
|
e.salary = (
|
||||||
|
SELECT
|
||||||
|
MAX(salary)
|
||||||
|
FROM
|
||||||
|
Employee
|
||||||
|
WHERE
|
||||||
|
departmentId = d.id
|
||||||
|
);
|
19
190_reverse_bits/main.cpp
Normal file
19
190_reverse_bits/main.cpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
uint32_t reverseBits(uint32_t n) {
|
||||||
|
uint32_t result = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < 32; i++) {
|
||||||
|
result <<= 1;
|
||||||
|
if (n & 1) {
|
||||||
|
result |= 1;
|
||||||
|
}
|
||||||
|
n >>= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
31
203_remove_linked_list_elements/main.cpp
Normal file
31
203_remove_linked_list_elements/main.cpp
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct ListNode {
|
||||||
|
int val;
|
||||||
|
ListNode* next;
|
||||||
|
ListNode() : val(0), next(nullptr) {}
|
||||||
|
ListNode(int x) : val(x), next(nullptr) {}
|
||||||
|
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
ListNode* removeElements(ListNode* head, int target) {
|
||||||
|
ListNode* tempHead = new ListNode(-1, head);
|
||||||
|
ListNode* currentNode = tempHead;
|
||||||
|
ListNode* lastNode = nullptr;
|
||||||
|
|
||||||
|
while (currentNode) {
|
||||||
|
if (currentNode->val == target) {
|
||||||
|
lastNode->next = currentNode->next;
|
||||||
|
currentNode = currentNode->next;
|
||||||
|
} else {
|
||||||
|
lastNode = currentNode;
|
||||||
|
currentNode = currentNode->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return tempHead->next;
|
||||||
|
}
|
||||||
|
};
|
40
57_insert_intervals/main.cpp
Normal file
40
57_insert_intervals/main.cpp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
|
||||||
|
if (intervals.empty()) return {newInterval};
|
||||||
|
|
||||||
|
vector<vector<int>> result;
|
||||||
|
|
||||||
|
if (newInterval[1] < intervals.front()[0]) {
|
||||||
|
result.emplace_back(newInterval);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < intervals.size(); i++) {
|
||||||
|
// new interval 與 intervals[i] 有交集
|
||||||
|
if ((intervals[i][0] <= newInterval[0] && newInterval[0] <= intervals[i][1]) ||
|
||||||
|
(newInterval[0] <= intervals[i][0] && intervals[i][0] <= newInterval[1])) {
|
||||||
|
vector<int> intervalToPush(intervals[i]);
|
||||||
|
intervalToPush[0] = min(newInterval[0], intervalToPush[0]);
|
||||||
|
for (; i < intervals.size(); i++) {
|
||||||
|
if (newInterval[1] < intervals[i][0]) break;
|
||||||
|
intervalToPush[1] = max(newInterval[1], intervals[i][1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
result.emplace_back(intervalToPush);
|
||||||
|
i--;
|
||||||
|
} else {
|
||||||
|
result.emplace_back(intervals[i]);
|
||||||
|
// new interval 跟前後兩個 intervals[i], intervals[i + 1] 沒有交集
|
||||||
|
if (intervals[i][1] < newInterval[0] &&
|
||||||
|
(i == intervals.size() - 1 || newInterval[1] < intervals[i + 1][0])) {
|
||||||
|
result.emplace_back(newInterval);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
57
61_rotate_list/main.cpp
Normal file
57
61_rotate_list/main.cpp
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
struct ListNode {
|
||||||
|
int val;
|
||||||
|
ListNode* next;
|
||||||
|
ListNode() : val(0), next(nullptr) {}
|
||||||
|
ListNode(int x) : val(x), next(nullptr) {}
|
||||||
|
ListNode(int x, ListNode* next) : val(x), next(next) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
private:
|
||||||
|
ListNode* indexOf(ListNode* head, int index) {
|
||||||
|
ListNode* currentNode = head;
|
||||||
|
int currentIndex = 0;
|
||||||
|
while (currentNode) {
|
||||||
|
if (currentIndex == index) return currentNode;
|
||||||
|
currentNode = currentNode->next;
|
||||||
|
currentIndex++;
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
ListNode* rotateRight(ListNode* head, int rotateCount) {
|
||||||
|
if (!head) return nullptr;
|
||||||
|
|
||||||
|
ListNode* currentNode = head;
|
||||||
|
ListNode* tail = nullptr;
|
||||||
|
int len = 0;
|
||||||
|
while (currentNode) {
|
||||||
|
len++;
|
||||||
|
tail = currentNode;
|
||||||
|
currentNode = currentNode->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (len == 1) return head;
|
||||||
|
|
||||||
|
rotateCount %= len;
|
||||||
|
|
||||||
|
if (rotateCount == 0) return head;
|
||||||
|
|
||||||
|
ListNode* newTail = indexOf(head, len - rotateCount - 1);
|
||||||
|
ListNode* newHead = newTail->next;
|
||||||
|
newTail->next = nullptr;
|
||||||
|
tail->next = head;
|
||||||
|
|
||||||
|
return newHead;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
ListNode* head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
|
||||||
|
Solution().rotateRight(head, 2);
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user