一串首尾相连的珠子(m 个),有N 种颜色(N<=10),设计一个算法,取出其中一段,要求包含所有N 中颜色,并使长度最短。
2014-07-25 19:51
666 查看
转换:将颜色用数字表示出来,比如五种颜色可以用0到4表示,题目就演化为由N个数字
毫无顺序的连成一串,数字串长度为m,在这长度为m的数字串中,取出一段,包含
所有N个数字,且使之长度最短。
分析:可以定义一长度为N的数组,color[0]...color[N-1],分别表示N种颜色,
从头开始遍历数字串m,将读到数字的位置按照“颜色”存入color数组的下标中,且下标
只保存最新位置,直到集齐N种color,则临时最短长度shortestLength=color最大
下标-color最小下标+1,继续遍历数字串,当再一次读到最小下标颜色时,更新最小长度,
直到得出最短长度。
代码如下:
运行结果如下:
毫无顺序的连成一串,数字串长度为m,在这长度为m的数字串中,取出一段,包含
所有N个数字,且使之长度最短。
分析:可以定义一长度为N的数组,color[0]...color[N-1],分别表示N种颜色,
从头开始遍历数字串m,将读到数字的位置按照“颜色”存入color数组的下标中,且下标
只保存最新位置,直到集齐N种color,则临时最短长度shortestLength=color最大
下标-color最小下标+1,继续遍历数字串,当再一次读到最小下标颜色时,更新最小长度,
直到得出最短长度。
代码如下:
package T40; public class getShortestFullColor { /** * @PLA 一串首尾相连的珠子(m 个),有N 种颜色(N<=10), 设计一个算法,取出其中一段,要求包含所有N 中颜色,并使长度最短。 * 并分析时间复杂度与空间复杂度。 */ /** * 将颜色用数字表示出来,比如五种颜色可以用0到4表示,题目就演化为由N个数字 * 毫无顺序的连成一串,数字串长度为m,在这长度为m的数字串中,取出一段,包含 所有N个数字,且使之长度最短。 */ /* * 分析: 可以定义一长度为N的数组,color[0]...color[N-1],分别表示N种颜色, * 从头开始遍历数字串m,将读到数字的位置按照“颜色”存入color数组的下标中,且下标 * 只保存最新位置,直到集齐N种color,则临时最短长度shortestLength=color最大 * 下标-color最小下标+1,继续遍历数字串,当再一次读到最小下标颜色时,更新最小长度, 直到得出最短长度。 */ private static int[] color = new int[6];// color[0]不用 private static int[] Pearl = new int[] { 1, 3, 4, 4, 5, 3, 2, 1 }; static int begin; static int over; private int shortestLength = 0; // 获取color数组最小位置 public int getMin(int[] color) { if (color == null) { return -1; } int temp = color[1];// 由于color[0]空置不用 for (int i = 1; i < color.length; i++) { // System.out.println("getMin"+color[i]); if (color[i] < temp) { temp = color[i]; } } return temp; } // 获取color数组最大位置 int getMax(int[] color) { if (color == null) { return -1; } int temp = color[1]; for (int i = 1; i < color.length; i++) { if (color[i] > temp) { temp = color[i]; } } return temp; } int getShortestLength(int[] color, int[] pearl) { int index = 0; if (pearl.length < color.length - 1) { return -1; } int minPosition = 0, maxPosition = 0, tempLength = 0; for (; index < pearl.length; index++) { // System.out.println(color[pearl[index]]); if (color[pearl[index]] == 0) { tempLength++; } color[pearl[index]] = index; if (tempLength == color.length - 1) { minPosition = getMin(color); maxPosition = getMax(color); shortestLength = maxPosition - minPosition + 1; // System.out.println("MAX"+maxPosition); // System.out.println("MIN"+minPosition); break; } } for (++index; index < pearl.length; ++index) { // System.out.println("NO"+index); if (color[pearl[index]] == minPosition) { // System.out.println("INNER"+color[pearl[index]]); color[pearl[index]] = index; int newMinPositino, newMaxPosition; /* * for(int i:color){ System.out.println("color:"+i); } */ newMinPositino = getMin(color); newMaxPosition = getMax(color); // System.out.println("newMax"+newMaxPosition); // System.out.println("newMin"+newMinPositino); if (newMaxPosition - newMinPositino + 1 < shortestLength) { shortestLength = newMaxPosition - newMinPositino + 1; maxPosition = newMaxPosition; minPosition = newMinPositino; } continue; } color[pearl[index]] = index; } begin = minPosition; over = maxPosition; return shortestLength; } public static void main(String[] args) { // TODO Auto-generated method stub getShortestFullColor g = new getShortestFullColor(); System.out.println("最短长度:" + g.getShortestLength(color, Pearl)); System.out.println("数字集为:"); for (; begin <= over; begin++) { System.out.println(Pearl[begin]); } } }
运行结果如下:
最短长度:5 数字集为: 4 5 3 2 1
相关文章推荐
- 一串首尾相连的珠子(m 个),有N 种颜色(N<=10),设计一个算法,取出其中一段,要求包含所有N 中颜色,并使长度最短。并分析时间复杂度与空间复杂度。
- 一串首尾相连的珠子(m 个),有N 种颜色(N<=10), 设计一个算法,取出其中一段,要求包含所有N 中颜色,并使长度最短
- 一串首尾相连的珠子(m个),有N种颜色(N<=10),设计一个算法,取出其中一段,要求包含所有N中颜色,并使长度最短
- 一串首尾相连的珠子(m 个),有N 种颜色(N<=10),设计一个算法,取出其中一段,要求包含所有N 中颜色,并使长度最短。并分析时间复杂度与空间复杂度。
- 一串首尾相连的珠子(m个),有N种颜色(N《=10),设计一个算法,取出其中一段,要求包含所有N中颜色,并使长度最短。并分析时间复杂度与空间复杂度。
- 一串首尾相连的珠子(m个),有N种颜色(N《=10),设计一个算法,取出其中一段,要求包含所有N中颜色,并使长度最短。并分析时间复杂度与空间复杂度。
- 字符串匹配之通配符问题------一串首尾相连的珠子(m个),有N种颜色(N《=10),设计一个算法,取出其中一段,要求包含所有N中颜色,并使长度最短。
- 字符串匹配之通配符问题------一串首尾相连的珠子(m个),有N种颜色(N《=10),设计一个算法,取出其中一段,要求包含所有N中颜色,并使长度最短。
- 一串首尾相连的珠子(m个),有N种颜色(N《=10),设计一个算法,取出其中一段,要求包含所有N中颜色,并使长度最短。并分析时间复杂度与空间复杂度
- 一串首尾相连的珠子(m个),有N种颜色(N<=10),取出其中一段,要求包含所有N中颜色,并使长度最短
- 【编程题目】一串首尾相连的珠子(m 个),有 N 种颜色(N<=10),取出其中一段,要求包含所有 N 中颜色,并使长度最短。
- 一串首尾相连的珠子(m个),有n种颜色(n<=10) 设计一种算法去除其中一段,要求包含所有的N种颜色
- 40_2 取出首尾相连的珠子中一段,要求包含所有N颜色,并长度最短。 滑动窗口问题
- 一串首尾相连的珠子(m个),有N种颜色(N<=10),使得最短的字符串包含全部颜色
- 有一串首尾相连的珠子,共有m个,每一个珠子有一种颜色,并且颜色的总数不超过n(n<=10),求连续的珠子的颜色总数为n时,长度最小的区间。
- 34. 百度研发笔试题: 一串首尾相连的珠子(m个),有N种颜色(N<=10)
- 一串首尾相连的珠子共m个,其中有n中颜色,求出包含全部这n种颜色的最短的串。
- 实现求出若干整数之和为500的连续整数(如98,99,100,101,102)的所有组合&&产生一个int数组,长度为100,并向其中随机插入1~100,要求不能重复
- <仅是自己做笔记。。。系列15>实现一个挺高级的字符匹配算法: 给一串很长字符串,要求找到符合要求的字符串,例如目的串:123 1******3***2 ,12*****3这些都要找出来
- ACM457现在给出了一个只包含大小写字母的字符串,不含空格和换行,要求把其中的大写换成小写,小写换成大写,然后输出互换后的字符串。输入 第一行只有一个整数m(m<=10),表示测试数据组数。