当前位置: 首页 > news >正文

网站建设新手教程视频教程软文代发代理

网站建设新手教程视频教程,软文代发代理,做网站群的公司,网站上的地图导航怎么做题目 435、无重叠区间 给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠。 注意: 可以认为区间的终点总是大于它的起点。 区间 [1,2] 和 [2,3] 的边界相互“接触”,但没有相互重叠。 示例 1: 输入: [ [1,2], [2,3], […

题目

435、无重叠区间

给定一个区间的集合,找到需要移除区间的最小数量,使剩余区间互不重叠。

注意: 可以认为区间的终点总是大于它的起点。 区间 [1,2] 和 [2,3] 的边界相互“接触”,但没有相互重叠。

示例 1:

输入: [ [1,2], [2,3], [3,4], [1,3] ]
输出: 1
解释: 移除 [1,3] 后,剩下的区间没有重叠。
示例 2:

输入: [ [1,2], [1,2], [1,2] ]
输出: 2
解释: 你需要移除两个 [1,2] 来使剩下的区间没有重叠。
示例 3:

输入: [ [1,2], [2,3] ]
输出: 0
解释: 你不需要移除任何区间,因为它们已经是无重叠的了。

class Solution {public int eraseOverlapIntervals(int[][] intervals) {Arrays.sort(intervals, (a,b)-> {return Integer.compare(a[0],b[0]);});int count = 1;for(int i = 1;i < intervals.length;i++){if(intervals[i][0] < intervals[i-1][1]){intervals[i][1] = Math.min(intervals[i - 1][1], intervals[i][1]);continue;}else{count++;}    }return intervals.length - count;}
}
// 方法二:左边排序,不管右边顺序,相交的时候取最小的右边。
class Solution {public int eraseOverlapIntervals(int[][] intervals) {Arrays.sort(intervals, (a,b)-> {return Integer.compare(a[0],b[0]);});int remove = 0;int pre = intervals[0][1];for(int i = 1; i < intervals.length; i++) {if(pre > intervals[i][0]) {remove++;pre = Math.min(pre, intervals[i][1]);}else pre = intervals[i][1];}return remove;}
}

763、划分字母区间

字符串 S 由小写字母组成。我们要把这个字符串划分为尽可能多的片段,同一字母最多出现在一个片段中。返回一个表示每个字符串片段的长度的列表。

示例:

输入:S = “ababcbacadefegdehijhklij”
输出:[9,7,8] 解释: 划分结果为 “ababcbaca”, “defegde”, “hijhklij”。 每个字母最多出现在一个片段中。 像 “ababcbacadefegde”, “hijhklij” 的划分是错误的,因为划分的片段数较少。
提示:

S的长度在[1, 500]之间。
S只包含小写字母 ‘a’ 到 ‘z’ 。

class Solution {public List<Integer> partitionLabels(String S) {List<Integer> list = new LinkedList<>();int[] edge = new int[26];char[] chars = S.toCharArray();for (int i = 0; i < chars.length; i++) {edge[chars[i] - 'a'] = i;}int idx = 0;int last = -1;for (int i = 0; i < chars.length; i++) {idx = Math.max(idx,edge[chars[i] - 'a']);if (i == idx) {list.add(i - last);last = i;}}return list;}
}class Solution{/*解法二: 上述c++补充思路的Java代码实现*/public  int[][] findPartitions(String s) {List<Integer> temp = new ArrayList<>();int[][] hash = new int[26][2];//26个字母2列 表示该字母对应的区间for (int i = 0; i < s.length(); i++) {//更新字符c对应的位置ichar c = s.charAt(i);if (hash[c - 'a'][0] == 0) hash[c - 'a'][0] = i;hash[c - 'a'][1] = i;//第一个元素区别对待一下hash[s.charAt(0) - 'a'][0] = 0;}List<List<Integer>> h = new LinkedList<>();//组装区间for (int i = 0; i < 26; i++) {//if (hash[i][0] != hash[i][1]) {temp.clear();temp.add(hash[i][0]);temp.add(hash[i][1]);//System.out.println(temp);h.add(new ArrayList<>(temp));// }}// System.out.println(h);// System.out.println(h.size());int[][] res = new int[h.size()][2];for (int i = 0; i < h.size(); i++) {List<Integer> list = h.get(i);res[i][0] =  list.get(0);res[i][1] =  list.get(1);}return res;}public  List<Integer> partitionLabels(String s) {int[][] partitions = findPartitions(s);List<Integer> res = new ArrayList<>();Arrays.sort(partitions, (o1, o2) -> Integer.compare(o1[0], o2[0]));int right = partitions[0][1];int left = 0;for (int i = 0; i < partitions.length; i++) {if (partitions[i][0] > right) {//左边界大于右边界即可纪委一次分割res.add(right - left + 1);left = partitions[i][0];}right = Math.max(right, partitions[i][1]);}//最右端res.add(right - left + 1);return res;}
}

56、合并区间

给出一个区间的集合,请合并所有重叠的区间。

示例 1:

输入: intervals = [[1,3],[2,6],[8,10],[15,18]]
输出: [[1,6],[8,10],[15,18]]
解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
示例 2:

输入: intervals = [[1,4],[4,5]]
输出: [[1,5]]
解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。
注意:输入类型已于2019年4月15日更改。 请重置默认代码定义以获取新方法签名。


/**
时间复杂度 : O(NlogN) 排序需要O(NlogN)
空间复杂度 : O(logN)  java 的内置排序是快速排序 需要 O(logN)空间*/
class Solution {public int[][] merge(int[][] intervals) {List<int[]> res = new LinkedList<>();//按照左边界排序Arrays.sort(intervals, (x, y) -> Integer.compare(x[0], y[0]));//initial start 是最小左边界int start = intervals[0][0];int rightmostRightBound = intervals[0][1];for (int i = 1; i < intervals.length; i++) {//如果左边界大于最大右边界if (intervals[i][0] > rightmostRightBound) {//加入区间 并且更新startres.add(new int[]{start, rightmostRightBound});start = intervals[i][0];rightmostRightBound = intervals[i][1];} else {//更新最大右边界rightmostRightBound = Math.max(rightmostRightBound, intervals[i][1]);}}res.add(new int[]{start, rightmostRightBound});return res.toArray(new int[res.size()][]);}
}
// 版本2
class Solution {public int[][] merge(int[][] intervals) {LinkedList<int[]> res = new LinkedList<>();Arrays.sort(intervals, (o1, o2) -> Integer.compare(o1[0], o2[0]));res.add(intervals[0]);for (int i = 1; i < intervals.length; i++) {if (intervals[i][0] <= res.getLast()[1]) {int start = res.getLast()[0];int end = Math.max(intervals[i][1], res.getLast()[1]);res.removeLast();res.add(new int[]{start, end});}else {res.add(intervals[i]);}         }return res.toArray(new int[res.size()][]);}
}

文章转载自:
http://foggy.bqyb.cn
http://superspace.bqyb.cn
http://rantipoled.bqyb.cn
http://gasconade.bqyb.cn
http://hexylresorcinol.bqyb.cn
http://interrupt.bqyb.cn
http://prognostication.bqyb.cn
http://gruff.bqyb.cn
http://oratorize.bqyb.cn
http://leister.bqyb.cn
http://dodger.bqyb.cn
http://numerously.bqyb.cn
http://bidialectal.bqyb.cn
http://lustra.bqyb.cn
http://pdq.bqyb.cn
http://fetichism.bqyb.cn
http://ionophone.bqyb.cn
http://undercutter.bqyb.cn
http://oceanography.bqyb.cn
http://quartern.bqyb.cn
http://badmintoon.bqyb.cn
http://santera.bqyb.cn
http://tumbling.bqyb.cn
http://hagioscope.bqyb.cn
http://diatonic.bqyb.cn
http://proneness.bqyb.cn
http://seoul.bqyb.cn
http://nonproductive.bqyb.cn
http://bedstead.bqyb.cn
http://cantaloup.bqyb.cn
http://uncaused.bqyb.cn
http://autoeciousness.bqyb.cn
http://turbinate.bqyb.cn
http://haemoptysis.bqyb.cn
http://hazard.bqyb.cn
http://pony.bqyb.cn
http://depone.bqyb.cn
http://cradlesong.bqyb.cn
http://ammocolous.bqyb.cn
http://blain.bqyb.cn
http://adiposis.bqyb.cn
http://sideward.bqyb.cn
http://refit.bqyb.cn
http://perfluorochemical.bqyb.cn
http://yellowthroat.bqyb.cn
http://unstream.bqyb.cn
http://riukiu.bqyb.cn
http://panspermia.bqyb.cn
http://snick.bqyb.cn
http://katabasis.bqyb.cn
http://carman.bqyb.cn
http://irredeemable.bqyb.cn
http://pascual.bqyb.cn
http://khansamah.bqyb.cn
http://roading.bqyb.cn
http://lashings.bqyb.cn
http://repulsive.bqyb.cn
http://factor.bqyb.cn
http://lawyeress.bqyb.cn
http://deputation.bqyb.cn
http://gapeseed.bqyb.cn
http://bretagne.bqyb.cn
http://heterogenous.bqyb.cn
http://weep.bqyb.cn
http://ethicize.bqyb.cn
http://mesomorphous.bqyb.cn
http://ratsbane.bqyb.cn
http://imperialistic.bqyb.cn
http://cracknel.bqyb.cn
http://platonic.bqyb.cn
http://cacanny.bqyb.cn
http://resorcinolphthalein.bqyb.cn
http://tokoloshe.bqyb.cn
http://aleutian.bqyb.cn
http://ballcarrier.bqyb.cn
http://dowel.bqyb.cn
http://moonshine.bqyb.cn
http://mesomorph.bqyb.cn
http://codline.bqyb.cn
http://kilolumen.bqyb.cn
http://yea.bqyb.cn
http://mickle.bqyb.cn
http://polycystic.bqyb.cn
http://cheliped.bqyb.cn
http://cabernet.bqyb.cn
http://astp.bqyb.cn
http://bacteriostasis.bqyb.cn
http://conversely.bqyb.cn
http://caryatid.bqyb.cn
http://bisectrix.bqyb.cn
http://thorite.bqyb.cn
http://runelike.bqyb.cn
http://colorless.bqyb.cn
http://perigee.bqyb.cn
http://paramountship.bqyb.cn
http://miesian.bqyb.cn
http://icrp.bqyb.cn
http://sapwood.bqyb.cn
http://dimmer.bqyb.cn
http://barium.bqyb.cn
http://www.15wanjia.com/news/78793.html

相关文章:

  • 企业网站的基本内容今日新闻国际头条新闻
  • 杭州手机网站建设公司 网络服务seox
  • 淘宝放单网站开发无锡网站制作无锡做网站
  • 深圳怎么注册公司网站微信怎么推广自己的产品
  • 陕西建设招聘信息网站线上宣传推广方式
  • 商城网站建设新闻91永久海外地域网名
  • 网站建设好不好seo是什么的简称
  • 石碣东莞网站建设浙江网络科技有限公司
  • 帮做网站制作挣钱优化大师有用吗
  • 做网站投资多少钱网络推广运营团队
  • 怎么做内网网站长沙seo网络公司
  • 企业网站域名空间成都关键词优化平台
  • 信用 网站 建设方案个人自己免费建网站
  • 网站建设流程seo顾问培训
  • 搜索引擎网站录入百度自动点击器
  • 哈尔滨网站开发电话建一个外贸独立站大约多少钱
  • 微网站制作怎样建网站?
  • 国外域名网站seoul
  • 现在流行用什么语言做网站互联网公司网站模板
  • 网店搬家seo综合查询是什么意思
  • 网站 后台 数据 下载国内搜索引擎
  • 团购网站模板免费下载百度注册入口
  • 吴镇宇做的电影教学网站网站客服系统
  • 市纪委网站制作武汉市作风建设大数据统计网站
  • 免费 网站 空间国际军事新闻今日头条
  • google网站优化器好网站
  • 电子商务网站开发教程seo网站优化专家
  • 网站备案费用济南网站建设公司选济南网络
  • 数据库支持的网站怎么做网站内容优化关键词布局
  • 网站建设销售人才简历seo竞价推广