当前位置:网站首页>Day37 LeetCode
Day37 LeetCode
2022-08-10 07:48:00 【太阳在坠落】
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;
}
}
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'));
}
}
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;
}
}
}
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;
}
}
逐步求和得到正数的最小值
给你一个整数数组 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;
}
}
边栏推荐
- 浅谈C语言实现冒泡排序
- 同步锁synchronized追本溯源
- IDLE开发wordCount程序(第五弹)
- 3.1-3.3 读书笔记
- PHP笔记 28 29 30 31
- Introduction to the delta method
- Big guy, when Oracle single-table incremental synchronization, the source database server takes up nearly 2g of memory. This is not normal, right?
- Using the color picker
- 个人博客系统
- .NET-8. My Thought Notes
猜你喜欢

WooCommerce installation and rest api usage

IDLE development wordCount program (5)

PLSQL学习第三天

力扣(LeetCode)221. 最大正方形(2022.08.09)

2022-08-01 网工进阶(二十三) VLAN高级技术-VLAN聚合、MUX VLAN

Using the color picker

2022-08-01 网工进阶(二十四) STP进阶知识

Based on STC8G2K64S4 single-chip microcomputer to display analog photosensitive analog value through OLED screen

iwemeta元宇宙:一个娃娃卖9999元,泡泡玛特认为一点也不贵

协同工具满足70%-90%的工作需求,成为企业香饽饽
随机推荐
Introduction to the delta method
34. Talk about why you want to split the database?What methods are there?
Quickly enter the current date and time
DGIOT 30 million meters set pressure reading
概率分布及其应用
MySQL设置初始密码—注意版本mysql-8.0.30
【MySQL】SQL语句
u-boot ERROR: Failed to allocate 0x5c6f bytes below 0x17ffffff.Failed using fdt_high value
CV+Deep Learning - network architecture Pytorch recurrence series - classification (3: MobileNet, ShuffleNet)
Process management (dynamic)
杭州公积金修改手机号信息
oracle业务表的数据发生增删改,该表的索引会写redo,undo吗?
IDLE development wordCount program (5)
Rust学习:6.1_复合类型之切片
机器人控制器编程实践指导书旧版-实践一 LED灯(数字量)
If the data of the oracle business table is added, deleted, or modified, will the index of the table write redo and undo?
Chapter 11 Database Design Specifications [2. Index and Tuning] [MySQL Advanced]
每日一题,数组字符串的匹配问题
uni 小程序腾讯地图polygon背景透明度
day16--抓包工具Charles的使用