文 / 2020.07.17
最近这段时间打算把数据结构重新学习一遍。选取的课程为MOOC上的课程《数据结构》。
计划将基础知识用思维导图的方式记录,并进行简单总结。
基本概念
什么是数据结构与算法?
数据结构是数据对象在计算机中的组织方式,算法是操作数据对象的方法。算法效率与数据组织方式、空间利用效率、算法巧妙程度有关。我们可以用时间复杂度和空间复杂度衡量一个算法。
脑图链接:https://www.processon.com/view/link/5f13fcab1e08537d50b32d8f#map
课后习题
01-复杂度1 最大子列和问题 (20分)
求出所有连续子列元素的和中最大者。
01-复杂度1 最大子列和问题 (20分) “最大子列和”则被定义为所有连续子列元素的和中最大者。例如给定序列{ -2, 11, -4, 13, -5, -2 },其连续子列{ 11, -4, 13 }有最大的和20。现要求你编写程序,计算给定整数序列的最大子列和。 本题旨在测试各种不同的算法在各种数据情况下的表现。各组测试数据特点如下: 数据1:与样例等价,测试基本正确性; 数据2:102个随机整数; 数据3:103个随机整数; 数据4:104个随机整数; 数据5:105个随机整数; 输入格式: 输入第1行给出正整数K (≤100000);第2行给出K个整数,其间以空格分隔。 输出格式: 在一行中输出最大子列和。如果序列中所有整数皆为负数,则输出0。 输入样例: 6 -2 11 -4 13 -5 -2 输出样例: 20
课中说了四种解决办法。分别是暴力3次循环O(N^3), 暴力2次循环O(N^3),二分O(NlogN),在线O(N)。
这里贴一个最后一种的Java的代码
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner in = new Scanner(System.in); 6 int N = in.nextInt(); 7 int[] arr = new int[N]; 8 for(int i = 0; i < N; i++) { 9 arr[i] = in.nextInt(); 10 } 11 System.out.println(maxSubSeqSum(N, arr)); 12 } 13 14 public static int maxSubSeqSum(int N, int[] arr) { 15 int curSum = 0, maxSum = 0; 16 for (int i = 0; i < N; i++) { 17 curSum += arr[i]; 18 if (curSum < 0) curSum = 0; 19 else if (maxSum < curSum) maxSum = curSum; 20 } 21 return maxSum; 22 } 23 }
01-复杂度2 Maximum Subsequence Sum (25分)
上一题的进阶版。
01-复杂度2 Maximum Subsequence Sum (25分) The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20. Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence. Input Specification: Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space. Output Specification: For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence. Sample Input: 10 -10 1 2 3 4 -5 -23 3 7 -21 Sample Output: 10 1 4
除了要求所有连续子列元素的和中最大者,还要求所求值的起始值和结束值。其中如果如有数都为负数,那么和为0,起始值和结束值分别为输入值的头和尾。
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner in = new Scanner(System.in); 6 int N = in.nextInt(); 7 8 int[] arr = new int[N]; 9 int curSum = 0, maxSum = -1, start = 0, end = 0, temp = 0; 10 11 for (int i = 0; i < N; i++) { 12 arr[i] = in.nextInt(); 13 curSum += arr[i]; 14 if (maxSum < curSum) { 15 maxSum = curSum; 16 start = temp; 17 end = i; 18 } 19 if (curSum < 0) { 20 curSum = 0; 21 temp = i + 1; 22 } 23 } 24 String str = ""; 25 if (maxSum < 0) { 26 str = "0 " + arr[0] + " " + arr[N - 1]; 27 } else { 28 str = maxSum + " " + arr[start] + " " + arr[end]; 29 } 30 System.out.println(str); 31 32 33 } 34 }
我用Java的代码第7个测试点“最大N”运行超时了,目前没有想到解决办法。所以再贴一份C语言的。
1 #include<stdio.h> 2 3 int main() { 4 int n; 5 scanf("%d", &n); 6 int arr[n], curSum = 0, maxSum = -1, start = 0, end = 0, temp = 0; 7 for (int i = 0; i < n; i++) { 8 scanf("%d", &arr[i]); 9 curSum += arr[i]; 10 if (maxSum < curSum) { 11 maxSum = curSum; 12 end = i; 13 start = temp; 14 } 15 if (curSum < 0) { 16 curSum = 0; 17 temp = i + 1; 18 } 19 } 20 if (maxSum < 0) printf("0 %d %d", arr[0], arr[n - 1]); 21 else printf("%d %d %d", maxSum, arr[start], arr[end]); 22 23 return 0; 24 }