数据结构和算法梳理

本文将目前刷题碰到的算法题做一下分类:

  • 查找:主要就是二分查找
  • 排序:快速排序和插入排序
  • 数据结构相关:栈、队列、链表、二叉树的操作,包括增加元素,读取元素,删除元素等。最好熟记基础的算法实现。
  • 回溯,贪心,动态规划,单调栈,深度优先遍历,广度优先遍历等。这部分就是技巧性较强的部分

  • 基础需要掌握算法的读取和输出,最好掌握两种语言,我是学的C++和python

  • Python的常用函数

  • # 简单的输入用input即可
    # 读取输入中的单个整数
    n = int(input())
    
    # map函数将输入的str转为int,随后用split分割空格
    array = list(map(int, input().split()))
    
    # 读取多行输入,每行一个整数,共 n 行
    data = [int(input()) for _ in range(n)]
    
    # 输出结果
    print("Output:", n)
    for num in array:
        print(num, end=' ')
    print()  # 换行
    
    # 如果需要输出多个变量
    a, b = 5, 10
    print(a, b)
    

    已知行数的时候:

  • n = int(input("Enter the number of lines: "))  # 读取行数
    lines = []
    
    for _ in range(n):
        line = input()  # 读取每一行
        lines.append(line)
    
    # 输出或处理这些行
    for line in lines:
        print(line)
    

    未知行数

  • import sys
    lines = sys.stdin.read().strip().split('\n')
    
    # 处理或输出所有行
    for line in lines:
        print(line)
    
    #考虑用try_except
    import sys
    
    lines = []
    try:
        while True:
            line = input()
            if line:
                lines.append(line)
            else:  # 如果遇到空行也停止读取
                break
    except EOFError:
        pass  # 输入结束
    
    # 输出或处理这些行
    for line in lines:
        print(line)
    
    

  • 在处理大量数据时,标准输入输出方法(input()print())可能较慢。在这种情况下,可以使用 sys.stdinsys.stdout 来加速读写

  • import sys
    input = sys.stdin.read
    data = input().split()
    
    # 处理数据
    index = 0
    n = int(data[index])
    index += 1
    array = []
    for _ in range(n):
        array.append(int(data[index]))
        index += 1
    
    # 输出结果
    sys.stdout.write(f"Output: {n}\n")
    for num in array:
        sys.stdout.write(f"{num} ")
    sys.stdout.write("\n")
    

    数据很多,频繁用input拖累性能,当需要频繁读取输入时,可以使用一个缓冲方法来一次性读入所有数据,然后逐个处理。

  • import sys
    input = sys.stdin.read().split()
    index = 0
    
    def next_int():
        global index
        index += 1
        return int(input[index-1])
    
    n = next_int()
    array = [next_int() for _ in range(n)]
    
    # 输出
    for item in array:
        print(item, end=' ')
    print()
    

    我本人也用C++,把C++的一些方法也整理到这里:

  • C++函数

  • #include <iostream>
    using namespace std;
    
    int main() {
    
        int n;
        cin >> n;
        int a;
        for (int i = 0; i < n; ++i) {
            cin >> a;
            cout << a << " ";
        }
        cout << endl;
        return 0;
    }
    
    #include <iostream>
    #include <string>
    
    int main() {
        int n; // 假设先读取一个表示行数的整数
        std::cin >> n;
        std::cin.ignore();  // 忽略行尾的换行符
    
        std::string line;
        for (int i = 0; i < n; ++i) {
            std::getline(std::cin, line); // 使用 getline 读取一行
            std::cout << line << std::endl; // 输出读取的行
        }
    
        return 0;
    }
    

    上面是已知输入行数的情况下

  • 以下是未知行数

  • #include <iostream>
    #include <string>
    
    int main() {
        std::string line;
        while (std::getline(std::cin, line)) { // 当能从 std::cin 读取行时继续循环
            std::cout << line << std::endl; // 输出每一行
        }
    
        return 0;
    }
    

    存储数据,用vector,后续处理

  • #include <iostream>
    #include <string>
    #include <vector>
    
    int main() {
        std::vector<std::string> lines;
        std::string line;
    
        while (std::getline(std::cin, line)) {
            lines.push_back(line); // 将读取的每行存储到 vector 中
        }
    
        // 处理或输出所有行
        for (const auto& str : lines) {
            std::cout << str << std::endl;
        }
    
        return 0;
    }
    

    大量数据,为增加速度,可以考虑取消绑定

  • #include <iostream>
    #include <string>
    
    int main() {
        std::ios::sync_with_stdio(false); // 提高性能,但不能与 stdio 同时使用
        std::cin.tie(nullptr); // 取消 cin 和 cout 之间的绑定
    
        std::string line;
        while (std::getline(std::cin, line)) {
            std::cout << line << std::endl;
        }
    
        return 0;
    }
    

  • 22
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值