LeetCode每日一练

2021/10/29【335. 路径交叉】(H)

https://leetcode-cn.com/problems/self-crossing/
在这里插入图片描述

// 哈哈这道题一看下来,这模拟不就完了,虽然觉得必会超时,但是还是还是先莽了,但是我看到超时的样例的时候我才突然幡然醒悟,这捏是贪吃蛇用例呀,最后放下自己的原本真~模拟的思路,和我一样的同学可以翻到后面笑下哈哈哈
// 言归正传,根据贪吃蛇用例,我们可以突然想到其实这个题可以反向来解,什么时候才会相交呢,顺着如何相交的思路的ac了,这个相交的思路其实就是把所有相交的可能想全并把这些边的映射关系缕清就完了,后面附个图
public boolean isSelfCrossing(int[] distance) {
        if (distance.length < 4) {
            return false;
        }
        for (int i = 3; i < distance.length; i++) {
            if (distance[i] >= distance[i-2] && distance[i-3]>=distance[i-1]) {
                return true;
            }
            if (i >= 4 && distance[i-3] == distance[i-1] && (distance[i-4]+distance[i]) >= distance[i-2]) {
                return true;
            }
            if (i >= 5  && distance[i-2] > distance[i] && distance[i-2] >distance[i-4] && distance[i-5] < distance[i-3]
                    && (distance[i-4]+distance[i]) >= distance[i-2] && (distance[i-1]+distance[i-5]) >= distance[i-3] ) {
                return true;
            }
        }
        return false;
    }

// 这是一开始直接思路开始莽的代码
public boolean isSelfCrossing(int[] distance) {
        int[][] local = new int[][]{{0, 1}, {-1, 0}, {0, -1}, {1, 0}};
        Map<String, Integer> pos = new HashMap<>();
        int x = 0;
        int y = 0;
        pos.put(getKey(x, y), 1);
        for (int i = 0; i < distance.length; i++) {
            for (int j = 1; j <= distance[i]; j++) {
                x = x+local[i%4][0];
                y = y+local[i%4][1];
                String key = getKey(x, y);
                if (pos.get(key) != null && pos.get(key) == 1) {
                    return true;
                }
                pos.put(key, 1);
            }
        }
        return false;
    }
    private String getKey(int x, int y) {
        return x+"_"+y;
    }


2021/10/28【869. 重新排序得到 2 的幂】(M)

https://leetcode-cn.com/problems/reordered-power-of-2/

这里想的是将要确定的数与这个数在一定范围内的2的幂做比较
比如 10 就是我会将101-100之间的所有的2的幂作比较,比较的方式就是对两个数的各个位都进行统计然后将两个数的数位统计结果进行比较
public boolean reorderedPowerOf2(int n) {
        Map<Integer, Integer> map = getMap(n);
        int nNum = 0;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            nNum = nNum + entry.getValue();
        }

        int num = 1;
        for(int i = 1; i <= 32; i++) {
            if (num >= (int) Math.pow(10, nNum)) {
                break;
            }
            Map<Integer, Integer> map2 = getMap(num);
            if (num == n) {
                return true;
            }
            if (map.size() == map2.size()) {
                boolean flag = true;
                for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
                    if (map2.get(entry.getKey()) == null || !map2.get(entry.getKey()).equals(entry.getValue())) {
                        flag = false;
                        break;
                    }
                }
                if (flag == true) {
                    return true;
                }
            }
            num = num * 2;
        }
        return false;
    }
    private Map<Integer, Integer> getMap(int copyN) {
        Map<Integer, Integer> map = new HashMap<>();
        while(copyN != 0) {
            if (map.get(copyN%10) == null) {
                map.put(copyN%10, 1);
            }else{
                map.put(copyN%10, map.get(copyN%10)+1);
            }
            copyN = copyN / 10;
        }
        return map;
    }

2021/09/10【1894. 找到需要补充粉笔的学生编号】

题目链接:https://leetcode-cn.com/problems/find-the-student-that-will-replace-the-chalk/

2021/09/09【68. 文本左右对齐】(H)

题目链接:https://leetcode-cn.com/problems/text-justification/
记录:一开始没有用StringBuilder,用的String,整个ac的时长为十几毫秒,后面换成了StringBuilder现在耗时一毫秒

class Solution {
    public List<String> fullJustify(String[] words, int maxWidth) {
        int i = 0;
        int nextNum = 0;
        List<String> wordList = new ArrayList<>();
        List<String> ans = new ArrayList<>();
        while(i < words.length) {
            if (nextNum == 0) {
                nextNum =words[i].length();
            }else {
                nextNum = nextNum + words[i].length() + 1;
            }
            if(nextNum > maxWidth) {
                    StringBuilder perStr = new StringBuilder("");
                    if (wordList.size() == 1) {
                        perStr.append(wordList.get(0));
                        while(perStr.length() < maxWidth) {
                            perStr.append(" ");
                        }
                    }else{
                        int avgSpaceNum = (maxWidth - nextNum + words[i].length() +wordList.size() )/(wordList.size()-1);
                        int overSpaceNum = (maxWidth - nextNum + words[i].length() +wordList.size() )%(wordList.size()-1);
                        String avgSpaceStr = "";
                        for (int j = 0; j< avgSpaceNum; j++) {
                            avgSpaceStr = avgSpaceStr + " ";
                        }
                        for (int j = 0; j < wordList.size(); j++) {
                            if (j == 0) {
                                perStr.append(wordList.get(j));
                            }else if (j <= overSpaceNum) {
                                perStr.append(avgSpaceStr).append(" ").append(wordList.get(j));
                            }else{
                                perStr.append(avgSpaceStr).append(wordList.get(j));
                            }
                        }
                    }
                    ans.add(perStr.toString());
                wordList.clear();
                nextNum = words[i].length();
            }
            wordList.add(words[i]);
            i++;
            if (i == words.length){
                StringBuilder perStr = new StringBuilder("");
                for (int j = 0; j<wordList.size();j++) {
                    if (j == 0) {
                        perStr.append(wordList.get(j));
                    }else {
                        perStr.append(" ").append(wordList.get(j));
                    }
                }
                while(perStr.length() <maxWidth){
                    perStr.append(" ");
                }
                ans.add(perStr.toString());
            }
        }
        return ans;
    }
}

2021/09/08【502. IPO】

题目链接:https://leetcode-cn.com/problems/ipo/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值