工作生活

ARTS-004

2019-07-08  本文已影响0人  玖柒叁

Algorithm


1108. Defanging an IP Address

public String defangIPaddr(String address) {
        StringBuilder sb = new StringBuilder();
        char[] ipv4 = address.toCharArray();
        for (int i = 0; i < ipv4.length; ++i) {
            if (ipv4[i] != '.') {
                sb.append(ipv4[i]);
                continue;
            }
            sb.append('[');
            sb.append('.');
            sb.append(']');
        }
        return sb.toString();
    }

1109. Corporate Flight Bookings

public int[] corpFlightBookings(int[][] bookings, int n) {
        int[] result = new int[n];
        for (int i = 0; i < bookings.length; ++i) {
            for (int j = bookings[i][0]; j <= bookings[i][1]; ++j) {
                int num =result[j] + bookings[i][2];
            }
        }
        return result;
    }

参考:C++ O(n)

int[] result = new int[n];
        for (int i = 0; i < bookings.length; ++i) {
            result[bookings[i][0] - 1] += bookings[i][2];
            //如果不是所有航班都被预定了,那么在j航班之后的就要排除掉
            if (bookings[i][1] < n) {
                result[bookings[i][1]] -= bookings[i][2];
            }
        }
        for (int i = 1; i < n; ++i) {
            result[i] += result[i-1];
        }
        return result;

Review


Tip


不小心把idea中debug窗口的console移出来后,只要移动到红框内即可以移动回去


示例

Share


上一篇下一篇

猜你喜欢

热点阅读