Skip to content

BJ001-1904147_第一周作业 #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Week_01/id_147/LeetCode_24_BJ001-1904147.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>

struct ListNode {
int val;
ListNode *next;

ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
ListNode *swapPairs(ListNode *head) {
ListNode *result = head;
ListNode *firstPre = nullptr;
ListNode *first = head;
ListNode *second = nullptr;
ListNode *secondNext = nullptr;
if (first != nullptr) {
second = first->next;
result = second;
}
while (first != nullptr && second != nullptr) {
secondNext = second->next;
second->next = first;
first->next = secondNext;
if (firstPre != nullptr) {
firstPre->next = second;
}
firstPre = first;
first = secondNext;
if (first != nullptr) {
second = first->next;
}
}
return result;
}
};
55 changes: 55 additions & 0 deletions Week_01/id_147/LeetCode_25_BJ001-1904147.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <iostream>
#include <vector>

struct ListNode {
int val;
ListNode *next;

ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
ListNode *reverseKGroup(ListNode *head, int k) {
ListNode *pre = nullptr;
ListNode *next = nullptr;
ListNode *node = head;
ListNode *result = nullptr;
std::vector<ListNode *> nodeList;
nodeList.resize(k, nullptr);
int i = 0;
while (node != nullptr) {
ListNode *pre = nodeList[0];
for (i = 0; i < k; ++i) {
if (node != nullptr) {
nodeList[i] = node;
node = node->next;
} else {
break;
};
}

if (i != k) {
if (result == nullptr) {
result = nodeList[0];
}
break;
}

if (result == nullptr) {
result = nodeList[k - 1];
}

if (pre != nullptr) {
pre->next = nodeList[k - 1];
}

for (i = 0; i < k - 1; ++i) {
nodeList[i + 1]->next = nodeList[i];
}

nodeList[0]->next = node;
}
return result;
}
};
28 changes: 28 additions & 0 deletions Week_01/id_147/LeetCode_441_BJ001-1904147.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>

using namespace std;

class Solution {
public:
int arrangeCoins(int n) {
int sum = n;
for (int i = 1; i <= n; ++i) {
sum -= i;

if (sum == 0) {
return i;
}

if (sum < 0) {
return i - 1;
}
}
}
};

int main(int argc, char **argv) {
Solution solution;
std::cout << solution.arrangeCoins(50) << std::endl;
std::cout << solution.arrangeCoins(8) << std::endl;
return 0;
}
44 changes: 44 additions & 0 deletions Week_01/id_147/LeetCode_503_BJ001-1904147.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
#include <vector>
#include <stack>

using namespace std;

class Solution {
public:
vector<int> nextGreaterElements(vector<int> &nums) {
vector<int> result;
auto n = nums.size();
result.resize(n, -1);
std::stack<int> stack;
for (int i = 0; i < 2 * n; ++i) {
int index = i % n;
auto num = nums[index];
if (stack.empty()) {
stack.push(num);
continue;
}
while (!stack.empty() && nums[stack.top()] < num) {
result[stack.top()] = num;
stack.pop();
}
stack.push(index);
}

return result;
}
};

int main(int argc, char **argv) {
vector<int> nums;
nums.emplace_back(1);
nums.emplace_back(2);
nums.emplace_back(1);
Solution solution;
auto result = solution.nextGreaterElements(nums);
for (auto i:result) {
std::cout << i << std::endl;
}

return 0;
}
67 changes: 67 additions & 0 deletions Week_01/id_147/LeetCode_81_BJ001-1904147.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <iostream>
#include <vector>

class Solution {
public:
bool search(std::vector<int> &nums, int target) {
if (nums.empty()) {
return false;
}
size_t length = nums.size();
if (length == 1) {
return nums[0] == target;
}
size_t left = 0;
size_t right = length - 1;

while (left < right) {
if (nums[left] == target) {
return true;
}
if (nums[right] == target) {
return true;
}
size_t mid = left + ((right - left) / 2);
if (nums[mid] == target) {
return true;
}

if (mid == right || mid == left) {
return false;
}

if (nums[left] <= nums[right]) {
if (nums[left] > target || nums[right] < target) {
return false;
}
if (nums[mid] > target) {
right = mid;
} else {
left = mid;
}
continue;
} else {
if (nums[mid] < target) {
if (nums[right] > target) {
left = mid;
} else {
if (nums[mid] < nums[right]) {
right = mid;
}else{
left = mid;
}
}
} else {
if (nums[right] > target) {
left = mid;
} else {
right = mid;
}
}
continue;
}
}

return nums[left] == target;
}
};
37 changes: 37 additions & 0 deletions Week_01/id_BJ001-1904147/LeetCode_24_BJ001-1904147.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <iostream>

struct ListNode {
int val;
ListNode *next;

ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
ListNode *swapPairs(ListNode *head) {
ListNode *result = head;
ListNode *firstPre = nullptr;
ListNode *first = head;
ListNode *second = nullptr;
ListNode *secondNext = nullptr;
if (first != nullptr) {
second = first->next;
result = second;
}
while (first != nullptr && second != nullptr) {
secondNext = second->next;
second->next = first;
first->next = secondNext;
if (firstPre != nullptr) {
firstPre->next = second;
}
firstPre = first;
first = secondNext;
if (first != nullptr) {
second = first->next;
}
}
return result;
}
};
55 changes: 55 additions & 0 deletions Week_01/id_BJ001-1904147/LeetCode_25_BJ001-1904147.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <iostream>
#include <vector>

struct ListNode {
int val;
ListNode *next;

ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
ListNode *reverseKGroup(ListNode *head, int k) {
ListNode *pre = nullptr;
ListNode *next = nullptr;
ListNode *node = head;
ListNode *result = nullptr;
std::vector<ListNode *> nodeList;
nodeList.resize(k, nullptr);
int i = 0;
while (node != nullptr) {
ListNode *pre = nodeList[0];
for (i = 0; i < k; ++i) {
if (node != nullptr) {
nodeList[i] = node;
node = node->next;
} else {
break;
};
}

if (i != k) {
if (result == nullptr) {
result = nodeList[0];
}
break;
}

if (result == nullptr) {
result = nodeList[k - 1];
}

if (pre != nullptr) {
pre->next = nodeList[k - 1];
}

for (i = 0; i < k - 1; ++i) {
nodeList[i + 1]->next = nodeList[i];
}

nodeList[0]->next = node;
}
return result;
}
};
Loading