leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval
lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的interval都可以保留。我们只需要两个指针,i&j分别保存重叠interval中最早start和最晚end即可,然后将interval [i, j]插入即可
class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
List<int[]> res = new ArrayList<>();
for(int[] interval : intervals){
if(interval[1] < newInterval[0]){
res.add(interval);
}
else if(interval[0] > newInterval[1]){
res.add(newInterval);
newInterval = interval;
}
else{
newInterval[0] = Math.min(newInterval[0], interval[0]);
newInterval[1] = Math.max(newInterval[1], interval[1]);
}
}
res.add(newInterval);
return res.toArray(new int[0][0]);
}
}
lc1046 Last Stone Weight 用优先队列解,弹出两个最大值,比较他们的差值,然后按题目描述操作即可。
注意java自带的PriorityQueue是自然顺序,别忘了改成降序, PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());
class Solution {
public int lastStoneWeight(int[] stones) {
PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());
for(int stone :stones){
pq.offer(stone);
}
while(pq.size() > 1){
int max1 = pq.poll();
int max2 = pq.poll();
if(max1 != max2)
pq.offer(max1 - max2);
}
return pq.size() > 0 ? pq.peek() : 0;
}
}
lc1047 Remove All Adjacent Duplicates in String
1)用stack,创建一个与原数组(题目输入为String S,访问某一index时别忘了用charAt(),或者直接新建一个char数组,用S.toChar()把S转成char[])同样大小的数组,两个index,i&j,i用来遍历原数组,j用来处理stack。判断栈顶元素是否与当前i指向元素相等,相等就j--,否则就给stack[j]赋值为SChar[i]并继续loop
2)双指针,一个正常遍历原数组的索引i,另一个模拟stack操作,但是不需要额外申请空间,直接指向原数组的指针j。注意初值要设成-1,否则不方便初始化。其它逻辑同1)。返回值就是原数组的0-i。所以要注意长度应该设成i+1,new String(Schar, 0, i+1)
给出双指针版代码
class Solution {
public String removeDuplicates(String S) {
int len = S.length();
char[] tmp = new char[len];
int i=0;
for(int j=0; j<len; j++){
if(i>0 && tmp[i-1] == S.charAt(j))
i--;
else
tmp[i++] = S.charAt(j);
}
return new String(tmp, 0, i);
}
}
lc56 Merge Interval
贪心,将interval按照start排好序,按顺序合并overlap的interval 由于题目给的是int[][],int[][0]是start,int[][1]是end。先对start排序,然后对end也排序,为什么?end排完序不就没法和其原本的start匹配了吗?仔细想一想,overlap的interval打乱顺序对最终的merge有影响吗?其实没有,画个图就明白了
class Solution {
public int[][] merge(int[][] intervals) {
int m = intervals.length;
int[] head = new int[m];
int[] tail = new int[m];
for(int i=0; i<m; i++){
head[i] = intervals[i][0];
tail[i] = intervals[i][1];
}
Arrays.sort(head);
Arrays.sort(tail);
List<int[]> res = new ArrayList<>();
//int[] tmp = new int[2];
for(int i=0, j=0, loop = 0; i<m; i++){
//int[] tmp = new int[2];
if(i == m-1 || head[i+1] > tail[i]){
tmp[0] = head[j];
tmp[1] = tail[i];
//res.add(new int[]{tmp[0], tmp[1]});
res.add(tmp);
j = i+1;
}
}
int[][] ans = new int[res.size()][2];
return res.toArray(ans);
}
}
leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval的更多相关文章
- 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...
- LeetCode 1047. Remove All Adjacent Duplicates In String
1047. Remove All Adjacent Duplicates In String(删除字符串中的所有相邻重复项) 链接:https://leetcode-cn.com/problems/r ...
- 【leetcode】1047. Remove All Adjacent Duplicates In String
题目如下: Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent a ...
- LeetCode 1047. 删除字符串中的所有相邻重复项(Remove All Adjacent Duplicates In String)
1047. 删除字符串中的所有相邻重复项 1047. Remove All Adjacent Duplicates In String 题目描述 LeetCode1047. Remove All Ad ...
- 【leetcode】1209. Remove All Adjacent Duplicates in String II
题目如下: Given a string s, a k duplicate removal consists of choosing k adjacent and equal letters from ...
- LeetCode 1046. 最后一块石头的重量(1046. Last Stone Weight) 50
1046. 最后一块石头的重量 1046. Last Stone Weight 题目描述 每日一算法2019/6/22Day 50LeetCode1046. Last Stone Weight Jav ...
- 【Leetcode_easy】1046. Last Stone Weight
problem 1046. Last Stone Weight 参考 1. Leetcode_easy_1046. Last Stone Weight; 完
- 56. Merge Interval
56. Merge Interval 0. 参考文献 序号 文献 1 花花酱 LeetCode 56. Merge Intervals 2 [LeetCode] Merge Intervals 合并区 ...
- [LeetCode] 57. Insert Interval 插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
随机推荐
- 如何快速合并多个TXT文本内容
工作中有时候需要合并很多文本内容,例如一些推送清单之类,一个一个打开去复制粘贴的话,少量还行,如果txt文本数据量大(10+M以上)且文件数量多(成百上千),这种方式就显得很低效了.具体要求如下: ...
- 180608发现的一个有趣的Douyin-Bot项目
今日发现的github 项目 Douyin-Bot 抖音机器人发现漂亮小姐姐 :sweat_smile: 在开发者模式下,找到指针位置,开启之后,当点击屏幕时,可以获取点击屏幕处的xy坐标了 我使用的 ...
- jvisualvm图解【转】
jvisualvm图解[转] http://blog.csdn.net/a19881029/article/details/8432368 jvisualvm能干什么:监控内存泄露,跟踪垃圾回收, ...
- python requests 高级用法
高级用法 本篇文档涵盖了 Requests 的一些高级特性. 会话对象 会话对象让你能够跨请求保持某些参数.它也会在同一个 Session 实例发出的所有请求之间保持 cookie, 期间使用 url ...
- 自己整理的一个访问SQLite3数据库的C++类
原文地址:自己整理的一个访问SQLite3数据库的C++类作者:vigra 近日,对SQLite3的使用进行了研究.真不愧是优秀的嵌入数据库,API接口也极其简捷.基本上只要使用以下几个接口就能完成数 ...
- mysql order by排序查询速度问题
SELECT * FROM `assets_message` LEFT JOIN purchase_message ON assets_message.purchase_id = purchase_m ...
- java接口的意义
java当中继承一个接口,要重写他的方法的话,那为什么还要多此一举的去实现一个接口呢? 直接把方法写在类当中不就可以了?就是说去掉类名后面的Implements 接口 ,可以不可以呢? 接口的最主要的 ...
- php析构函数小结
l 基本语法 class 类名{ public function __destruct(){ //函数体 //析构函数的最重要的作用,就是释放对象创建的资源 //比如 数据库连接, 文件句柄, ...
- python 之 字符串处理
分割字符串 根据某个分割符分割 >>> a = '1,2,3,4' >>> a.split(',') ['] 根据多个分隔符分割 >>> line ...
- linux中断处理-顶半部(top half)和底半部(bottom half) -转
原文:http://rensanshi.blog.163.com/blog/static/21395510820136282224877/ 设备的中断会打断内核中进程的正常调度和运行,系统对更高吞吐率 ...