mirror of
https://git.wolves.top/wolves/leetcode.git
synced 2025-11-04 17:26:32 +08:00
init
This commit is contained in:
35
24/01/2497.h
Normal file
35
24/01/2497.h
Normal file
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// Created by 李洋 on 2024/1/3.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_2497_H
|
||||
#define LEECODE_C_2497_H
|
||||
|
||||
#include "../../dataStruct/LinkedList/lists.h"
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
//单调栈
|
||||
ListNode *removeNodes(ListNode *head) {
|
||||
stack<ListNode *> S;
|
||||
S.push(head);
|
||||
ListNode *temp = head->next;
|
||||
while (temp) {
|
||||
while (!S.empty() && temp->val > S.top()->val) {
|
||||
S.pop();
|
||||
}
|
||||
S.push(temp);
|
||||
temp = temp->next;
|
||||
}
|
||||
temp = S.top();
|
||||
S.pop();
|
||||
while (!S.empty()) {
|
||||
S.top()->next = temp;
|
||||
temp = S.top();
|
||||
S.pop();
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_2497_H
|
||||
33
24/01/670.h
Normal file
33
24/01/670.h
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Created by 李洋 on 2024/1/22.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_670_H
|
||||
#define LEECODE_C_670_H
|
||||
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int maximumSwap(int num) {
|
||||
string charArray = to_string(num);
|
||||
int n = charArray.size();
|
||||
int maxIdx = n - 1;
|
||||
int idx1 = -1, idx2 = -1;
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
if (charArray[i] > charArray[maxIdx]) {
|
||||
maxIdx = i;
|
||||
} else if (charArray[i] < charArray[maxIdx]) {
|
||||
idx1 = i;
|
||||
idx2 = maxIdx;
|
||||
}
|
||||
}
|
||||
if (idx1 >= 0) {
|
||||
swap(charArray[idx1], charArray[idx2]);
|
||||
return stoi(charArray);
|
||||
} else {
|
||||
return num;
|
||||
}
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_670_H
|
||||
45
24/01/82.h
Normal file
45
24/01/82.h
Normal file
@@ -0,0 +1,45 @@
|
||||
//
|
||||
// Created by 李洋 on 2024/1/15.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_82_H
|
||||
#define LEECODE_C_82_H
|
||||
|
||||
// 本题与83题中要求删除重复多余的节点不同,要求只要重复就删除掉所有重复的节点,关机在于链表头的处理
|
||||
// 这里采用的是创建一个新的头节点,然后对后续节点进行继续处理,一次遍历即可
|
||||
|
||||
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) {}
|
||||
};
|
||||
|
||||
ListNode *deleteDuplicates(ListNode *head) {
|
||||
if (!head) {
|
||||
return head;
|
||||
}
|
||||
|
||||
ListNode* dummy = new ListNode(0, head);
|
||||
|
||||
ListNode* cur = dummy;
|
||||
while (cur->next && cur->next->next) {
|
||||
if (cur->next->val == cur->next->next->val) {
|
||||
int x = cur->next->val;
|
||||
while (cur->next && cur->next->val == x) {
|
||||
cur->next = cur->next->next;
|
||||
}
|
||||
}
|
||||
else {
|
||||
cur = cur->next;
|
||||
}
|
||||
}
|
||||
|
||||
return dummy->next;
|
||||
}
|
||||
|
||||
#endif //LEECODE_C_82_H
|
||||
Reference in New Issue
Block a user