如何将力扣的代码在自己的编译器上运行?


   最近在力扣上刷题有一段时间了,一直在想有时间把这些代码移到自己的编辑器上运行一下子,那就决定是今天了。具体的过程如下,主要是用一个小例子说明的,其他的也大致相当:

1. 写入头文件

   我们可以加入这样的头文件:#include<bits/stdc++.h>,它相当于是很多头文件的集合,有了它,就不用写各种头文件了,它是一个万能头文件,具体是什么呢,简单来说就是把下面的头文件全包含了:

#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

是不是很简单?(bushi ~)
在这里插入图片描述
PS : #include <bits/stdc++.h> 的缺点:

  • 在C++中,头文件 #include <bits/stdc++.h> 是一个非标准的头文件引用,它包含了C++标准库的所有头文件。这种写法在某些编译器中可以正常工作,但并不是所有编译器都支持这种方式

  • 该头文件的目的是为了方便地引入常用的C++标准库头文件,例如 、、 等,而不需要单独列出它们。这样可以简化代码编写,减少头文件引用的繁琐

  • 如果你的编译器支持 #include <bits/stdc++.h> 这种写法,你可以使用它。但请注意,这种写法可能会导致编译时间增加,因为它会引入大量的头文件,而不论你的代码实际上需要使用哪些库函数。

   总结:在编译器支持、做编程题目的时候可以用这个头文件,可以避免大量的头文件书写。在对性能要求极高、引入头文件不多的时候还是老老实实写自己需要的头文件就好。还是要依据时间和代码效率的实际情况来选择。

2. 加入命名空间

   第二步是加入命名空间using namespace std; 用于输入输出。

3. 写好主函数:

int main(){  
    //在这里定义好函数中的测试数据
    cout<<   //这里输出你力扣中的主函数; 
    return 0;    
}

在这里插入图片描述
   力扣中的解是用一个Solution类的方式写出来的,例如

class Solution {
public:
    int strStr(string haystack, string needle) {

    }
};

   但其实一般来说,我们直接用里面的函数strStr(string haystack, string needle)即可。
   主要用于讲解怎么在编译器上执行,其实就是KMP算法。这个题目及其详细思路如下 : (思路留一个坑,下次一定奉上链接!)

   来了来了:C++ 实现 strStr() 也就是优化了大概4次那么多吧(KMP算法next+nextval)

四、KMP算法 实例验证

   以上就是大致思路了,下面直接上一个实例:

28. 实现 strStr() 函数。

题目给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串出现的第一个位置(下标从 0 开始)。如果不存在,则返回 -1 。

说明:
当 needle 是空字符串时,我们应当返回什么值呢?这是一个在面试中很好的问题。
对于本题而言,当 needle 是空字符串时我们应当返回 0 。这与 C 语言的 strstr() 以及 Java 的 indexOf() 定义相符。

示例 1:
输入:haystack = “hello”, needle = “ll”
输出:2

示例 2:
输入:haystack = “aaaaa”, needle = “bba”
输出:-1

示例 3:
输入:haystack = “”, needle = “”
输出:0

提示:
0 <= haystack.length, needle.length <= 5 * 104
haystack 和 needle 仅由小写英文字符组成

#include<bits/stdc++.h>//头文件
using namespace std;//命名空间
vector<int> get_next(string needle){
        int len =needle.size();
        vector<int>next(len+1,0);
        int i=-1,j=0;
        next[0]=-1;
        while(j<len){
            if(i==-1 || needle[i]==needle[j]){
                i++;j++;
                next[j] = i;
            }
            else{
                i=next[i];
            }            
        }   
        return next;    
}
int strStr(string haystack, string needle) {
        int len =needle.size();
        vector<int>next = get_next(needle);
        int i=0,j=0;
        while(j<len  && i<haystack.size()){
            if(j==-1 || haystack[i]==needle[j]){
                i++,j++;
            }
            else{
                j = next[j];
            }            
        }
        if(j==len){
            return i-j;
        }
        return -1;
}

int main(){  
    string haystack="aaaaa";
    string needle="bba";
    cout<<strStr(haystack, needle); 
    return 0;    
}

附上运行截图:

   string haystack="aaaaa";
    string needle="bba";

在这里插入图片描述

   string haystack="hello";
   string needle="llo";

在这里插入图片描述
以上,怕什么真理无穷,进一步有进一步的欢喜!

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐