From 9512ed6ed827eec346d38301c5a35a70b03346ee Mon Sep 17 00:00:00 2001 From: yhan <50407509+lingxier@users.noreply.github.com> Date: Tue, 19 Nov 2024 15:15:06 +0800 Subject: [PATCH 1/4] feat: add solution(s) to lc problem(s): No.67 --- .../README.md" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git "a/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/README.md" index ebe212c8fb7b0..ace1c65a610c1 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/README.md" @@ -143,6 +143,46 @@ class Solution { } ``` +#### C++ + +```cpp +class Solution { +public: + int StrToInt(string str) { + int res = 0, bndry = INT_MAX / 10; + int i = 0, sign = 1, length = str.size(); + if(length == 0) { + return 0; + } + // 删除首部空格 + while(str[i] == ' '){ + if(++i == length) { + return 0; + } + } + //若有负号则标识符号位 + if(str[i] == '-') { + sign = -1; + } + if(str[i] == '-' || str[i] == '+') { + i++; + } + for(int j = i; j < length; j++) { + if(str[j] < '0' || str[j] > '9') { + break; + } + //res>214748364越界;res=214748364且str[j] > '7'越界 + if(res > bndry || res == bndry && str[j] > '7'){ + return sign == 1 ? INT_MAX : INT_MIN; + } + //从左向右遍历数字并更新结果 + res = res * 10 + (str[j] - '0'); + } + return sign * res; + } +}; +``` + #### Go ```go From cc3e2933d9d845dee223ddab314701429f23f8e6 Mon Sep 17 00:00:00 2001 From: yhan <50407509+lingxier@users.noreply.github.com> Date: Tue, 19 Nov 2024 15:24:23 +0800 Subject: [PATCH 2/4] feat: add solution(s) to lc problem(s): No.67 --- .../README.md" | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git "a/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/README.md" index ace1c65a610c1..d0fe5413d5135 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/README.md" @@ -151,31 +151,31 @@ public: int StrToInt(string str) { int res = 0, bndry = INT_MAX / 10; int i = 0, sign = 1, length = str.size(); - if(length == 0) { + if (length == 0) { return 0; } // 删除首部空格 - while(str[i] == ' '){ - if(++i == length) { + while (str[i] == ' ') { + if (++i == length) { return 0; } } - //若有负号则标识符号位 - if(str[i] == '-') { + // 若有负号则标识符号位 + if (str[i] == '-') { sign = -1; } - if(str[i] == '-' || str[i] == '+') { + if (str[i] == '-' || str[i] == '+') { i++; } - for(int j = i; j < length; j++) { - if(str[j] < '0' || str[j] > '9') { + for (int j = i; j < length; j++) { + if (str[j] < '0' || str[j] > '9') { break; } - //res>214748364越界;res=214748364且str[j] > '7'越界 - if(res > bndry || res == bndry && str[j] > '7'){ + // res>214748364越界;res=214748364且str[j] > '7'越界 + if (res > bndry || res == bndry && str[j] > '7') { return sign == 1 ? INT_MAX : INT_MIN; } - //从左向右遍历数字并更新结果 + // 从左向右遍历数字并更新结果 res = res * 10 + (str[j] - '0'); } return sign * res; From 6faf533d2604e3cbf490b2ce0e2ffe2352bce59c Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 19 Nov 2024 19:10:15 +0800 Subject: [PATCH 3/4] Update README.md --- .../README.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/README.md" index d0fe5413d5135..1edfd50f307a8 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/README.md" @@ -148,7 +148,7 @@ class Solution { ```cpp class Solution { public: - int StrToInt(string str) { + int strToInt(string str) { int res = 0, bndry = INT_MAX / 10; int i = 0, sign = 1, length = str.size(); if (length == 0) { From 79bffa1d47ac6a6182da552351af49af2a33d801 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Tue, 19 Nov 2024 19:11:01 +0800 Subject: [PATCH 4/4] Create Solution.cpp --- .../Solution.cpp" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 "lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/Solution.cpp" diff --git "a/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/Solution.cpp" new file mode 100644 index 0000000000000..c392b7ddcbd1f --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/Solution.cpp" @@ -0,0 +1,35 @@ +class Solution { +public: + int strToInt(string str) { + int res = 0, bndry = INT_MAX / 10; + int i = 0, sign = 1, length = str.size(); + if (length == 0) { + return 0; + } + // 删除首部空格 + while (str[i] == ' ') { + if (++i == length) { + return 0; + } + } + // 若有负号则标识符号位 + if (str[i] == '-') { + sign = -1; + } + if (str[i] == '-' || str[i] == '+') { + i++; + } + for (int j = i; j < length; j++) { + if (str[j] < '0' || str[j] > '9') { + break; + } + // res>214748364越界;res=214748364且str[j] > '7'越界 + if (res > bndry || res == bndry && str[j] > '7') { + return sign == 1 ? INT_MAX : INT_MIN; + } + // 从左向右遍历数字并更新结果 + res = res * 10 + (str[j] - '0'); + } + return sign * res; + } +};