1. 快排
class Solution {
public:
vector<int> getLeastNumbers(vector<int>& arr, int k) {
vector<int> ans;
quickSort(arr, 0, arr.size() - 1);
cout<<arr[0];
ans.assign(arr.begin(), arr.begin() + k);
return ans;
}
private:
void quickSort(vector<int>& arr, int l, int r){
if(l >= r) return;
int i = l, j = r;
while(i < j){
//arr[l]为哨兵,从右向左找比它小的
while(i < j && arr[j] >= arr[l]) j--;
//从左向右找比哨兵大的
while(i < j && arr[i] <= arr[l]) i++;
//交换两者
swap(arr[i], arr[j]);
}
//最后交换基准数
swap(arr[l], arr[i]);
quickSort(arr, l, i - 1);
quickSort(arr, i + 1, r);
}
};
2. 二维前缀和
class NumMatrix {
public:
vector<vector<int>> sum;
NumMatrix(vector<vector<int>>& matrix) {
int m = matrix.size();
if(m > 0){
int n = matrix[0].size();
sum.resize(m + 1, vector<int>(n + 1, 0));
for(int i = 0; i < m; i++){
for(int j = 0; j < n ;j++){
sum[i + 1][j + 1] = sum[i][j + 1] + sum[i + 1][j] - sum[i][j] + matrix[i][j];
}
}
}
}
int sumRegion(int row1, int col1, int row2, int col2) {
return sum[row2 + 1][col2 + 1] - sum[row1][col2 + 1] - sum[row2 + 1][col1] + sum[row1][col1];
}
};
3.快速幂
class Solution {
public:
double myPow(double x, int n) {
long b = n;
double ans = 1;
if(b < 0){
x = 1 / x;
b = -b;
}
while(b){
if(b & 1 == 1) ans *= x;
x *= x;
b >>= 1;
}
return ans;
}
};
4.stringstream的用法
#include<iostream>
#include<sstream> //istringstream 必须包含这个头文件
#include<string>
using namespace std;
int main()
{
string str="i am a boy";
istringstream is(str);
string s;
while(is>>s)
{
cout<<s<<endl;
}
}
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
if ( root == nullptr ) return "";
ostringstream output;
queue<TreeNode*> que;
que.push(root);
while ( !que.empty() ) {
TreeNode* node = que.front();
que.pop();
if ( node == nullptr ) output << "# ";
else {
output << node->val << ' ';
que.push(node->left);
que.push(node->right);
}
}
return output.str();
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
if ( data.empty() ) return nullptr;
vector<TreeNode*> nodes;
string val;
istringstream input(data);
while ( input >> val ) {
if ( val == "#" ) nodes.push_back(nullptr);
else nodes.push_back(new TreeNode(stoi(val)));
};
int pos = 1;
for ( int i = 0; i < nodes.size(); ++i ) {
if ( nodes[i] == nullptr ) continue;
nodes[i]->left = nodes[pos++];
nodes[i]->right = nodes[pos++];
}
return nodes[0];
}
};
5.堆的自定义排序
方式1 struct
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
//1.map记录元素出现的次数
unordered_map<int,int>map;//两个int分别是元素和出现的次数
for(auto& c:nums){
map[c]++;
}
//2.利用优先队列,将出现次数排序
//自定义优先队列的比较方式,小顶堆
struct myComparison{
bool operator()(pair<int,int>&p1,pair<int,int>&p2){
return p1.second>p2.second;//小顶堆是大于号
}
};
//创建优先队列
priority_queue<pair<int,int>,vector<pair<int,int>>,myComparison> q;
//遍历map中的元素
//1.管他是啥,先入队列,队列会自己排序将他放在合适的位置
//2.若队列元素个数超过k,则将栈顶元素出栈(栈顶元素一定是最小的那个)
for(auto& a:map){
q.push(a);
if(q.size()>k){
q.pop();
}
}
//将结果导出
vector<int> res(k);
for(int i=k-1;i>=0;i--){
res[i] = q.top().first;
q.pop();
}
return res;
}
};
方式2
6.map的auto遍历方式
参考
方式1
for(auto it:mp){
cout <<it->first << " " << it->second ;
}
方式2
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
unordered_map<int, int> mp;
int main() {
int n;
cin >> n;
for (int i=0; i <= n; i++) {
int a;
cin >> a;
mp[a]++;
}
for (auto &[k , v] : mp) {
cout << k << " " << v << endl;
}
return 0;
}
7.lambda表达式
class Solution {
public:
string largestNumber(vector<int>& nums)
{
if (all_of(nums.begin(), nums.end(), [](int x) { return x == 0; })) {
return string("0");
}
vector<string> strNums(nums.size());
std::transform(nums.begin(), nums.end(), strNums.begin(), [](int x) {
return std::to_string(x);
});
std::sort(strNums.begin(), strNums.end(), [](const string& x, const string& y) {
/* x为后面元素,y为前面元素,return true则将x移动到前面 */
return x + y > y + x;
});
return std::accumulate(strNums.begin(), strNums.end(), string());
}
};
作者:harold
链接:https://leetcode.cn/problems/largest-number/solutions/38574/c-4ms-by-harold-15/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。