zoukankan      html  css  js  c++  java
  • LeetCode Weekly Contest 144

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

      

  • 相关阅读:
    解决SecureCRT中文显示乱码
    最新ubuntu10.10更新源
    向linux内核添加系统调用新老内核比较
    Field requires API level 5 (current min is 1) 问题的解决
    ubuntu 搜索文件方法(find命令)
    ubuntu12.04终端全屏
    .classpath 文件中的excluding属性
    eclipse中的.project 和 .classpath文件的具体作用
    Windows Mobile项目编译很慢情况的解决(VS2008)
    windowsphone7高级编程中提到的地址
  • 原文地址:https://www.cnblogs.com/Asimple/p/11258509.html
Copyright © 2011-2022 走看看