问题陈述
我们已经给出了一个包含起始和结束时间对的二维数组,表示12小时制的时间间隔。同时,我们还给出了一个以12小时制表示的字符串 str。我们需要找到包含由 str 表示的时间的总间隔数。
示例示例
输入
arr[][2] = {{“12:02:AM”, “10:55:PM”}, {“12:51:AM”, “11:40:AM”}, {“01:30:AM”, “12:00:PM”}, {“11:57:PM”, “11:59:PM”}}, str = “2:30:AM”
输出
3
Explanation
的中文翻译为:解释
时间“2:30:AM”与前三个时间间隔相交。
输入
arr[][2] = {{“01:02:PM”, “10:55:PM”}, {“01:30:AM”, “11:00:AM”}}, str = “11:30:PM”
输出
0
Explanation
的中文翻译为:解释
时间“11:30:PM”与数组中给定的任何时间间隔不相交。
方法一
在这种方法中,我们将把时间转换为24小时制。然后,我们将通过比较来计算与给定时间相交的时间间隔的总数。此外,我们将使用substr()方法来获取子字符串,并使用stoi()方法将字符串转换为整数。
算法
步骤 1 - 定义convertTime()函数,用于将时间转换为24小时制。
步骤 1.1 − 使用replace()方法将第三个位置的冒号替换为空字符串。
步骤 1.2 − 从给定的字符串中获取表示小时的第一个和第二个字符,并通过将第一个数字乘以10再加上第二个数字将其转换为小时。
步骤 1.3 - 将 'time_24' 变量初始化为零。
步骤 1.4 − 我们需要处理两种情况来将时间转换为24小时制。第一种情况是上午,第二种情况是下午。
步骤 1.4.1 - 如果字符串中的第5个字符是'A',则时间为上午。如果时间为上午,并且小时等于12,则从字符串中仅提取分钟,因为我们将12:00 AM视为00:00小时。否则,将时间字符串转换为整数值。
步骤 1.4.2 - 如果字符串中的第五个字符是 'P',则时间为下午。提取时间字符串,并将其转换为整数。此外,如果小时不等于 12,则将 1200 添加到 'time_24' 变量中。
-
第二步 - convertTime()函数将以以下格式返回时间。
12:00:AM = 0000
12:58:AM = 0059
11:32:AM = 1132
11:32:PM = 1200 + 1132 = 2332
04:56:PM = 1200 + 456 = 1656
如果字符串中的第5个字符是'A',则时间为上午。如果时间是上午,并且小时等于12,则从字符串中仅提取分钟,因为我们将12:00 AM视为00:00小时。否则,将时间字符串转换为整数值。
第三步 - 将给定的时间字符串转换为24小时制格式。
第四步 - 使用for循环遍历时间间隔数组,并将每个时间字符串转换为24小时制。
第5步 - 同时,继续检查给定的时间字符串是否在当前间隔之间。如果是,则将'res'的计数增加1。
第六步 - 返回‘res’变量的值。
Example
的翻译为:示例
#include <bits/stdc++.h> using namespace std; // Function to convert the given time_24 in 24 hours format int convertTime(string str){ // Remove the colon from the string str.replace(2, 1, ""); // Stores the hour int char_h1 = (int)str[1] - '0'; int char_h2 = (int)str[0] - '0'; int hours = (char_h2 * 10 + char_h1 % 10); // variable to store the time in 24 hours format int time_24 = 0; // If the time is in "AM." if (str[5] == 'A'){ // If hours are equal to 12, then update it to 0 as 12 AM, and only minutes to time_24 if (hours == 12){ time_24 += stoi(str.substr(2, 2)); } else { // add hours and minutes to time_24 time_24 += stoi(str.substr(0, 4)); } } // If time is in "PM" else { // If hours is equal to 12, add time as it is to time_24 if (hours == 12){ time_24 += stoi(str.substr(0, 4)); } else { // add time to time_24 time_24 += stoi(str.substr(0, 4)); // add 1200 = 12 : 00 PM to time_24 to convert in 24 hours format. time_24 += 1200; } } return time_24; } // Function to find the total number of intervals that intersects with given meeting time_24 int totalIntersects(string arr[][2], int len, string str){ // to store the total number of intervals int res = 0; // convert the given time_24 in 24 hours format int convertedStr = convertTime(str); // Traverse the array for (int i = 0; i < len; i++){ // convert the starting time_24 of the current interval in 24-hour format int initial = convertTime(arr[i][0]); // convert the ending time_24 of the current interval in 24-hour format int end = convertTime(arr[i][1]); // If the given time_24 lies in the interval [initial, end], then increment res by 1 if ((initial <= convertedStr && convertedStr <= end) || (convertedStr >= end && convertedStr <= initial)) res++; } // Return res return res; } int main(){ string arr[][2] = {{"11:00:AM", "11:55:PM"}, {"12:19:AM", "9:30:AM"}, {"12:51:AM", "12:59:PM"}, {"6:57:AM", "7:50:PM"}}; string str = "12:54:AM"; int len = sizeof(arr) / sizeof(arr[0]); cout << "The total number of the interval that intersects with given meeting time_24 are - " << totalIntersects(arr, len, str) << endl; }
输出
The total number of the interval that intersects with given meeting time_24 are - 2
时间复杂度 - O(N),因为我们遍历时间间隔的数组。
空间复杂度 − O(1),因为我们不使用常量空间。
在解决上述问题时,用户应主要关注将时间转换为24小时制,然后只需进行正常的比较。
以上是计算与给定会议时间相交的区间数的详细内容。更多信息请关注PHP中文网其他相关文章!

C#和C 的历史与演变各有特色,未来前景也不同。1.C 由BjarneStroustrup在1983年发明,旨在将面向对象编程引入C语言,其演变历程包括多次标准化,如C 11引入auto关键字和lambda表达式,C 20引入概念和协程,未来将专注于性能和系统级编程。2.C#由微软在2000年发布,结合C 和Java的优点,其演变注重简洁性和生产力,如C#2.0引入泛型,C#5.0引入异步编程,未来将专注于开发者的生产力和云计算。

C#和C 的学习曲线和开发者体验有显着差异。 1)C#的学习曲线较平缓,适合快速开发和企业级应用。 2)C 的学习曲线较陡峭,适用于高性能和低级控制的场景。

C#和C 在面向对象编程(OOP)中的实现方式和特性上有显着差异。 1)C#的类定义和语法更为简洁,支持如LINQ等高级特性。 2)C 提供更细粒度的控制,适用于系统编程和高性能需求。两者各有优势,选择应基于具体应用场景。

从XML转换到C 并进行数据操作可以通过以下步骤实现:1)使用tinyxml2库解析XML文件,2)将数据映射到C 的数据结构中,3)使用C 标准库如std::vector进行数据操作。通过这些步骤,可以高效地处理和操作从XML转换过来的数据。

C#使用自动垃圾回收机制,而C 采用手动内存管理。1.C#的垃圾回收器自动管理内存,减少内存泄漏风险,但可能导致性能下降。2.C 提供灵活的内存控制,适合需要精细管理的应用,但需谨慎处理以避免内存泄漏。

C 在现代编程中仍然具有重要相关性。1)高性能和硬件直接操作能力使其在游戏开发、嵌入式系统和高性能计算等领域占据首选地位。2)丰富的编程范式和现代特性如智能指针和模板编程增强了其灵活性和效率,尽管学习曲线陡峭,但其强大功能使其在今天的编程生态中依然重要。

C 学习者和开发者可以从StackOverflow、Reddit的r/cpp社区、Coursera和edX的课程、GitHub上的开源项目、专业咨询服务以及CppCon等会议中获得资源和支持。1.StackOverflow提供技术问题的解答;2.Reddit的r/cpp社区分享最新资讯;3.Coursera和edX提供正式的C 课程;4.GitHub上的开源项目如LLVM和Boost提升技能;5.专业咨询服务如JetBrains和Perforce提供技术支持;6.CppCon等会议有助于职业

C#适合需要高开发效率和跨平台支持的项目,而C 适用于需要高性能和底层控制的应用。1)C#简化开发,提供垃圾回收和丰富类库,适合企业级应用。2)C 允许直接内存操作,适用于游戏开发和高性能计算。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

Atom编辑器mac版下载
最流行的的开源编辑器

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境