1. 程式人生 > >Leetcode contests 93 題解

Leetcode contests 93 題解

868. Binary Gap

  簡單題,就是求一個數字二進位制形式中兩個1的最大間隔位置,比如22的二進位制0b10110,最大距離就是2,0b100001,最大距離是5。

class Solution {
    public int binaryGap(int N) {
        String bs = Integer.toBinaryString(N);
        int ans = 0;
        int lasone = -1;
        for (int i = bs.length()-1; i >= 0; i--) {
            if
(bs.charAt(i) == '0') continue; if (lasone == -1) { lasone = i; } else { ans = Math.max(ans, lasone - i); lasone = i; } } return ans; } }

869. Reordered Power of 2

  一個數字各個位的數重排後能不能變成2的冪,注意不能有前導0,也就是說類似10是不能重排成01的。 起始考慮將數字重排是比較複雜,換個思路,2的次方數起始是很少的,早10^9次方內也就30個,所以只需要拿這30個數來和N比較,看他們有沒有相同個數的數字。

class Solution {
    public boolean reorderedPowerOf2(int N) {
        if (N == 1)
            return true;
        int num = 2;
        while (num <= 1000000000) {
            if (isSame(num, N)) {
                return true;
            }
            num = num<<1;
        }
        return false
; } private boolean isSame(int x, int y) { int[] cnt1 = new int[10]; int[] cnt2 = new int[10]; while (x != 0) { cnt1[x%10]++; x /= 10; } while (y != 0) { cnt2[y%10]++; y /= 10; } for (int i = 0; i < 10; i++) { if (cnt1[i] != cnt2[i]) return false; } return true; } }

870. Advantage Shuffle

  起始就是hdoj 1502田忌賽馬,但要求的結果不一樣而已。這裡我用了個pair來記錄B中每個數字對應的位置。

import java.util.Arrays;
import java.util.Comparator;

class Solution {
    private class Pair {
        public int i;
        public int v;
        public Pair(int x, int y) {
            this.i = x;
            this.v = y;
        }
    }
    public int[] advantageCount(int[] A, int[] B) {
        Pair[] pairs = new Pair[B.length];
        for (int i = 0; i < B.length; i++) {
            pairs[i] = new Pair(i, B[i]);
        }
        Arrays.sort(A);
        Arrays.sort(pairs, new Comparator<Pair>() {
            @Override
            public int compare(Pair o1, Pair o2) {
                return o1.v - o2.v;
            }
        });
        int n = A.length;
        int[] res = new int[n];

        for(int ts = 0, tf = n-1, ks = 0, kf = n-1; kf >= ks || tf >= ts; ) {
            if(A[ts] > pairs[ks].v){//比較最慢的馬,能贏就贏
                res[pairs[ks].i] = A[ts];
                ts++;
                ks++;
            }
            else if(A[ts] < pairs[ks].v){//不能贏,就用田忌最慢的消耗齊王最快的
                res[pairs[kf].i] = A[ts];
                ts++;
                kf--;
            }

            else if(A[tf] > pairs[kf].v){//比較雙方最快的馬,能贏就贏
                res[pairs[kf].i] = A[tf];
                tf--;
                kf--;
            }
            else if(A[tf] < pairs[kf].v){//不能贏就用田忌最慢的馬消耗齊王最快的馬
                res[pairs[kf].i] = A[ts];
                ts++;
                kf--;
            }
            else if(A[ts] < pairs[kf].v){//拿田忌最慢的和齊王最快的比,如果慢,就消耗
                res[pairs[kf].i] = A[ts];
                ts++;
                kf--;
            }
            else{//如果一樣,後邊都是平局
                res[pairs[kf].i] = A[ts];
                ts++;
                kf--;
            }

        }
        return res;
    }
}