ARTS 第 肆 期

目录

内容目录

Algorithm

周赛题目如下:
https://leetcode.cn/problems/check-if-grid-satisfies-conditions/
思路:
双重循环遍历检测,时间复杂度为 O(M*N)

代码:

class Solution {
public:
    bool satisfiesConditions(vector<vector<int>>& grid) {
        //判断每一行
        for (int i = 0; i < grid.size(); i++) {  //行
            for (int j = 0; j < grid[0].size(); j++) { //列
                if ((i + 1) < grid.size()) {
                    if (grid[i][j] != grid[i +1][j]) 
                        return false;
                }
                if ((j + 1) < grid[0].size()) {
                    if (grid[i][j] == grid[i][j+1])
                        return false;
                }

            }   
        }
        return true;
    }
};

Review

https://github.com/microsoft/proxy
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p3086r2.pdf
微软开发的基于 C++20 替换虚函数实现运行时多态的一种解决方法。

Tip

Tip1

使用 pre-commit 用作 commit 时的一些规范限制,可以参考百度飞桨的代码风格检查指南:

https://www.paddlepaddle.org.cn/documentation/docs/zh/2.4/dev_guides/git_guides/codestyle_check_guide_cn.html

主要步骤:

  1. 1.pip install pre-commit,这一步如果安装完后还是找不到 pre-commit ,可以重启电脑试试
  2. 在项目根目录增加并修改配置文件:pre-commit-config.yaml
    3.执行 pre-commit install

Tip2

在 c/c++ 项目中尽量开启警告,并将警告视为错误,添加如下选项:

-Wall  -Wextra  -Werror

Wall 标志如果开启,则如下警告会生效:

-Waddress
-Waligned-new (C++ and Objective-C++ only)
-Warray-bounds=1 (only with -O2)
-Warray-compare
-Warray-parameter=2
-Wbool-compare
-Wbool-operation
-Wc++11-compat  -Wc++14-compat  -Wc++17compat  -Wc++20compat
-Wcatch-value (C++ and Objective-C++ only)
-Wchar-subscripts
-Wclass-memaccess (C++ and Objective-C++ only)
-Wcomment
-Wdangling-else
-Wdangling-pointer=2
-Wdelete-non-virtual-dtor (C++ and Objective-C++ only)
-Wduplicate-decl-specifier (C and Objective-C only)
-Wenum-compare (in C/ObjC; this is on by default in C++)
-Wenum-int-mismatch (C and Objective-C only)
-Wformat=1
-Wformat-contains-nul
-Wformat-diag
-Wformat-extra-args
-Wformat-overflow=1
-Wformat-truncation=1
-Wformat-zero-length
-Wframe-address
-Wimplicit (C and Objective-C only)
-Wimplicit-function-declaration (C and Objective-C only)
-Wimplicit-int (C and Objective-C only)
-Winfinite-recursion
-Winit-self (C++ and Objective-C++ only)
-Wint-in-bool-context
-Wlogical-not-parentheses
-Wmain (only for C/ObjC and unless -ffreestanding)
-Wmaybe-uninitialized
-Wmemset-elt-size
-Wmemset-transposed-args
-Wmisleading-indentation (only for C/C++)
-Wmismatched-dealloc
-Wmismatched-new-delete (C++ and Objective-C++ only)
-Wmissing-attributes
-Wmissing-braces (only for C/ObjC)
-Wmultistatement-macros
-Wnarrowing  (C++ and Objective-C++ only)
-Wnonnull
-Wnonnull-compare
-Wopenmp-simd (C and C++ only)
-Woverloaded-virtual=1 (C++ and Objective-C++ only)
-Wpacked-not-aligned
-Wparentheses
-Wpessimizing-move (C++ and Objective-C++ only)
-Wpointer-sign (only for C/ObjC)
-Wrange-loop-construct (C++ and Objective-C++ only)
-Wreorder (C++ and Objective-C++ only)
-Wrestrict
-Wreturn-type
-Wself-move (C++ and Objective-C++ only)
-Wsequence-point
-Wsign-compare (C++ and Objective-C++ only)
-Wsizeof-array-div
-Wsizeof-pointer-div
-Wsizeof-pointer-memaccess
-Wstrict-aliasing
-Wstrict-overflow=1
-Wswitch
-Wtautological-compare
-Wtrigraphs
-Wuninitialized
-Wunknown-pragmas
-Wunused
-Wunused-but-set-variable
-Wunused-const-variable=1 (only for C/ObjC)
-Wunused-function
-Wunused-label
-Wunused-local-typedefs
-Wunused-value
-Wunused-variable
-Wuse-after-free=2
-Wvla-parameter
-Wvolatile-register-var
-Wzero-length-bounds

Wextra 标志如果开启,则如下警告会生效:

-Wabsolute-value (only for C/ObjC)
-Walloc-size
-Wcalloc-transposed-args
-Wcast-function-type
-Wclobbered
-Wdeprecated-copy (C++ and Objective-C++ only)
-Wempty-body
-Wenum-conversion (only for C/ObjC)
-Wexpansion-to-defined
-Wignored-qualifiers  (only for C/C++)
-Wimplicit-fallthrough=3
-Wmaybe-uninitialized
-Wmissing-field-initializers
-Wmissing-parameter-type (C/ObjC only)
-Wold-style-declaration (C/ObjC only)
-Woverride-init (C/ObjC only)
-Wredundant-move (C++ and Objective-C++ only)
-Wshift-negative-value (in C++11 to C++17 and in C99 and newer)
-Wsign-compare (C++ and Objective-C++ only)
-Wsized-deallocation (C++ and Objective-C++ only)
-Wstring-compare
-Wtype-limits
-Wuninitialized
-Wunused-parameter (only with -Wunused or -Wall)
-Wunused-but-set-parameter (only with -Wunused or -Wall)

可以参考:
https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

如果不想开某个具体警告,可以像如下设置:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-function -Wno-unused-parameter")

Tip3

qnx 下调整线程优先级方法:

slay  -P priority -T tid pid

如果没有 -T 则,该进程所有线程都调整优先级

Share

无论是什么职业,都不能使自己过于忙碌,过于忙碌实际上是用战术上的勤奋来掩饰战略上的懒惰,每天都要留有空余时间去反思,而不是依靠惯性去过每一天,思考包括但不限于:1.有没有更优的方法 2.做的事到底有没有收益

打赏作者