From 7e791fe554c0848c022f9317399301dc2b10ed32 Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Wed, 2 Nov 2022 17:39:35 +0800 Subject: [PATCH] Fix s0005 solution2 Make sure to initialize every variable on declaration! --- src/s0005_longest_palindromic_substring.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/s0005_longest_palindromic_substring.cpp b/src/s0005_longest_palindromic_substring.cpp index 351dbc2..4a75938 100644 --- a/src/s0005_longest_palindromic_substring.cpp +++ b/src/s0005_longest_palindromic_substring.cpp @@ -96,7 +96,7 @@ Result expand(string s, int i) { } // 边界情况为长度为 1 的字符时 - int left1, right1; + int left1 = 0, right1 = 0; for (int x = 0; i + x < len && i - x >= 0; x++) { if (s[i - x] == s[i + x]) { left1 = i - x; @@ -107,7 +107,7 @@ Result expand(string s, int i) { } // 边界情况为长度为 2 的字符串时 - int left2, right2; + int left2 = 0, right2 = 0; for (int x = 0; i + x + 1 < len && i - x >= 0; x++) { if (s[i - x] == s[i + x + 1]) { left2 = i - x; @@ -123,7 +123,7 @@ Result expand(string s, int i) { string Solution2::longestPalindrome(string s) { int len = s.length(); int left = 0, right = 0; - Result result; + Result result{0, 0, 0, 0}; for (int i = 0; i < len; i++) { result = expand(s, i); if (result.right1 - result.left1 > right - left) {