第一题
class Solution {
public:
string getSmallestString(string s) {
for(int i = 0; i + 1 < s.size(); i ++ )
if(s[i] % 2 == s[i + 1] % 2)
if(s[i] > s[i + 1])
{
swap(s[i], s[i + 1]);
break;
}
return s;
}
};
第二题
/**
* Definition for singly-linked list.
* 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* modifiedList(vector<int>& nums, ListNode* head) {
unordered_map<int, int> hash;
for(auto num: nums) hash[num] ++ ;
while(head->next && hash[head->val]) head = head -> next;
auto p = head, q = head-> next;
while(q)
{
while(q != nullptr && hash[q->val]) q = q->next;
p -> next = q;
p = q;
if(q) q = q -> next;
}
return head;
}
};
第三题 && 第四题
class Solution {
public:
long long minimumCost(int m, int n, vector<int>& horizontalCut, vector<int>& verticalCut) {
ranges::sort(horizontalCut, greater<>());
ranges::sort(verticalCut, greater<>());
int cnt_h = 1, cnt_v = 1;
int i = 0, j = 0;
long long ans = 0;
while(i < m - 1 || j < n - 1)
{
if(j == n - 1 || i < m - 1 && horizontalCut[i] > verticalCut[j])
{
ans += horizontalCut[i ++ ] * cnt_h;
cnt_v ++ ;
}
else
{
ans += verticalCut[j ++ ] * cnt_v;
cnt_h ++ ;
}
}
return ans;
}
};