From e251f9e1acfdbbbee3cef63671752babb5c62c35 Mon Sep 17 00:00:00 2001 From: yhan <50407509+lingxier@users.noreply.github.com> Date: Tue, 19 Nov 2024 15:11:26 +0800 Subject: [PATCH 1/2] feat: add solution(s) to lc problem(s): No.67 --- .../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..2c8398a7affef --- /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; + } +}; From 2dec8a0d185ca679a3dc03f407d51d94b8b99561 Mon Sep 17 00:00:00 2001 From: yhan <50407509+lingxier@users.noreply.github.com> Date: Tue, 19 Nov 2024 15:22:28 +0800 Subject: [PATCH 2/2] feat: add solution(s) to lc problem(s): No.67 --- .../Solution.cpp" | 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/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" index 2c8398a7affef..5d30b3bae674c 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/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" @@ -3,31 +3,31 @@ class Solution { 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;