当前位置: 首页 > article >正文

力扣刷题(数组篇)

日期类

#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

栗子:迷宫问题

429. N 叉树的层序遍历

994. 腐烂的橘子

127. 单词接龙
127相似433. 最小基因变化

752. 打开转盘锁


http://www.kler.cn/a/543063.html

相关文章:

  • centos7 升级openssl并安装python3
  • Flink KafkaConsumer offset是如何提交的
  • 本地基于GGUF部署的DeepSeek实现轻量级调优之二:检索增强生成(RAG)
  • 个人毕业设计--基于HarmonyOS的旅行助手APP的设计与实现(挖坑)
  • “可通过HTTP获取远端WWW服务信息”漏洞修复
  • 一种非完全图下的TSP求解算法
  • 全面理解-命名修饰规则(命名倾轧Name Mangling)
  • Redis 常见面试题汇总(持续更新)
  • 2.2 神经网络语言模型:从词向量到上下文感知的进化革命
  • 第三届通信网络与机器学习国际学术会议(CNML 2025)
  • 光耦隔离的作用及其原理 光耦隔离输入输出能共地
  • 从零到一学习c++(基础篇--筑基期六-string)
  • 【iSAID:用于航空影像实例分割的大规模数据集】
  • 嵌入式之详解:startup.S文件
  • Cherry Studio 连接私域deepseek-r1模型搭建私域知识库和智能体(也可使用第三方模型)
  • 图像处理之图像亮度/对比度调整
  • 【AI知识点】Adversarial Validation(对抗验证)
  • Redis核心技术知识点全集
  • 从工匠故事读懂开源软件的特点与价值
  • 物理引擎Box2D
  • 《图解设计模式》笔记(八)管理状态
  • 异位妊娠唯一相关的是年龄(U型曲线)
  • SWIFT (Scalable lightWeight Infrastructure for Fine-Tuning)
  • 【多模态大模型】系列2:Transformer Encoder-Decoder——BLIP、CoCa、BEITv3
  • 【MATLAB源码-第261期】基于matlab的帝企鹅优化算法(EPO)机器人栅格路径规划,输出做短路径图和适应度曲线
  • 浏览器渲染方式及性能优化