力扣刷题(数组篇)
日期类
#pragma once
#include <iostream>
#include <assert.h>
using namespace std;
class Date
{
public:
// 构造会频繁调用,所以直接放在类里面(类里面的成员函数默认为内联)
Date(int year = 1, int month = 1, int day = 1)//构造
{
_year = year;
_month = month;
_day = day;
//if (!CheckDate())
//{
// Print();
// cout << "刚构造的日期非法" << endl;
//}
assert(CheckDate());
}
void Print() const; // 打印
int GetMonthDay(int year, int month)// 获取某年某月的天数
{
static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int day = days[month];
if (month == 2
&& ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
day += 1;
}
return day;
}
bool CheckDate()// 检查日期是否合法
{
if (_year >= 1
&& _month > 0 && _month < 13
&& _day > 0 && _day <= GetMonthDay(_year, _month))
{
return true;
}
else
{
return false;
}
}
bool operator==(const Date& d) const;
bool operator>(const Date& d) const;
bool operator!=(const Date& d) const;
bool operator>=(const Date& d) const;
bool operator<(const Date& d) const;
bool operator<=(const Date& d) const;
Date& operator+=(int day);
Date operator+(int day) const;
Date& operator-=(int day);
Date operator-(int day) const;
// 特殊处理,使用重载区分,后置++重载增加一个int参数跟前置构成函数重载进行区分
Date& operator++(); // 前置
Date operator++(int); // 后置
Date& operator--();// 前置
Date operator--(int);// 后置
int operator-(const Date& d) const; //日期减日期
void PrintWeekDay() const; //返回*this是星期几
private:
int _year;
int _month;
int _day;
};
#include "Date.h"
// void Date::Print(const Date* const this)
void Date::Print() const
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
// 任何一个类,只需要写一个> == 或者 < ==重载 剩下比较运算符重载复用即可
bool Date::operator== (const Date& d) const
{
return _year == d._year
&& _month == d._month
&& _day == d._day;
}
bool Date::operator>(const Date& d) const
{
if ((_year > d._year)
|| (_year == d._year && _month > d._month)
|| (_year == d._year && _month == d._month && _day > d._day))
{
return true;
}
else
{
return false;
}
}
bool Date::operator!=(const Date& d) const
{
return !(*this == d);
}
bool Date::operator>=(const Date& d) const
{
return (*this > d) || (*this == d);
}
bool Date::operator<(const Date& d) const
{
return !(*this >= d);
}
bool Date::operator<=(const Date& d) const
{
return !(*this > d);
}
Date& Date::operator+=(int day)
{
if (day < 0)
{
return *this -= -day;
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
Date Date::operator+(int day) const
{
Date ret = *this;
ret += day;
return ret;// 出了作用域ret对象就不在了,所以不能用引用返回
}
Date& Date::operator-=(int day)
{
if (day < 0)
{
return *this += -day;
}
_day -= day;
while (_day <= 0)
{
_month--;
if (_month == 0)
{
_year--;
_month = 12;
}
_day += GetMonthDay(_year, _month);
}
return *this;
}
Date Date::operator-(int day) const
{
Date ret = *this;
ret -= day;// ret.operator-=(day);
return ret;// 和 + 一样,出了作用域ret对象就不在了,所以不能用引用返回
}
Date& Date::operator++() // 前置
{
return *this += 1;
}
Date Date::operator++(int) // 后置
{
Date ret = *this;
*this += 1;
return ret;
}
Date& Date::operator--() // 前置
{
return *this -= 1;
}
Date Date::operator--(int) // 后置
{
Date ret = *this;
*this -= 1;
return ret;
}
int Date::operator-(const Date& d) const
{
int ret = 0;
int flag = -1;
Date min = *this;//默认第一个小,返回的时候乘上 -1
Date max = d;
if (*this > d)//默认错误,把小和大重置,返回时乘上 1
{
flag = 1;
min = d;
max = *this;
}
while (min != max)
{
++min;
++ret;
}
return ret * flag;
}
void Date::PrintWeekDay() const //打印*this是星期几
{
const char* Week[] = { "星期一","星期二" ,"星期三" , "星期四" ,"星期五" , "星期六" , "星期天" };
Date flag(1900, 1, 1); //1900年1月1日是星期一,自己减自己为0,对应下标0
cout << Week[(*this - flag) % 7] << endl;
}
#include "Date.h"
void TestDate7()
{
Date d1(2023, 5, 5);
Date d2(2023, 6, 7);
d1.Print();
d1.PrintWeekDay();
d2.Print();
d2.PrintWeekDay();
Date d3(1900, 1, 7);
Date d4(2050, 6, 7);
d3.Print();
d3.PrintWeekDay();
d4.Print();
d4.PrintWeekDay();
}
int main()
{
TestDate7();
return 0;
}
485. 最大连续 1 的个数 - 力扣(LeetCode)
剑指 Offer 04. 二维数组中的查找11111111
二维数组中的查找_牛客题霸_牛客网 (nowcoder.com)
剑指 Offer 11. 旋转数组的最小数字11111111
旋转数组的最小数字_牛客题霸_牛客网 (nowcoder.com)
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面111111111
调整数组顺序使奇数位于偶数前面__牛客网 (nowcoder.com)
剑指 Offer 39. 数组中出现次数超过一半的数字11111111111
数组中出现次数超过一半的数字_牛客题霸_牛客网 (nowcoder.com)
剑指 Offer 05. 替换空格111111111
替换空格_牛客题霸_牛客网 (nowcoder.com)
剑指 Offer 06. 从尾到头打印链表
从尾到头打印链表_牛客题霸_牛客网 (nowcoder.com)
剑指 Offer 07. 重建二叉树
重建二叉树_牛客题霸_牛客网 (nowcoder.com)
动态规划:
剑指 Offer 42. 连续子数组的最大和111111111111111
连续子数组的最大和_牛客题霸_牛客网 (nowcoder.com)
hash
排序算法的特殊理解
剑指 Offer 52. 两个链表的第一个公共节点
两个链表的第一个公共结点_牛客题霸_牛客网 (nowcoder.com)
7 算法公开课
动态规划
剑指 Offer 10- I. 斐波那契数列
剑指 Offer II 098. 路径的数目11111111111
剑指 Offer II 099. 最小路径之和
背包问题
背包问题_哔哩哔哩笔试题_牛客网 (nowcoder.com)
剑指 Offer II 094. 最少回文分割
剑指 Offer II 086. 分割回文子字符串
72. 编辑距离
115. 不同的子序列
贪心算法
栗子:选择排序
1221. 分割平衡字符串111111111111111
122. 买卖股票的最佳时机 II1111111111111
55. 跳跃游戏11111111111
435. 无重叠区间1111111000000
回溯算法
深度优先 : DFS
栗子 : 排列组合
690. 员工的重要性
733. 图像渲染
733相似:463. 岛屿的周长
130. 被围绕的区域
130相似:剑指 Offer II 105. 岛屿的最大面积
17. 电话号码的字母组合11111111
17相似:401. 二进制手表
39. 组合总和
1079. 活字印刷
51. N 皇后
51相似:52. N 皇后 II
广度优先: BFS
栗子:迷宫问题