LeetCode Weekly Contest 144
1108. Defanging an IP Address
Given a valid (IPv4) IP address
, return a defanged version of that IP address.
A defanged IP address replaces every period "."
with "[.]"
.
Example 1:
Input: address = "1.1.1.1" Output: "1[.]1[.]1[.]1"
Example 2:
Input: address = "255.100.50.0" Output: "255[.]100[.]50[.]0"
Constraints:
- The given
address
is a valid IPv4 address.
题目大意:给你一个ip地址,任务是将ip地址的"."改成"[.]"。
思路:直接模拟就好,或者直接用replace方法。
1 2 3 4 5 6 7 8 9 10 11 12 | class Solution { public String defangIPaddr(String address) { int len = address.length(); StringBuffer bf = new StringBuffer(); for ( int i= 0 ; i<len; i++) { char ch = address.charAt(i); if ( ch == '.' ) bf.append( "[.]" ); else bf.append(ch); } return bf.toString(); } } |
1109. Corporate Flight Bookings
There are n
flights, and they are labeled from 1
to n
.
We have a list of flight bookings. The i
-th booking bookings[i] = [i, j, k]
means that we booked k
seats from flights labeled i
to j
inclusive.
Return an array answer
of length n
, representing the number of seats booked on each flight in order of their label.
Example 1:
Input: bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5 Output: [10,55,45,25,25]
Constraints:
1 <= bookings.length <= 20000
1 <= bookings[i][0] <= bookings[i][1] <= n <= 20000
1 <= bookings[i][2] <= 10000
题目大意:给你一个航班预定表,表中第 i
条预订记录 bookings[i] = [i, j, k]
意味着我们在从 i
到 j
的每个航班上预订了 k
个座位。返回一个长度为 n
的数组 answer
,按航班编号顺序返回每个航班上预订的座位数。
思路:直接看就是桶排,只不过长度太长,直接暴力肯定会时间超限。这时就需要时间优化了,看题目是i到j连续的,第一反应的优化方法就是前缀和。
1 2 3 4 5 6 7 8 9 10 11 12 13 | class Solution { public int [] corpFlightBookings( int [][] bookings, int n) { int [] a = new int [n+ 1 ]; for ( int [] b : bookings){ a[b[ 0 ]- 1 ] += b[ 2 ]; a[b[ 1 ]] -= b[ 2 ]; } for ( int i = 0 ;i < n;i++){ a[i+ 1 ] += a[i]; } return Arrays.copyOf(a, n); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
2016-07-28 ACM题目————滑雪
2016-07-28 ACM题目————次小生成树