[面试备忘]数组(一维)最大子串总结
求整型数组中最大子串方法总结:
1 #include <iostream> 2 #include <cassert> 3 4 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 5 //判断两已排序数组是否有相同元素 6 bool sameNumber(const int* preArray, int preSize, const int* postArray, int postSize) 7 { 8 assert( preArray && postArray); 9 int preIndex = 0; 10 int postIndex = 0; 11 while (preIndex != preSize && postIndex != postSize) { 12 if (preArray[preIndex] == postArray[postIndex]) { 13 return true; 14 } 15 if (preArray[preIndex] > postArray[postIndex]) { 16 ++postIndex; 17 } else { 18 ++preIndex; 19 } 20 } 21 return false; 22 } 23 24 //求数组中最大子串 普通方法 25 int getMaxSubArrayNormal(const int* array, int size) 26 { 27 assert(array); 28 int max = array[0]; 29 for (int outIndex = 0; outIndex != size; ++outIndex) { 30 int tempValue = 0; 31 for (int inIndex = outIndex; inIndex != size; ++inIndex) { 32 tempValue += array[inIndex]; 33 if (tempValue > max) { 34 max = tempValue; 35 } 36 } 37 } 38 return max; 39 } 40 41 //二分方法, 递归实现. 42 int getMaxSubArrayRecursion(const int* array, int size) 43 { 44 if (size == 1) { 45 return array[0]; 46 } 47 int tempSum = array[size / 2 - 1]; 48 int firstHalfMax = array[size / 2 - 1]; 49 for (int index = size / 2 - 2; index >= 0; --index) { 50 tempSum += array[index]; 51 if (tempSum > firstHalfMax) { 52 firstHalfMax = tempSum; 53 } 54 } 55 tempSum = array[size / 2]; 56 int secondHalfMax = array[size / 2]; 57 for (int index = size / 2 + 1; index != size; ++index) { 58 tempSum += array[index]; 59 if (tempSum > secondHalfMax) { 60 secondHalfMax = tempSum; 61 } 62 } 63 return MAX(firstHalfMax + secondHalfMax, 64 MAX(getMaxSubArrayRecursion(array, size / 2), 65 getMaxSubArrayRecursion(array + size / 2, size - size / 2))); 66 } 67 68 //动规方法, 元素如果抖为负数返回0 69 int getMaxSubArrayDP(const int* array, int size) 70 { 71 assert(array); 72 int max = 0; 73 int tempSum = 0; 74 for (int index = 0; index != size; ++index) { 75 tempSum += array[index]; 76 if (tempSum > max) { 77 max = tempSum; 78 } else if (tempSum < 0) { 79 tempSum = 0; 80 } 81 } 82 return max; 83 } 84 85 //动规, 对元素组合没有要求, 返回实际最大子串 86 int getMaxSubArrayDPGeneral(const int* array, int size) 87 { 88 assert(array); 89 int max = array[0]; 90 int tempSum = array[0]; 91 for (int index = 1; index != size; ++index) { 92 if (tempSum < 0) { 93 tempSum = array[index]; 94 } else { 95 tempSum += array[index]; 96 } 97 if (tempSum > max) { 98 max = tempSum; 99 }100 }101 return max; 102 }103 104 //同上, 不过额外返回最大子串起止下标105 int getMaxSubArrayDPGeneralWithRange(const int* array, int size, int& startIndex, int& endIndex)106 {107 assert(array);108 int max = array[0];109 int tempSum = array[0];110 startIndex = endIndex = 0;111 for (int index = 1; index != size; ++index) {112 bool updateStartIndex = false;113 if (tempSum < 0) {114 updateStartIndex = true;115 tempSum = array[index];116 } else {117 tempSum += array[index];118 }119 if (tempSum > max) {120 if (updateStartIndex) {121 startIndex = index;122 }123 endIndex = index;124 max = tempSum;125 }126 }127 return max; 128 }129 130 int main()131 {132 int preArray[] = { 1, 3, 4, 6, 8, 9 };133 int postArray[] = { 5, 8, 10 };134 std::cout << std::boolalpha << sameNumber(preArray, 135 sizeof(preArray) / sizeof(*preArray), postArray, 136 sizeof(postArray) / sizeof(*postArray)) << std::endl;137 int maxSubArray[] = { -14, 3, 5, -6, 5, -3, -2, -1, 16, -10, -2 };138 int startIndex = 0;139 int endIndex = 0;140 std::cout << getMaxSubArrayNormal(maxSubArray, sizeof(maxSubArray) / sizeof(*maxSubArray)) << ' '141 << getMaxSubArrayDP(maxSubArray, sizeof(maxSubArray) / sizeof(*maxSubArray)) << ' '142 << getMaxSubArrayDPGeneral(maxSubArray, sizeof(maxSubArray) / sizeof(*maxSubArray)) << ' '143 << getMaxSubArrayDPGeneralWithRange(maxSubArray, sizeof(maxSubArray) / sizeof(*maxSubArray), startIndex, endIndex) << ' ' 144 << getMaxSubArrayRecursion(maxSubArray, sizeof(maxSubArray) / sizeof(*maxSubArray)) << std::endl;145 for (int index = startIndex; index <= endIndex; ++index) {146 std::cout << maxSubArray[index] << ' ' << std::flush;147 }148 std::cout << std::endl;149 return 0;150 }
首尾"相连"数组求解于上面递归求解方法类似,将问题分解为正常解于跨界最大子串比较问题.
转载于:https://www.cnblogs.com/lifengzhong/archive/2011/08/30/2159714.html
[面试备忘]数组(一维)最大子串总结相关推荐
- Python语法备忘
Python语法备忘 Numpy numpy数据类型: np常用函数 ndarry ndarray属性 ndarray函数 ndarray索引/切片方式 random函数 linalg函数 Panda ...
- 资源 | AI、神经网络、机器学习、深度学习以及大数据学习备忘单
向AI转型的程序员都关注了这个号☝☝☝ 以下是关于神经网络.机器学习.深度学习以及大数据学习的备忘单,其中部分内容和此前发布的<资源 | 值得收藏的 27 个机器学习的小抄>有所重复,大家 ...
- php7和7.1,PHP7.0和7.1 部分新增特性备忘代码分享
以下分别备忘 PHP 7.0 & 7.1 版本的部分新增特性. PHP 7.0$foo = null; $bar = $foo ?? 123; // 相当于 $bar = isset($bar ...
- jQuery学习笔记--JqGrid相关操作 方法列表 备忘 重点讲解(超重要)
JqGrid相关操作备忘 方法列表 特别推荐:怎样获取某一方某一列的值: [html] view plaincopy var rowdata=jQuery("#list").jqG ...
- hp-ux 单用户 启动_UX备忘单:搜索与浏览
hp-ux 单用户 启动 重点 (Top highlight) When designing search results and interest sites, you have to keep i ...
- fib函数用python编写_Python中利用函数装饰器实现备忘功能
"备忘"的定义 "memoization"(备忘)这个词是由Donald Michie在1968年提出的,它基于拉丁语单词"memorandum&qu ...
- PowerBuilder/PB常用备忘
目录 1. PB使用Microsoft.XMLHttp组件的属性与方法 1-1 使用步骤 1-2 XMLHTTP方法: 1-3 XMLHTTP属性: 1-4 示例: 2. Pb中Window添加鼠标滚 ...
- jQuery学习笔记--JqGrid相关操作 方法列表 备忘 重点讲解(超重要) from:jpr1990
JqGrid相关操作备忘 方法列表 1.获得当前列表行数:$("#gridid").getGridParam("reccount"); 2.获取选中行数据(js ...
- MATLAB 备忘清单_开发速查表分享
MATLAB 备忘清单 MATLAB是美国MathWorks公司出品的商业数学软件,用于数据分析.无线通信.深度学习.图像处理与计算机视觉.信号处理.量化金融与风险管理.机器人,控制系统等领域. MA ...
- Java中Steam流的用法及使用备忘
文章目录 Java中Steam流的用法及使用备忘 一. 流的常用创建方法 1-1 使用Collection下的 stream() 和 parallelStream() 方法 1-2 使用Arrays ...
最新文章
- JAVA秒杀mysql层实现_Java高并发秒杀API之web层
- sql发送邮件 html,T-SQL;如何在表之前和之后以HTML格式使用sp_send_dbmail
- SpringCloud 02_什么是分布式、多线程、高并发?(浅析)
- 施一公:让科技工作成为富有吸引力的工作
- LeetCode14最长公共前缀
- 阐述简称PO,VO,TO,BO,DAO,POJO
- OSPF路由协议基础(OSPF基本配置)
- 树莓派入门教程 - 0 - 准备篇 - 0.1 第一次开机
- cnocr:用来做中文OCR的Python3包,装上就能用!
- C#通过XElement写入XML文件
- rtmp直播相关的开源项目
- alpha-beta剪枝算法
- 新gre考试用什么复习资料?
- 什么是工程思维和产品思维
- CDH6.3.2安装部署
- 努力是你最幸福的时候
- 华为m6更新鸿蒙吗,华为手机升级鸿蒙的方法,有哪位升级成功的吗?
- Exception in thread “main“ java.lang.UnsupportedClassVersionError
- 告诉你WHT中文站是什么?
- 【多传感器融合理论】03多传感器信息融合理论(上)
热门文章
- caffe--交通标志识别
- python实现常见的整数进制、字符进制、ASCII码进制之间的转换
- AES-128\192\256加密算法及其安全脆弱分析
- 2021-07-01小程序01
- Dialog里加入下拉框Java_android 自定义dialog弹出框,带单选多选下拉
- era5数据内容说明_接口测试:A04_HttpRunner通用_02_提取数据_01_extract关键字
- 背包问题 装箱问题 货盘装填问题 区别
- java并发编程(7)-- 线程 自旋锁
- Kubernetes 小白学习笔记(22)--kubernetes的运维-管理Namespace
- 数据结构和算法——树结构(二叉树的创建、查找、遍历和删除)