Leetcode Biweekly Contest 2

https://leetcode.com/contest/biweekly-contest-2

A:Sum of Digits in the Minimum Number

给一个数组,求最小数的数字和的奇偶性

import java.util.Arrays;

public class Leetcode1085 {
    public int sumOfDigits(int[] A) {
        Arrays.sort(A);
        int s = 0;
        for (char x : (A[0] + "").toCharArray()) {
            s += x - '0';
        }
        return 1 - s % 2;
    }
}

B:High Five

给了学生的成绩,求每个学生最高五科成绩的平均分

import java.util.Arrays;

/**
 * Created by dezhonger on 2019/6/16
 */
public class Leetcode1086 {
    public int[][] highFive(int[][] items) {
        Arrays.sort(items, (a, b) -> {
            if (a[0] != b[0]) return a[0] - b[0];
            return -(a[1] - b[1]);
        });

        int n = items.length;
        int[][] ret = new int[n][];
        int p = 0;
        for (int i = 0; i < n; ) {
            int j = i;
            while (j < n && items[i][0] == items[j][0]) j++;

            int s = 0;
            int num = 0;
            for (int k = 0; k < 5 && k < j - i; k++) {
                s += items[k + i][1];
                num++;
            }
            ret[p++] = new int[]{items[i][0], s / num};
            i = j;
        }
        return Arrays.copyOf(ret, p);
    }
}

C:Brace Expansion

求可以组成的字符串数组

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Created by dezhonger on 2019/6/16
 */
public class Leetcode1087 {
    public static void main(String[] args) {
//        new Leetcode1087().expand("{a,b}c{d,e}f");
        new Leetcode1087().expand("{a}{a}{a}");
    }
    public String[] expand(String S) {
        for (int i = 0; i < S.length();) {
            if (S.charAt(i) == '{') {
                List<String> list = new ArrayList<>();
                int st = i + 1;
                int j = i + 1;
                for (; j < S.length(); j++) {
                    if (S.charAt(j) =='}')break;
                }
                String[] str = S.substring(st, j).split(",");
                list.addAll(Arrays.asList(str));
                r.add(list);
                i = j + 1;
            } else {
                int j = i;
                StringBuilder sb = new StringBuilder();
                for (; j < S.length(); j++) {
                    if (S.charAt(j) == '{') break;
                    sb.append(S.charAt(j));
                }
                List<String> list = new ArrayList<>();
                list.add(sb.toString());
                r.add(list);
                i = j;

            }
        }


        dfs(0, "", r.size());
        Collections.sort(res);
        return res.toArray(new String[res.size()]);
    }

    List<String> res = new ArrayList<>();
    List<List<String>> r = new ArrayList<>();

    void dfs(int index, String tmp, int len) {
        if (index == len) {
            res.add(tmp);
            return;
        }
        List<String> strings = r.get(index);
        for (int i = 0; i < strings.size(); i++) {
            dfs(index + 1, tmp + strings.get(i), len);
        }
    }
}

D:Confusing Number II

求旋转180°后和原数不同的数的个数

数量不多,dfs即可

/**
 * Created by dezhonger on 2019/6/16
 */
public class Leetcode1088 {
    public static void main(String[] args) {
        new Leetcode1088().confusingNumberII(10_0000_0000);
    }

    int res = 0;
    int[] a = new int[]{0, 1, 6, 8, 9};
    int N;

    boolean check(int x) {
        int[] b = new int[]{0, 1, 2, 3, 4, 5, 9, 7, 8, 6};
        int t = 0;
        int cur = x;
        while (x > 0) {
            t = t * 10 + b[x % 10];
            x /= 10;

        }
        return !(cur == t);
    }

    void dfs(int pos, int x) {
        if (x > 0 && check(x)) {
            res++;
        }
        int start = (pos == 0 && x == 0) ? 1 : 0;
        for (int i = start; i < 5; i++) {
            if (1L * x * 10 + a[i] <= N) {
                dfs(pos + 1, x * 10 + a[i]);
            }
        }
    }


    public int confusingNumberII(int N) {
        this.N = N;
        dfs(0, 0);
        return res;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值