引言
四度春秋,弹指间转瞬即逝~
而今作为一个社会人,感觉自己在死瘦宅的路上愈行愈远,无法自拔~ 也越来越拖延(懒癌晚期)
为了缓解下这种情况,决定没事刷刷题,而LeetCode就是一个不错的选择(正好也用新入坑的 GO语言写写代码试试水)~
谨以此文记录下LeetCode的刷题之路,也算是作为一个个人题解Orz~
技巧处理
C++ 输入输出加速
在刷LeetCode的时候,习惯性看看最优时间的大佬们的解法,偶然发现有些题同样的方法别人运行时间就要低上不少,然后发现这些提交一般都有如下这个 Lambda表达式。
1 2 3 4 5 6 |
static const auto __________ = []() { ios::sync_with_stdio(false); cin.tie(nullptr); return nullptr; }(); |
仔细看了看LeetCode对于使用C++提交的人,输入输出默认使用的C++的io,而不是C的,上面 Lambda捕获,则可以用来解除C++为了兼容C而采取的缓存同步机制,提升 cin, cout的速度
ios::sync_with_stdio(false) 解惑
因为C++中的 std::cin和 std::cout为了兼容C,保证在代码中同时出现 std::cin和scanf或 std::cout和printf时输出不发生混乱,所以C++用一个流缓冲区来同步C的标准流。通过std :: ios_base :: sync_with_stdio函数可以解除这种同步,让 std::cin和 std::cout不再经过缓冲区,自然就节省了许多时间。
cin.tie(nullptr) 解惑
std::cin默认是与 std::cout绑定的,所以每次操作的时候(也就是调用”<<”或者”>>”)都要刷新(调用flush),这样增加了IO的负担,通过tie(nullptr)来解除 std::cin和 std::cout之间的绑定,来降低IO的负担使效率提升。
话说当年一直相信了C++ IO流效率不如C的鬼话,之后也一直没去深究原因
题解汇总
沉潜深度源自始终如一的专注。人的生命是有限的,精力是有限的,只有专注才能将人的力量发挥到极致。
题解链接 | 题目名称 | 难度 |
---|---|---|
0001: 传送门 Click Here! | Two Sum | Easy |
0002: 传送门 Click Here! | Add Two Numbers | Medium |
0003: 传送门 Click Here! | Longest Substring Without Repeating Characters | Medium |
0004: 传送门 Click Here! | Median of Two Sorted Arrays | Hard |
0005: 传送门 Click Here! | Longest Palindromic Substring | Medium |
0006: 传送门 Click Here! | ZigZag Conversion | Medium |
0007: 传送门 Click Here! | Reverse Integer | Easy |
0008: 传送门 Click Here! | String to Integer (atoi) | Medium |
0009: 传送门 Click Here! | Palindrome Number | Easy |
0010: 传送门 Click Here! | Regular Expression Matching | Hard |
0011: 传送门 Click Here! | Container With Most Water | Medium |
0012: 传送门 Click Here! | Integer to Roman | Medium |
0013: 传送门 Click Here! | Roman to Integer | Easy |
0014: 传送门 Click Here! | Longest Common Prefix | Easy |
0015: 传送门 Click Here! | 3Sum | Medium |
0016: 传送门 Click Here! | 3Sum Closest | Medium |
0017: 传送门 Click Here! | Letter Combinations of a Phone Number | Medium |
0018: 传送门 Click Here! | 4Sum | Medium |
0019: 传送门 Click Here! | Remove Nth Node From End of List | Medium |
0020: 传送门 Click Here! | Valid Parentheses | Easy |
0021: 传送门 Click Here! | Merge Two Sorted Lists | Easy |
0022: 传送门 Click Here! | Generate Parentheses | Medium |
0023: 传送门 Click Here! | Merge k Sorted Lists | Hard |
0024: 传送门 Click Here! | Swap Nodes in Pairs | Medium |
0025: 传送门 Click Here! | Reverse Nodes in k-Group | Hard |
0026: 传送门 Click Here! | Remove Duplicates from Sorted Array | Easy |
0027: 传送门 Click Here! | Remove Element | Easy |
0028: 传送门 Click Here! | Implement strStr() | Easy |
0029: 传送门 Click Here! | Divide Two Integers | Medium |
0030: 传送门 Click Here! | Substring with Concatenation of All Words | Hard |
0031: 传送门 Click Here! | Next Permutation | Easy |
0032: 传送门 Click Here! | Longest Valid Parentheses | Hard |