项目介绍
什么是最长递增子序列
六种解法
正文开始
1、暴力法
//外循环是字符串的起始位置 for (int beginLocation = 0; beginLocation < sequence.length() - 1; beginLocation++) { //内部循环是暴力字符串的长度增长空间 for (int subLength = 1; subLength < sequence.length() - beginLocation; subLength++) { sb = new StringBuffer(); sb.append(sequence.charAt(beginLocation)); dealString(sb, sequence, beginLocation, subLength); } }
//生成字符串,其中beginLocation的字符是必须包含的 public void dealString(StringBuffer sb, String s, int beginPosition, int strdepth) { if (strdepth == 0 && judge(sb.toString())) { if (sb.length() > best_length) { best_length = sb.length(); } return; } for (int i = beginPosition + 1; i < s.length(); i++) { sb.append(s.charAt(i)); dealString(sb, s, i, strdepth - 1); sb.deleteCharAt(sb.length() - 1); } }
2、动态规划法
for (int i = 0; i < length; i++) { for (int j = 0; j < i; j++) { if ((intArray[j] < intArray[i])) { longest[i] = (longest[j] + 1) > longest[i] ? (longest[j] + 1) : longest[i]; } } if (longest[i] > best) { best = longest[i]; point = i; } }
3、分治法【局限于连续子串,本题仅供参考】
public int divide(int[] stringArr, int left, int right) { if (left < right) { int mid = (left + right) / 2; int leftValue = divide(stringArr, left, mid); int rightValue = divide(stringArr, mid + 1, right); int midValue = middleHandle(stringArr, left, right); return Math.max(Math.max(leftValue, rightValue), midValue); } return 0; }
//向左扩展 while (leftPoint - 1 >= left && stringArr[leftPoint] > stringArr[leftPoint - 1]) { count++; leftPoint--; } //向右扩展 while (rightPoint + 1 <= right && stringArr[rightPoint] < stringArr[rightPoint + 1]) { count++; rightPoint++; }
4、字符串对比法
//进行快速排序 QuickSortDuplexing q = new QuickSortDuplexing(); q.sortMethod(ints); //因为是递增序列,所以要去重 HashMap hashMap = new HashMap(); for (int i = 0; i < c.length; i++) { hashMap.put(ints[i], 1); } String temp = hashMap.keySet().toString().replace(",", "").replace("[", "").replace("]", "").replace(" ", ""); //再进行最长子序列比较 LCS lcs = new LCS(); int length = lcs.count(temp, sequence).getCommondLength();
5、分支限界法
//剩下的待遍历距离小于当前的最优解,就没有必要再继续下去了。 for (int i = 1; count_best <= length - i; i++) { list_temp = new ArrayList(); list_temp.add(StringArray[i - 1]); count_temp = 1; count(i); }
//当前temp的值加上剩下待遍历的距离,小于等于最优值的时候,就没有必要再继续下去了。 if ((length - 1) - depth + (count_temp + 1) <= count_best) { return; } if (count_temp > count_best || depth == length - 1) { //更新最优解,并继续下去 if (count_temp > count_best) { list_best = new ArrayList<>(list_temp); count_best = count_temp; } //达到终点,停止递归 if (depth == length - 1) { return; } } //分支递归 for (int i = depth; i < length; i++) { if (list_temp.get(count_temp - 1) < StringArray[i]) { count_temp++; list_temp.add(StringArray[i]); count(i + 1); list_temp.remove(list_temp.get(--count_temp)); } }
6、扑克法【扑克法本质是分治思想】
for (int i = 0; i < count; i++) { left = 0; right = piles; poker = intArray[i]; while (left < right) { mid = (left + right) / 2; if (poker < top[mid]) { right = mid;// ? right = mid-1; } else if (poker > top[mid]) { left = mid + 1; } else { right = mid; } } if(left == piles){ piles++; } top[left] = poker; }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算