数据结构

最近在中国大学慕课上参加了浙江大学陈越和何钦铭老师的数据结构的课,正好弥补我对于基础知识的缺失。不过我暂时不会C语言,相关的题目我只能使用python来实现,等暑假翁凯老师的C语言课开了之后会把需要使用C语言的题目再补上。

第一讲 基本概念

1.1 什么是数据结构

解决问题方法的效率和数据的组织方式有关(如何排放图书馆的书)

解决问题方法的效率和空间的利用效率有关(遍历和递归)

解决问题方法的效率和算法的巧妙程度有关

数据结构是数据对象在计算机中的组织方式(逻辑结构和物理存储结构),数据对象必定与一系列加在其上的操作相关联,完成这些操作所用的方法就是算法。

抽象数据类型(Abstract Data Type)

抽象:描述数据类型的方法不依赖具体实现(与存放数据的机器、数据存储的物理结构、实现操作的算法和编程语言都无关)

1.2 什么是算法

算法是一个有限的指令集,接收一些输入,产生输出,一定在有限的步骤后终止,每一条指令必须:不能有歧义、在计算机能处理的范围内、描述不依赖于任何一种语言的具体实现。

判断算法好坏的两个依据:时间复杂度和空间复杂度。

1.3 练习题

"""
给定K个整数组成的序列{ N​1, N2, ..., NK},“连续子列”被定义为{ N​i, N​i+1, ..., N​j},其中 1≤i≤j≤K。“最大子列和”则被定义为所有连续子列元素的和中最大者。例如给定序列{ -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。

输入样例:
-2 11 -4 13 -5 -2
输出样例:
"""

def MaxSum(n, li):
    BiggestSum = 0
    NowSum = 0
    for i in li:
        NowSum += int(i)
        if NowSum < 0:
            NowSum = 0
        if NowSum > BiggestSum:
            BiggestSum = NowSum
    return BiggestSum

n = int(input())
li = [int(i) for i in input().split(" ")]

print(MaxSum(n, li))

01-复杂度1 最大子列和问题
01-复杂度1 最大子列和问题
"""
Given a sequence of K integers { N​1, N2, ..., NK}. A continuous subsequence is defined to be { N​i, Ni+1, ..., N​j} where 1≤i≤j≤K. 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
"""

def MaxSum(n, li):
    BiggestSum = -1
    NowSum = 0
    temp = 0
    start = 0
    end = n - 1
    for i in range(n):
        NowSum += int(li[i])
        if NowSum < 0:
            NowSum = 0
            temp = i + 1
        elif NowSum > BiggestSum:
            BiggestSum = NowSum
            start = temp
            end = i
    return BiggestSum, li[start], li[end]

n = int(input())
li = [int(i) for i in input().split(" ")]

ret = MaxSum(n, li)
if ret[0] >= 0:
    print(ret[0], end=" ")
    print(ret[1], end=" ")
    print(ret[2], end="")
else:
    print(0, end=" ")
    print(li[0], end=" ")
    print(li[-1], end="")
01-复杂度2 Maximum Subsequence Sum

第二讲、线性结构

一、线性表

原文地址:https://www.cnblogs.com/yinwenjie/p/11016298.html