class Solution {
public:
string multiply(string num1, string num2){
vector<int>product(num1.size()+ num2.size(),0);//这里是把数值先按位存储起来for(int i =0; i < num1.size(); i++)for(int j =0; j < num2.size(); j++)
product[num1.size()- i + num2.size()- j -2]+=(num1[i]-'0')*(num2[j]-'0');//这个是处理进位int t =0;for(int i =0; i < product.size(); i++){int&x = product[i];
t += x;
x = t %10;
t /=10;}
string res;//去除前导零int k = product.size()-1;while(!product[k]&& k >0)k--;for(int i = k; i >=0; i--)res +=to_string(product[i]);return res;}};
LeetCode 48. Rotate Image
class Solution {
public:voidrotate(vector<vector<int>>& matrix){//45度进行翻转for(int i =0; i < matrix.size(); i++)for(int j =0; j < i; j++)swap(matrix[i][j], matrix[j][i]);//水平翻转for(int i =0; i < matrix.size(); i++)for(int j =0, k = matrix[i].size()-1; j < k; j++, k--)swap(matrix[i][j], matrix[i][k]);}};
LeetCode 31. Next Permutation
class Solution {
public:voidnextPermutation(vector<int>& nums){//遍历所有的数字for(int i = nums.size()-1; i >0; i--)//如果有逆序if(nums[i-1]< nums[i]){//从逆序中找到一个比自己大的数字int j = i;while(j +1< nums.size()&& nums[j +1]> nums[i-1])j++;//交换两个数字swap(nums[i-1], nums[j]);//再重新排下序reverse(nums.begin()+ i, nums.end());return;}//如果没有,说明就是最大的了reverse(nums.begin(), nums.end());}};
LeetCode 23. Merge k Sorted Lists
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode*mergeKLists(vector<ListNode*>& lists){//用一个优先队列来存储
priority_queue<pair<int, ListNode*>> heap;for(auto list : lists){//因为是最大堆,所以加了一个负号,就变成最小堆了if(list)heap.push({-list->val, list});}
ListNode * dummy = new ListNode(-1);auto cur = dummy;while(heap.size()){//取出最小值,然后该链表还有没有下一个auto t = heap.top();
heap.pop();if(t.second->next)heap.push({-t.second->next->val, t.second->next});
cur->next = t.second;
cur = cur->next;}
cur->next =NULL;return dummy->next;}};
LeetCode 33. Search in Rotated Sorted Array
class Solution {
public:intsearch(vector<int>& nums,int target){//数据比较小,那就直接查找就好了if(nums.size()<5){for(int i =0; i < nums.size(); i++)if(nums[i]== target)return i;return-1;}//第一次二分,查找到边界int left =0, right = nums.size()-1;if(nums[0]> nums.back()){int l =0, r = nums.size()-1;while(l < r){int mid = l + r +1>>1;if(nums[mid]>= nums[0])l = mid;else r = mid -1;}if(target >= nums[0])left =0, right = l;else left = l +1, right = nums.size()-1;}//因为二分是针对有序数据的,所以第二次二分while(left < right){int mid = left + right >>1;if(nums[mid]< target)left = mid +1;else right = mid;}if(nums[left]== target)return left;return-1;}};
LeetCode 11. Container With Most Water
class Solution {
public:intmaxArea(vector<int>& height){int res =0;for(int l =0, r = height.size()-1; l < r;){int left = height[l], right = height[r];
res =max(res,(r-l)*min(left, right));if(left < right)l++;else r--;}return res;}};
LeetCode 41. First Missing Positive
class Solution {
public:intfirstMissingPositive(vector<int>& nums){for(int i =0; i < nums.size(); i++)while(nums[i]>=1&& nums[i]<= nums.size()&& nums[nums[i]-1]!= nums[i])swap(nums[i], nums[nums[i]-1]);for(int i =0; i < nums.size(); i++)if(nums[i]!= i +1)return i +1;return nums.size()+1;}};
LeetCode 25. Reverse Nodes in k-Group
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode*reverseKGroup(ListNode* head,int k){//使用虚拟节点
ListNode * dummy = new ListNode(-1);
dummy->next = head;auto cur = dummy;//链表题一般需要画图,需要注意的细节比较多while(cur){int s =0;for(auto i = cur->next; i;i = i->next)s++;if(s < k)break;
s =0;auto a = cur->next, b = a->next;while(s < k -1){
s++;auto c = b->next;
b->next = a;
a = b, b = c;}auto p = cur->next;
cur->next->next = b;
cur->next = a;
cur = p;}return dummy->next;}};