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的更多相关文章

  1. 【LeetCode】1047. Remove All Adjacent Duplicates In String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 日期 题目地址:https://leetcode ...

  2. LeetCode 1047. Remove All Adjacent Duplicates In String

    1047. Remove All Adjacent Duplicates In String(删除字符串中的所有相邻重复项) 链接:https://leetcode-cn.com/problems/r ...

  3. 【leetcode】1047. Remove All Adjacent Duplicates In String

    题目如下: Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent a ...

  4. LeetCode 1047. 删除字符串中的所有相邻重复项(Remove All Adjacent Duplicates In String)

    1047. 删除字符串中的所有相邻重复项 1047. Remove All Adjacent Duplicates In String 题目描述 LeetCode1047. Remove All Ad ...

  5. 【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 ...

  6. LeetCode 1046. 最后一块石头的重量(1046. Last Stone Weight) 50

    1046. 最后一块石头的重量 1046. Last Stone Weight 题目描述 每日一算法2019/6/22Day 50LeetCode1046. Last Stone Weight Jav ...

  7. 【Leetcode_easy】1046. Last Stone Weight

    problem 1046. Last Stone Weight 参考 1. Leetcode_easy_1046. Last Stone Weight; 完

  8. 56. Merge Interval

    56. Merge Interval 0. 参考文献 序号 文献 1 花花酱 LeetCode 56. Merge Intervals 2 [LeetCode] Merge Intervals 合并区 ...

  9. [LeetCode] 57. Insert Interval 插入区间

    Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...

随机推荐

  1. C# 封装首页、上一页、下一月、尾页处理器

    public void BtnPageClickEvent(object sender,string focusForeground,string lostFocusForeground) { But ...

  2. 半宿了,仿写了个CList模板类,留着以后用吧

    难题还是很多阿.模板类.本身就是个问题 要考虑到各个方面,哎.问题很多 比如,如果模板类型为数值型.指针型数据,倒也可以 但是如果模板类型为聚合型,就麻烦了,判断.比较,什么乱七八糟的,都麻烦了. 哎 ...

  3. Pthread spinlock自旋锁

    锁机制(lock) 是多线程编程中最常用的同步机制,用来对多线程间共享的临界区(Critical Section) 进行保护. Pthreads提供了多种锁机制,常见的有:1) Mutex(互斥量): ...

  4. Leetcode931. Minimum Falling Path Sum下降路径最小和

    给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以从第一行中的任何元素开始,并从每一行中选择一个元素.在下一行选择的元素和当前行所选元素最多相隔一列. 示例: 输入:[ ...

  5. 【daydayup】ceshuChat

    时时当勉励,岁月不待人.珍惜时间伐~ 先看看项目运行的效果 这个是在本地环境打开了两个8080端口进行模拟运行的. 先放下作者的开源地址:https://github.com/ceshu/ceshuC ...

  6. react+redux+react-redux练习项目

    一,项目目录 二.1.新建pages包,在pages中新建TodoList.js: 2.新建store包,在store包中新建store.js,reducer.js,actionCreater.js, ...

  7. HTML-完美解决父子元素的外边距重叠和高度塌陷问题

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. csp-s模拟测试10.1(b)X 国的军队,排列组合, 回文题解

    题面:https://www.cnblogs.com/Juve/articles/11615883.html X 国的军队: 好像有O(T*N)的直接贪心做法 其实多带一个log的二分也可以过 先对所 ...

  9. bootStrap-treeview插件

    简要教程 bootstrap-treeview是一款效果非常酷的基于bootstrap的jQuery多级列表树插件.该jQuery插件基于Twitter Bootstrap,以简单和优雅的方式来显示一 ...

  10. LUOGUP3498 [POI2010]KOR-Beads (哈希)

    传送门 解题思路 这是一道上周的考试题...当时考的时候看了一眼,"呀,这不是调和级数,nlogn么!!!" ,然后一写就写了个n^2的....结果边界还弄错40分滚蛋了..正解就 ...