1、C语言排序函数qsort
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
qsort (values, 6, sizeof(int), compare);
2、月份
// a==1是闰年
if(year%400==0)
a=1;
else
{
if(year%4==0&&year%100!=0)
a=1;
else
a=0;
}
// 每个月的天数
1 2 3 4 5 6 7 8 9 10 11 12
31 28/29 30 31 30 31 30 31 31 30 31 31
no/yes 闰年
偶数
(x&1)==0
奇数
(x&1)!=0
(target-1)/2 // 相当于target除2向下取整
istringstream
class Solution {
public:
string reverseWords(string s) {
stack<string> stk;
string res,str;
istringstream ss(s);
while (ss >> str) stk.push(str), stk.push(" ");
if (!stk.empty()) stk.pop();
while (!stk.empty()) res += stk.top(), stk.pop();
return res;
}
};
// 直观上看 istringstream 可以将一句话根据空格进行分割,每次输出一个单词到string对象
容器内元素排序 通过定义规则的方式排序
sort(arr.begin(),arr.end(),[&](int x,int y){
if (bit[x] < bit[y]) {
return true;
}
if (bit[x] > bit[y]) {
return false;
}
return x < y;
});
sort中定义排序规则:
大根堆 小根堆
priority_queue<int, vector<int>, std::less<int>> maxHeap;
priority_queue<int, vector<int>, std::greater<int>> minHeap;
输入:people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
输出:[[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
// u[0] v[0] 从大到小排序
// u[1] v[1] 从小到大排序
sort(people.begin(), people.end(), [](const auto& u, const auto& v) {
return (u[0] > v[0]) || (u[0] == v[0] && u[1] < v[1]);
});