ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode Weekly Contest 144

2019-07-28 12:56:27  阅读:262  来源: 互联网

标签:25 144 Contest int IP Example bookings address LeetCode


1108. Defanging an IP Address

Given a valid (IPv4) IP address, return a defanged version of that IP address.

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方法。

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 <= 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连续的,第一反应的优化方法就是前缀和。

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);
	}
}

  

 

标签:25,144,Contest,int,IP,Example,bookings,address,LeetCode
来源: https://www.cnblogs.com/Asimple/p/11258509.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有