欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

LeetCode 第14双周赛

程序员文章站 2022-07-12 12:09:01
...

T1:十六进制魔术数字

解题思路:就直接按照题意来,先将字符串转换为16进制,然后在根据题目进行判断(这里偷了个懒,直接利用了java库函数????)

import java.math.BigInteger;
class Solution {
    public String toHexspeak(String num) {
        BigInteger bigInteger = new BigInteger(num,10);
        String s = bigInteger.toString(16);
        StringBuilder sb = new StringBuilder();
        for (char a:s.toCharArray()) {
            if (a=='0') a='O';
            if (a=='1') a= 'I';
            if (a>='2' && a<='9')return "ERROR";
            sb.append(a);
        }
        return sb.toString().toUpperCase();
    }
}

T2:删除区间

解题思路:题意就是要求将与toBeRemoved区间范围内的部分都删除,用例已经提示的十分清楚了。其实就是一个分类讨论的题目。

    public List> removeInterval(int[][] intervals, int[] toBeRemoved) {
        int n = intervals.length;
        List<List<Integer>> res = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            if (intervals[i][1]<=toBeRemoved[0] || intervals[i][0]>=toBeRemoved[1]){
                ArrayList<Integer> tmp = new ArrayList<>();
                tmp.add(intervals[i][0]);
                tmp.add(intervals[i][1]);
                res.add(tmp);
            }else if (intervals[i][1]>toBeRemoved[0] && intervals[i][0]<=toBeRemoved[0] && toBeRemoved[1]>intervals[i][1]){
                ArrayList<Integer> tmp = new ArrayList<>();
                tmp.add(intervals[i][0]);
                tmp.add(toBeRemoved[0]);
                res.add(tmp);
            }else if (intervals[i][0]<toBeRemoved[1] && intervals[i][0]>=toBeRemoved[0] && toBeRemoved[1]<intervals[i][1]){
                ArrayList<Integer> tmp = new ArrayList<>();
                tmp.add(toBeRemoved[1]);
                tmp.add(intervals[i][1]);
                res.add(tmp);
            }else if (intervals[i][0]<=toBeRemoved[0] && intervals[i][1]>=toBeRemoved[1]){
                ArrayList<Integer> tmp1 = new ArrayList<>();
                tmp1.add(intervals[i][0]);
                tmp1.add(toBeRemoved[0]);
                res.add(tmp1);
                ArrayList<Integer> tmp2 = new ArrayList<>();
                tmp2.add(toBeRemoved[1]);
                tmp2.add(intervals[i][1]);
                res.add(tmp2);
            }
        }
        return res; 
    }

T3: