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
addressis a valid IPv4 address.
题目大意:给你一个ip地址,任务是将ip地址的"."改成"[.]"。
思路:直接模拟就好,或者直接用replace方法。
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 jinclusive.
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 <= 200001 <= bookings[i][0] <= bookings[i][1] <= n <= 200001 <= bookings[i][2] <= 10000
题目大意:给你一个航班预定表,表中第 i 条预订记录 bookings[i] = [i, j, k] 意味着我们在从 i 到 j 的每个航班上预订了 k 个座位。返回一个长度为 n 的数组 answer,按航班编号顺序返回每个航班上预订的座位数。
思路:直接看就是桶排,只不过长度太长,直接暴力肯定会时间超限。这时就需要时间优化了,看题目是i到j连续的,第一反应的优化方法就是前缀和。
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);
}
}