• Day37 LeetCode


    1. 外星语言是否排序

    某种外星语也使用英文小写字母,但可能顺序 order 不同。字母表的顺序(order)是一些小写字母的排列。

    给定一组用外星语书写的单词 words,以及其字母表的顺序 order,只有当给定的单词在这种外星语中按字典序排列时,返回 true;否则,返回 false。

    分析:

    遍历words,挨个进行比较,有一个错的就直接返回false。

    class Solution {
        public boolean isAlienSorted(String[] words, String order) {
            if (words.length == 0) return true;
            for (int i = 0; i < words.length-1; i++) {
                int j = 0, k = 0;
                while (j < words[i].length() && k < words[i+1].length()){
                    if (order.indexOf(words[i].charAt(j)) > order.indexOf(words[i+1].charAt(k))){
                        return false;
                    }
                    if (order.indexOf(words[i].charAt(j)) < order.indexOf(words[i+1].charAt(k))){
                        break;
                    }
                    k++;
                    j++;
                }
                if (j<words[i].length() && k == words[i+1].length()) return false;
            }
            return true;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    2. 最小时间差

    给定一个 24 小时制(小时:分钟 “HH:MM”)的时间列表,找出列表中任意两个时间的最小时间差并以分钟数表示。

    分析:

    把所有时间都转化为分钟制,然后对时间列表排序,遍历数组比较相邻项之差,最后要记得比较第一个和最后一个之差。

    class Solution {
        public int findMinDifference(List<String> timePoints) {
            Collections.sort(timePoints);
            int cur = countMins(timePoints.get(0));
            int pre = 0;
            int res = Integer.MAX_VALUE;
            for (int i = 1; i < timePoints.size(); i++) {
                pre = cur;
                cur = countMins(timePoints.get(i));
                res = Math.min((cur - pre), res);
            }
            res = Math.min(res, countMins(timePoints.get(0))+1440-cur);
            return res;
    
    
        }
    
        public int countMins(String timePoint){
            return ((timePoint.charAt(0)-'0')*10 + (timePoint.charAt(1)-'0'))*60 + ((timePoint.charAt(3)-'0')*10 + (timePoint.charAt(4)-'0'));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    3. 后缀表达式

    根据 逆波兰表示法,求该后缀表达式的计算结果。

    有效的算符包括 +、-、*、/ 。每个运算对象可以是整数,也可以是另一个逆波兰表达式。

    分析:

    用栈模拟。

    class Solution {
        public int evalRPN(String[] tokens) {
            Stack<Integer> stack = new Stack<>();
            for (String c : tokens) {
                switch (c) {
                    case "+":
                    case "-":
                    case "*":
                    case "/":
                        int num2 = stack.pop();
                        int num1 = stack.pop();
                        stack.push(calc(num1, num2, c));
                        break;
                    default:
                        stack.push(Integer.parseInt(c));
                }
            }
            return stack.pop();
        }
    
        private int calc(int a, int b, String op) {
            switch (op) {
                case "+":
                    return a + b;
                case "-":
                    return a - b;
                case "*":
                    return a * b;
                default:
                    return a / b;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    4. 小行星碰撞

    给定一个整数数组 asteroids,表示在同一行的小行星。

    对于数组中的每一个元素,其绝对值表示小行星的大小,正负表示小行星的移动方向(正表示向右移动,负表示向左移动)。每一颗小行星以相同的速度移动。

    找出碰撞后剩下的所有小行星。碰撞规则:两个行星相互碰撞,较小的行星会爆炸。如果两颗行星大小相同,则两颗行星都会爆炸。两颗移动方向相同的行星,永远不会发生碰撞。

    分析:

    class Solution {
        public int[] asteroidCollision(int[] asteroids) {
            Deque<Integer> stack = new ArrayDeque<Integer>();
            for (int aster : asteroids) {
                boolean alive = true;
                while (alive && aster < 0 && !stack.isEmpty() && stack.peek() > 0) {
                    alive = stack.peek() < -aster; // aster 是否存在
                    if (stack.peek() <= -aster) {  // 栈顶小行星爆炸
                        stack.pop();
                    }
                }
                if (alive) {
                    stack.push(aster);
                }
            }
            int size = stack.size();
            int[] ans = new int[size];
            for (int i = size - 1; i >= 0; i--) {
                ans[i] = stack.pop();
            }
            return ans;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    逐步求和得到正数的最小值

    给你一个整数数组 nums 。你可以选定任意的 正数 startValue 作为初始值。

    你需要从左到右遍历 nums 数组,并将 startValue 依次累加上 nums 数组中的值。

    请你在确保累加和始终大于等于 1 的前提下,选出一个最小的 正数 作为 startValue 。

    分析:

    遍历数组求出数组的最小前缀和min,如果min大于等于0则startValue=1;如果min小于0则startValue=1-min。

    class Solution {
        public int minStartValue(int[] nums) {
            int min = Integer.MAX_VALUE;
            int sum = 0, res = 0;
            for (int i = 0; i < nums.length; i++) {
                sum += nums[i];
                if (sum<min) min = sum;
            }
            if (min>=0){
                res = 1;
            }else {
                res = 1 - min;
            }
            return res;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
  • 相关阅读:
    win或安卓通过内网穿透(frp)远控Mac的配置指南
    医院导航解决方案,医院导诊图怎么制作?哪里可以做?
    【地图之vue-baidu-map】点击获取坐标(点Marker)、坐标集(多边形polygon)
    Python中的运算符
    TIM1计数模式
    SRS、ZLMediakit音视频流媒体服务器
    513找树左下角值
    跟着 Guava、Spring 学习如何设计观察者模式
    动态规划:06不同路径II
    计算地球上两点间的方位角和俯仰角【输入经纬度】
  • 原文地址:https://blog.csdn.net/weixin_49546933/article/details/126255145