id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if(numRows==1 || numRows==s.size())\n return s;\n string result[numRows][s.size()];\n int row=0,col=0,curr=0;\n while(curr<s.size()){\n while(row<numRows && curr<s.size()){\n result[row++][col]=s[curr++];\n }\n row=max(0,row-2);\n while(row>0 && curr<s.size()){\n result[row--][++col]=s[curr++];\n }\n col++;\n }\n string ans;\n for(int i=0;i<numRows;i++){\n for(int j=0;j<s.size();j++){\n ans+=result[i][j];\n }\n }\n return ans;\n }\n};", "memory": "41881" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n if (numRows == 1 || numRows >= s.size()) {\n return s;\n }\n \n vector<vector<char>> pattern(numRows, vector<char>(s.size(), '\\0'));\n int temp = 0;\n int j = 0;\n\n for (int i = 0; i < s.size(); ) {\n \n while (j < numRows && temp < s.size()) {\n pattern[j][i] = s[temp++];\n ++j;\n }\n \n j = numRows - 2;\n i++;\n \n \n while (j > 0 && temp < s.size()) {\n pattern[j][i++] = s[temp++];\n --j;\n }\n \n }\n\n \n string result;\n for (int row = 0; row < numRows; ++row) {\n for (int col = 0; col < s.size(); ++col) {\n if (pattern[row][col] != '\\0') {\n result += pattern[row][col];\n }\n }\n }\n\n return result;\n }\n};\n", "memory": "42293" }
6
<p>The string <code>&quot;PAYPALISHIRING&quot;</code> is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)</p> <pre> P A H N A P L S I I G Y I R </pre> <p>And then read line by line: <code>&quot;PAHNAPLSIIGYIR&quot;</code></p> <p>Write the code that will take a string and make this conversion given a number of rows:</p> <pre> string convert(string s, int numRows); </pre> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 3 <strong>Output:</strong> &quot;PAHNAPLSIIGYIR&quot; </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> s = &quot;PAYPALISHIRING&quot;, numRows = 4 <strong>Output:</strong> &quot;PINALSIGYAHRPI&quot; <strong>Explanation:</strong> P I N A L S I G Y A H R P I </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> s = &quot;A&quot;, numRows = 1 <strong>Output:</strong> &quot;A&quot; </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= s.length &lt;= 1000</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), <code>&#39;,&#39;</code> and <code>&#39;.&#39;</code>.</li> <li><code>1 &lt;= numRows &lt;= 1000</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n string convert(string s, int numRows) {\n // Edge case: if numRows is 1, no zigzagging is required\n if (numRows == 1) return s;\n\n // Initialize a 2D vector with numRows and enough columns for the string\n vector<vector<char>> vec(numRows, vector<char>(s.size(), '\\0'));\n\n int i = 0, m = 0, n = 0;\n bool goingDown = true; // Direction control\n\n // Fill the 2D vector with characters in a zigzag pattern\n while (i < s.size()) {\n vec[m][n] = s[i];\n i++;\n \n if (goingDown) {\n // Move down\n m++;\n if (m == numRows) {\n m = numRows - 2; // Go back up after reaching the bottom\n n++;\n goingDown = false;\n }\n } else {\n // Move up\n m--;\n n++;\n if (m == -1) {\n m = 1; // Start moving down after reaching the top\n goingDown = true;\n }\n }\n }\n\n // Build the final string by concatenating non-empty characters\n string result;\n for (int row = 0; row < numRows; row++) {\n for (int col = 0; col < s.size(); col++) {\n if (vec[row][col] != '\\0') {\n result += vec[row][col];\n }\n }\n }\n\n return result;\n }\n};", "memory": "42293" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n long long int reverse(long long x) {\n long long int ans=0;\n while(x!=0){\n long long int digit=x%10;if((ans>INT_MAX/10)||(ans<INT_MIN/10)){\n return 0;\n }\n ans=(ans*10)+digit;\n x=x/10;\n }\n return ans;\n }\n};", "memory": "7900" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int reverse(int x) {\n int ans=0,rem;\n while(x!=0){\n rem=x%10;\n x/=10;\n if(ans>INT_MAX/10 || ans<INT_MIN/10)\n return 0;\n ans=ans*10+rem;\n\n }\n return ans;\n \n \n }\n};", "memory": "8000" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int reverse(int x) {\n long long ans = 0;\n\n while(x != 0){\n int digit = x%10;\n ans = ans*10 + digit;\n x/=10;\n }\n\n if(ans >= INT_MAX || ans <= INT_MIN) return 0;\n return ans;\n }\n};", "memory": "8000" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int reverse(int x) {\n int temp = x;\n int ans=0;\n while(temp != 0){\n int last = temp % 10;\n if((ans > INT_MAX /10) || (ans< INT_MIN/10)){\n return 0;\n }\n ans = ans*10 + last;\n temp/=10;\n }\n return ans;\n }\n};", "memory": "8100" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int reverse(int x) {\n int temp = x;\n int ans=0;\n while(temp != 0){\n int last = temp % 10;\n if((ans > INT32_MAX /10) || (ans< INT32_MIN/10)){\n return 0;\n }\n ans = ans*10 + last;\n temp/=10;\n }\n return ans;\n }\n};", "memory": "8100" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int reverse(int x) {\n long n=0;\n while(x!=0){\n n=n*10+x%10;\n x=x/10;\n if(n>INT_MAX || n<INT_MIN){\n return 0;\n }\n }\n return n;\n }\n};", "memory": "8200" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int reverse(int x) {\n int reversed = 0;\n while (x) {\n int temp = x % 10;\n if ((reversed > INT_MAX / 10) || (reversed < INT_MIN / 10)) {\n return 0;\n }\n x = x / 10;\n reversed = reversed * 10 + temp;\n }\n return reversed;\n }\n};", "memory": "8200" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int reverse(int x) {\n\n long long int rev =0;\n int rem=0;\n \n \n if (x>0){\n while (x>0){\n rem=x%10;\n //if (rev > (LLONG_MAX - rem) / 10 || rev < (LLONG_MIN - rem) / 10) {\n // return 0; // Return 0 if overflow or underflow occurs\n \n rev= (rev*10)+rem;\n x=x/10;\n\n }\n \n if (rev <INT_MAX) return rev;\n else return 0;}\n \n\n else {\n x= abs(x);\n while (x>0){\n rev= (rev*10)+(x%10);\n x=x/10;\n }\n if (rev <INT_MAX) return rev*-1;\n else return 0;\n }\n \n }\n};", "memory": "8300" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int reverse(int x) {\n int reversed = 0;\n while (x != 0) {\n int pop = x % 10;\n x /= 10;\n if (reversed > INT_MAX / 10 || (reversed == INT_MAX / 10 && pop > 7)) return 0;\n if (reversed < INT_MIN / 10 || (reversed == INT_MIN / 10 && pop < -8)) return 0;\n reversed = reversed * 10 + pop;\n }\n return reversed;\n }\n};\n", "memory": "8300" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int reverse(int x) {\n // Return immediately if x is zero\n if(x == 0) return x;\n\n int rev = 0; // This will store the reversed number\n int temp; // Temporary variable to hold the last digit\n bool negative = false; // Flag to check if the number is negative\n\n // If x is negative, make it positive and set the negative flag\n if(x < 0){\n x = abs(x);\n negative = true;\n }\n\n // Skip any trailing zeros by dividing x by 10 until the last digit is non-zero\n while(x % 10 == 0){\n x /= 10;\n }\n\n // Loop to reverse the digits of x\n while ( x != 0)\n {\n // Before multiplying by 10, check for overflow\n if(rev > INT_MAX / 10 || rev < INT_MIN / 10) return 0;\n\n temp = x % 10; // Get the last digit\n rev = rev * 10 + temp; // Append the digit to the reversed number\n x /= 10; // Remove the last digit from x\n }\n\n // If the original number was negative, apply the negative sign to the reversed number\n if(negative) rev = -rev;\n\n return rev; // Return the reversed number\n }\n};", "memory": "8400" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
2
{ "code": "class Solution {\npublic:\n \n \n int reverse(int x) {\n int revNum=0;\n while(x!=0){\n int ld=x%10;\n if (revNum > INT_MAX / 10 ) return 0;\n if (revNum < INT_MIN / 10 ) return 0;\n revNum=revNum*10+ld;\n x/=10;\n }\n \n return revNum;\n\n }\n};", "memory": "8400" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int reverse(int x) {\n long long revNum = 0;\n while(x != 0){\n int digit = x % 10;\n revNum = revNum * 10 + digit;\n x /= 10;\n }\n if(revNum < INT_MIN || revNum > INT_MAX) return 0;\n return revNum;\n }\n};", "memory": "8500" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int reverse(int x) {\n long t=0;\n while(x){\n t=t*10+x%10;\n x/=10;\n if(t<INT_MIN || t>INT_MAX) return 0;\n }\n \n return int(t);\n }\n};", "memory": "8500" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int reverse(int x) {\n bool sign = false;\n if(x<0) sign=true;\n x = abs(x);\n vector<int> digits;\n while(x>0){\n digits.push_back(x%10);\n x/=10;\n }\n long ans=0;\n for(int i=0;i<digits.size();i++){\n ans*=10;\n ans+=(long)digits[i];\n if(ans>INT_MAX) return 0;\n }\n if(sign) ans=-ans;\n return (int)ans;\n }\n};", "memory": "8600" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int comp(string a, string b){\n int n = a.size();\n int m = b.size();\n int flag = 0;\n if(n<m) return 1;\n else{\n m = m-1;\n for(int i = 0; i<n; i++){\n\n if(a[i]>b[(m)] && !flag){ return 0;}\n if(a[i]<b[(m)]){ return 1;}\n m--;\n }\n\n return 1;\n }\n }\n int reverse(int x) {\n if(!x) return x;\n if(x == INT_MIN) return 0;\n\n string all;\n string actual;\n int maxi = INT_MAX;\n while(maxi){\n all.push_back((maxi%10)+'0');\n maxi = maxi/10;\n }\n int temp = x>=0?x:-1*x;\n \n while(temp){\n actual.push_back((temp%10)+'0');\n temp = temp/10;\n }\n ::reverse(all.begin(),all.end());\n cout<<all<<\" \"<<actual;\n if(actual.size()<all.size() || actual<=all){\n temp = stoi(actual);\n return x>=0 ?temp:-1*temp;\n }\n else return 0;\n }\n};", "memory": "8700" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int reverse(int x) {\n vector<int> digits;\n while(x!=0)\n {\n int dig = x%10;\n x=x/10;\n digits.push_back(dig);\n }\n for(int i=digits.size()-1;i>=0;i--)\n {\n if( x +digits[i]*pow(10,digits.size()-1-i) > INT_MAX || x +digits[i]*pow(10,digits.size()-1-i) < INT_MIN )\n {\n return 0;\n }\n x += digits[i]*pow(10,digits.size()-1-i);\n }\n return x;\n }\n};", "memory": "8700" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int reverse(int x) {\n int count = 0;\n int n = abs(x);\n vector<int> v;\n while(n>0){\n count++;\n v.push_back(n%10);\n n = n/10;\n }\n count = count-1;\n\n int ans = 0;\n for(int i=0;i<v.size();i++){\n \n if(INT_MAX - ans > pow(10,count)*v[i]){\n ans += pow(10,count)*v[i];\n }\n else{\n return 0;\n }\n count--;\n }\n\n if(x<0){\n ans = 0-ans;\n }\n return ans;\n }\n};\n\n// Example 1:\n// Input: x = 123\n// Output: 321\n\n// Example 2:\n// Input: x = -123\n// Output: -321\n\n// Example 3:\n// Input: x = 120\n// Output: 21\n", "memory": "8800" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int reverse(int x) {\n /*int i = 0;\n int result = 0;\n while(x != 0) {\n std::cout << \"x : \" << x << std::endl;\n std::cout << \"i : \" << i << std::endl;\n std::cout << result << std::endl;\n std::cout << \"-----------\" << std::endl;\n result += ((x%10) * pow(10, i));\n i++;\n x = x/10;\n }\n std::cout << \"x : \" << x << std::endl;\n std::cout << \"i : \" << i << std::endl;\n std::cout << result << std::endl;\n std::cout << \"-----------\" << std::endl;\n result += ((x%10) * pow(10, i));\n return result;*/\n std::vector<int> digits;\n int result = 0;\n int min = std::numeric_limits<int>::min();\n int max = std::numeric_limits<int>::max();\n while(x!=0) {\n digits.push_back(x%10);\n x = x/10;\n }\n for(int i = 0; i < digits.size(); i++) {\n cout << \"i : \" << i << endl;\n cout << digits[i] << endl;\n cout << \"result : \" << result << endl;\n cout << \"----------\" << endl;\n if(max - (pow(10,digits.size()-1-i) * digits[i]) <= result || min - (pow(10,digits.size()-1-i) * digits[i]) >= result) {\n return 0;\n }\n result += pow(10,digits.size()-1-i) * digits[i];\n }\n return result;\n \n }\n};", "memory": "8800" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int reverse(int x) {\n int count = 0;\n int n = abs(x);\n vector<int> v;\n while(n>0){\n count++;\n v.push_back(n%10);\n n = n/10;\n }\n count = count-1;\n\n int ans = 0;\n for(int i=0;i<v.size();i++){\n \n if(INT_MAX - ans > pow(10,count)*v[i]){\n ans += pow(10,count)*v[i];\n }\n else{\n return 0;\n }\n count--;\n }\n\n if(x<0){\n ans = 0-ans;\n }\n return ans;\n }\n};", "memory": "8900" }
7
<p>Given a signed 32-bit integer <code>x</code>, return <code>x</code><em> with its digits reversed</em>. If reversing <code>x</code> causes the value to go outside the signed 32-bit integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then return <code>0</code>.</p> <p><strong>Assume the environment does not allow you to store 64-bit integers (signed or unsigned).</strong></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 123 <strong>Output:</strong> 321 </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -123 <strong>Output:</strong> -321 </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 120 <strong>Output:</strong> 21 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup> &lt;= x &lt;= 2<sup>31</sup> - 1</code></li> </ul>
3
{ "code": "class Solution {\nprivate:\n bool overflowNum(const int& digit, const int& n) {\n if (digit > 2 && n > 8) return true;\n return false;\n }\n\n bool overflowNum(const int& max_num, const int& a, const int& b) {\n if (a > max_num - b) return true;\n return false;\n }\npublic:\n int reverse(int x) {\n int max_num = numeric_limits<int>::max();\n bool neg = x < 0 ? true : false;\n // x = x < 0 ? -x : x;\n int rev_num = 0;\n vector<int> digits;\n while (x) {\n digits.push_back(abs(x % 10));\n x /= 10;\n }\n bool leading_zero = true;\n int pos = digits.size();\n for (const int& digit : digits) {\n pos -= 1;\n cout << digit << \": \" << pos << \"\\n\";\n if (digit == 0 && leading_zero) continue;\n if (overflowNum(digit, pos)) return 0;\n int n = digit * pow(10, pos);\n if (overflowNum(max_num, rev_num, n)) return 0;\n rev_num += n;\n }\n if (neg) return -rev_num;\n return rev_num;\n }\n};", "memory": "8900" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
0
{ "code": "#include <string>\n#include <limits> // for std::numeric_limits\n\nclass Solution {\npublic:\n int myAtoi(const std::string& s) {\n int i = 0;\n int n = s.size();\n int sign = 1;\n long long result = 0;\n\n // Skip leading whitespace\n while (i < n && s[i] == ' ') {\n ++i;\n }\n\n // Check for sign\n if (i < n && (s[i] == '+' || s[i] == '-')) {\n sign = (s[i] == '-') ? -1 : 1;\n ++i;\n }\n\n // Convert string to integer\n while (i < n && std::isdigit(s[i])) {\n int digit = s[i] - '0';\n\n // Check for overflow and underflow\n if (result > (std::numeric_limits<int>::max() - digit) / 10) {\n return sign == 1 ? std::numeric_limits<int>::max() : std::numeric_limits<int>::min();\n }\n\n result = result * 10 + digit;\n ++i;\n }\n\n return sign * static_cast<int>(result);\n }\n};\n", "memory": "8500" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n //O(n) time complexity\n //O(1) space complexity.\n int myAtoi(const string& s) {\n int ptr = 0;\n\n //Ignore leading whitespace\n while (ptr < s.size() && s[ptr] == ' ') {\n ptr++;\n }\n\n if (ptr == s.size()) return 0; //assume postive 0 -> no sign, no characters\n\n int factor = 1; //Check sign\n\n if (s[ptr] == '-') {\n factor = -1;\n ptr++;\n } else if (s[ptr] == '+') {\n ptr++;\n }\n\n //Leading 0s\n\n while (ptr < s.size() && s[ptr] == '0') {\n ptr++;\n }\n\n int sum = 0;\n\n while (ptr < s.size() && isdigit(s[ptr])) {\n\n const int digit = factor * (s[ptr] - '0');\n\n if (sum > (INT_MAX / 10)) {\n return INT_MAX;\n } else if (sum < (INT_MIN / 10)) {\n return INT_MIN;\n }\n\n sum *= 10;\n\n if (factor == 1 && sum > INT_MAX - digit) return INT_MAX;\n else if (factor == -1 && sum < INT_MIN - digit) return INT_MIN;\n sum += digit;\n\n ptr++;\n }\n\n return sum;\n }\n};", "memory": "8600" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n \nint myAtoi(const string& str) {\n int i = 0;\n int n = str.length();\n // Skip leading spaces\n while (i < n && str[i] == ' ') {\n i++;\n }\n\n // Check if the string is empty after removing spaces\n if (i == n) return 0;\n\n // Check for optional sign\n int sign = 1;\n if (str[i] == '+' || str[i] == '-') {\n sign = (str[i] == '-') ? -1 : 1;\n i++;\n }\n\n // Convert characters to integer\n long result = 0;\n while (i < n && isdigit(str[i])) {\n result = result * 10 + (str[i] - '0');\n if (result * sign >= INT_MAX) return INT_MAX;\n if (result * sign <= INT_MIN) return INT_MIN;\n i++;\n }\n\n return result * sign;\n}\n};", "memory": "8700" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int myAtoi(string str) {\n int res = 0, sign = 1, i = 0;\n // handle empty string\n if (str.empty()) return 0;\n // handle leading spaces\n while (str[i] == ' ') ++i;\n // handle sign\n if (str[i] == '+' || str[i] == '-') {\n sign = (str[i++] == '+') ? 1 : -1; \n }\n // convert number and avoid overflow\n while (i < str.size()) {\n if (str[i] < '0' || str[i] > '9') break;\n if (res > INT_MAX / 10 || (res == INT_MAX / 10 && str[i] - '0' > 7)) {\n return (sign == 1) ? INT_MAX : INT_MIN;\n }\n res = 10 * res + (str[i++] - '0');\n }\n return sign * res;\n }\n};", "memory": "8700" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int myAtoi(string s) {\n int n=s.size();\n int i=0,si=1;\n while(i<n && s[i]==' ') i++;\n\n long long res=0;\n if(i<n && (s[i]=='+'||s[i]=='-')){\n si=(s[i]=='+')?1:-1;\n i++;\n }\n\n \n while(i<n && isdigit(s[i])){\n res=res*10+(s[i]-'0');\n if(res*si <INT_MIN) return INT_MIN;\n if(res*si>INT_MAX) return INT_MAX;\n i++;\n }\n return res*si;\n \n }\n};", "memory": "8800" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int myAtoi(string s) {\n int i=0;\n int sign=1;\n long ans=0;\n while(i<s.length() && s[i]==' ')\n i++;\n if(s[i]=='-')\n {\n sign=-1;\n i++;\n }\n else if(s[i]=='+')\n i++;\n while(i<s.length())\n {\n if(s[i]>='0' && s[i]<='9')\n {\n ans=ans*10+(s[i]-'0');\n if(ans>INT_MAX && sign==-1)\n return INT_MIN;\n else if(ans>INT_MAX && sign==1)\n return INT_MAX;\n i++;\n }\n else\n return ans*sign;\n }\n return (ans*sign);\n }\n};", "memory": "8800" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int myAtoi(string s) {\n int ans = 0;\n int i=0,f=1;\n while(i<s.length() && s[i]==' ') {\n i++;\n }\n if(s[i]=='+'){\n f=1;\n i++;\n } else if(s[i]=='-') {\n f=-1;\n i++;\n }\n while(i<s.length() && s[i]>='0' && s[i]<='9') {\n if(ans > INT_MAX/10) {\n if(f==1) return INT_MAX;\n return INT_MIN;\n } else {\n ans *= 10;\n }\n int x = s[i]-'0';\n if(ans>INT_MAX-x) {\n if(f==1) return INT_MAX;\n return INT_MIN;\n }\n ans += x;\n i++;\n }\n return ans*f;\n }\n};", "memory": "8900" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int myAtoi(string s) {\n long num=0;\n int i=0;\n int sign=1;\n\n while(s[i]==' ') i++;\n if(s[i]=='-' || s[i]=='+'){\n sign=(s[i]=='-')?-1:1;\n i++;\n }\n while(i<s.length() && isdigit(s[i])){\n num=num*10+(s[i]-'0');\n if(num*sign <= INT_MIN) return INT_MIN;\n if(num*sign >= INT_MAX) return INT_MAX;\n i++;\n }\n\n return num*sign;\n }\n};", "memory": "8900" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
0
{ "code": "#include <iostream>\n#include <climits> // For INT_MAX and INT_MIN\nusing namespace std;\n\nclass Solution {\npublic:\n int myAtoi(string s) {\n int index = 0;\n int n = s.length();\n int sign = 1;\n long long result = 0; // Use long long to handle overflow\n\n // Step 1: Ignore leading whitespaces\n while (index < n && s[index] == ' ') {\n ++index;\n }\n\n // Step 2: Handle sign\n if (index < n && (s[index] == '+' || s[index] == '-')) {\n sign = (s[index] == '-') ? -1 : 1;\n ++index;\n }\n\n // Step 3: Convert digits to integer\n while (index < n && isdigit(s[index])) {\n result = result * 10 + (s[index] - '0');\n if (result * sign > INT_MAX) {\n return INT_MAX;\n }\n if (result * sign < INT_MIN) {\n return INT_MIN;\n }\n ++index;\n }\n\n return result * sign;\n }\n};\n", "memory": "9000" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
0
{ "code": "class Solution {\npublic:\n int myAtoi(string s) {\n int front = 1;\n int num = 0;\n int flag = 0;\n for (int i = 0; i < s.length(); i++) {\n if (s[i] == ' ') {\n if (flag == 1) {\n break;\n }\n continue;\n }\n else if (s[i] == '-') {\n if (flag == 1) {\n break;\n }\n flag = 1;\n front = -1;\n }\n else if (s[i] == '+') {\n if (flag == 1) {\n break;\n }\n flag = 1;\n }\n else if (s[i] >= '0' && s[i] <= '9') {\n flag = 1;\n if (num > 214748364 || (num == 214748364 && s[i] > '7')) {\n if (front == -1) {\n return -2147483648;\n }\n else {\n return 2147483647;\n }\n }\n else {\n num = num * 10 + (s[i] - '0');\n }\n }\n else {\n break;\n }\n }\n return num * front;\n }\n};", "memory": "9000" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int myAtoi(string s) \n {\n int i=0;\n int sign=1;\n long ans=0;\n while(i<s.length() && s[i]==' ')\n i++;\n if(s[i]=='-')\n {\n sign=-1;\n i++;\n }\n else if(s[i]=='+')\n i++;\n while(i<s.length())\n {\n if(s[i]>='0' && s[i]<='9')\n {\n ans=ans*10+(s[i]-'0');\n if(ans>INT_MAX && sign==-1)\n return INT_MIN;\n else if(ans>INT_MAX && sign==1)\n return INT_MAX;\n i++;\n }\n else\n return ans*sign;\n }\n return (ans*sign);\n }\n};", "memory": "9100" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
1
{ "code": "class Solution {\npublic:\n int myAtoi(string s) {\n\n int ans = 0;\n int signState = 0;\n bool fresh = true;\n\n for(int i = 0; i<s.length() ;i++){\n if(fresh){\n if( !signState && !ans) {\n if(s[i]=='+')\n {signState = 1; fresh=false; continue;}\n else if(s[i]=='-')\n {signState = 2; fresh=false; continue;}\n }\n if(s[i]==' ') continue;\n }\n\n if(std::isdigit(s[i])) {\n if( ( ((long)ans*10 + (long)((int)s[i] - 48)) > std::numeric_limits<int>::max()) ) \n {\n if(signState==2)\n return std::numeric_limits<int>::min() ;\n else\n return std::numeric_limits<int>::max() ;\n }\n \n ans = ans*10 + ((int)s[i] - 48); fresh = false;\n }\n else break;\n }\n return ans*((signState==2)?-1:1);\n }\n};", "memory": "9100" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\n public:\n int myAtoi(string s) {\n trim(s);\n if (s.empty())\n return 0;\n\n const int sign = s[0] == '-' ? -1 : 1;\n if (s[0] == '+' || s[0] == '-')\n s = s.substr(1);\n\n long num = 0;\n\n for (const char c : s) {\n if (!isdigit(c))\n break;\n num = num * 10 + (c - '0');\n if (sign * num < INT_MIN)\n return INT_MIN;\n if (sign * num > INT_MAX)\n return INT_MAX;\n }\n\n return sign * num;\n }\n\n private:\n void trim(string& s) {\n s.erase(0, s.find_first_not_of(' '));\n s.erase(s.find_last_not_of(' ') + 1);\n }\n};\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n\n", "memory": "9200" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
2
{ "code": "class Solution {\npublic:\n int myAtoi(string s) {\n long ans = 0;\n int sign = 1;\n int i = 0;\n\n while (s[i] == ' ') i++;\n\n if (s[i] == '-' || s[i] == '+') {\n sign = (s[i] == '-') ? -1 : 1;\n i++;\n }\n while (i < s.length() && isdigit(s[i])) {\n ans = ans * 10 + (s[i] - '0');\n if (ans*sign <= INT_MIN) return INT_MIN;\n if (ans*sign >= INT_MAX) return INT_MAX;\n i++;\n }\n return ans * sign; \n }\n};\n\n", "memory": "9200" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int myAtoi(string s) {\n int len = s.size();\n double num = 0;\n int i=0;\n while(s[i] == ' '){\n i++;\n }\n bool positive = s[i] == '+';\n bool negative = s[i] == '-';\n positive == true ? i++ : i;\n negative == true ? i++ : i;\n while(i < len && s[i] >= '0' && s[i] <= '9'){\n num = num*10 + (s[i]-'0');\n i++;\n }\n num = negative ? -num : num;\n cout<<num<<endl;\n num = (num > INT_MAX) ? INT_MAX : num;\n num = (num < INT_MIN) ? INT_MIN : num;\n cout<<num<<endl;\n return int(num);\n }\n};", "memory": "9300" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\n public:\n int myAtoi(string s) {\n trim(s);\n if (s.empty())\n return 0;\n\n const int sign = s[0] == '-' ? -1 : 1;\n if (s[0] == '+' || s[0] == '-')\n s = s.substr(1);\n\n long num = 0;\n\n for (const char c : s) {\n if (!isdigit(c))\n break;\n num = num * 10 + (c - '0');\n if (sign * num < INT_MIN)\n return INT_MIN;\n if (sign * num > INT_MAX)\n return INT_MAX;\n }\n\n return sign * num;\n }\n\n private:\n void trim(string& s) {\n s.erase(0, s.find_first_not_of(' '));\n s.erase(s.find_last_not_of(' ') + 1);\n }\n};", "memory": "9300" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n void trim(string& s){\n s.erase(0,s.find_first_not_of(' '));\n s.erase(s.find_last_not_of(' ') + 1);\n }\n int myAtoi(string s) {\n trim(s);\n int sign = s[0] == '-'? -1:1;\n if(s[0]=='+' || s[0]=='-')\n s = s.substr(1);\n long num = 0;\n for(const char c : s){\n if(!isdigit(c)){\n break;\n }\n num = num*10 + (c - '0');\n if(sign * num < INT_MIN){\n return INT_MIN;\n }\n if(sign*num > INT_MAX){\n return INT_MAX;\n }\n }\n\n return sign*num;\n }\n};", "memory": "9400" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int myAtoi(string s) {\n \n int i = 0, n = s.length();\n bool neg = false;\n \n if (n == 0)\n return 0;\n \n while (i < n && s[i] == ' ')\n i++;\n \n if (i < n && s[i] == '-')\n {\n neg = true;\n i++;\n }\n else if (i < n && s[i] == '+')\n {\n i++;\n }\n \n while (i < n && s[i] == '0')\n i++;\n \n int j = i;\n \n for(; i < n; i++){\n if (!isdigit(s[i]))\n break;\n }\n \n if (i == j)\n return 0;\n \n if (i - j > 10){\n if(neg)\n return INT_MIN;\n else\n return INT_MAX;\n }\n \n if (i - j == 10){\n if(neg)\n {\n string str = \"2147483648\";\n \n int k = 0;\n while (k < 10){\n if (str[k] == s[j+k]){\n k++;\n \n if (k == 10)\n return INT_MIN;\n \n continue;\n }\n else if (str[k] < s[j+k]){\n return INT_MIN;\n }\n else{\n break;\n }\n }\n }\n else\n {\n string str = \"2147483647\";\n \n int k = 0;\n while (k < 10){\n if (str[k] == s[j+k]){\n k++;\n \n if (k == 10)\n return INT_MAX;\n \n continue;\n }\n else if (str[k] < s[j+k]){\n return INT_MAX;\n }\n else{\n break;\n }\n }\n }\n }\n \n string str = s.substr(j, i-j);\n int res = stoi(str);\n \n if(neg)\n res = -res;\n \n return res;\n }\n};", "memory": "9400" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int myAtoi(string s) {\n bool flag = true,b=false;\n string f = \"\";\n int count = 0;\n for (int i = 0; i < s.length(); i++) {\n if (s[i] == '-' && !count && !b) {\n b=true;\n flag = false;\n } else if (s[i] == ' ' && !count && !b) {\n continue;\n }\n else if(s[i] == '+' && !b && !count){\n b=true;\n continue;\n }\n else if ((s[i] - '0') <= 9 && (s[i] - '0') >= 0) {\n f += s[i];\n // flag = true;\n count++;\n } else {\n break;\n }\n }\n\n string f1 = \"\";\n bool final = true;\n for (int i = 0; i < f.size(); i++) {\n if (f[i] == 0 && final) {\n continue;\n } else {\n f1 += f[i];\n final = false;\n }\n }\n if (f1.empty())\n return 0;\n long long ans = 0;\n // cout<<f1;\n for (int i = 0; i < f1.size(); i++) {\n if(ans<=INT_MAX){\n\n ans = (long long)ans * (long long)10 + (long long)(f1[i] - '0');\n }\n }\n if (!flag){\n ans *= -1;\n ans = max(ans , (long long) INT_MIN);\n }\n else{\n ans = min(ans , (long long) INT_MAX);\n }\n return (int)ans;\n }\n};\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n \n \n \n \n\n\n\n\n\n\n\n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n \n \n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n \n \n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n \n \n \n", "memory": "9500" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int myAtoi(string s) {\n bool neg = false;\n int i = 0;\n int n = s.length();\n if (n == 0) {\n return 0;\n }\n while (i < n && s[i] == ' ') {\n i++;\n }\n if (i < n && s[i] == '-') {\n i++;\n neg = true;\n } else if (i < n && s[i] == '+') {\n i++;\n }\n while (i < n && s[i] == '0') {\n i++;\n }\n int j = i;\n for (; i < n; i++) {\n if (!isdigit(s[i])) {\n break;\n }\n }\n if (i == j) {\n return 0;\n }\n if (i - j > 10) {\n if(neg){\n return INT_MIN;\n }\n else{\n return INT_MAX;\n }\n }\n if (i - j == 10) {\n if (neg) {\n string str = \"2147483648\";\n int k = 0;\n while (k < 10) {\n if (str[k] == s[j + k]) {\n k++;\n if (k == 10) {\n return INT_MIN;\n }\n continue;\n } else if (s[j + k] > str[k]) {\n return INT_MIN;\n } else {\n break;\n }\n }\n } else {\n string str = \"2147483647\";\n int k = 0;\n while (k < 10) {\n if (str[k] == s[j + k]) {\n k++;\n if (k == 10) {\n return INT_MAX;\n }\n continue;\n } else if (s[j + k] > str[k]) {\n return INT_MAX;\n } else {\n break;\n }\n }\n }\n }\n string str = s.substr(j, i - j);\n int res = stoi(str);\n if (neg) {\n res = -res;\n }\n return res;\n }\n};", "memory": "9500" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int myAtoi(string s) {\n bool neg = false;\n int i = 0;\n int n = s.length();\n if (n == 0) {\n return 0;\n }\n while (i < n && s[i] == ' ') {\n i++;\n }\n if (i < n && s[i] == '-') {\n i++;\n neg = true;\n } else if (i < n && s[i] == '+') {\n i++;\n }\n while (i < n && s[i] == '0') {\n i++;\n }\n int j = i;\n for (; i < n; i++) {\n if (!isdigit(s[i])) {\n break;\n }\n }\n if (i == j) {\n return 0;\n }\n if (i - j > 10) {\n if(neg){\n return INT_MIN;\n }\n else{\n return INT_MAX;\n }\n }\n if (i - j == 10) {\n if (neg) {\n string str = \"2147483648\";\n int k = 0;\n while (k < 10) {\n if (str[k] == s[j + k]) {\n k++;\n if (k == 10) {\n return INT_MIN;\n }\n continue;\n } else if (s[j + k] > str[k]) {\n return INT_MIN;\n } else {\n break;\n }\n }\n } else {\n string str = \"2147483647\";\n int k = 0;\n while (k < 10) {\n if (str[k] == s[j + k]) {\n k++;\n if (k == 10) {\n return INT_MAX;\n }\n continue;\n } else if (s[j + k] > str[k]) {\n return INT_MAX;\n } else {\n break;\n }\n }\n }\n }\n string str = s.substr(j, i - j);\n int res = stoi(str);\n if (neg) {\n res = -res;\n }\n return res;\n }\n};", "memory": "9600" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int myAtoi(string s) {\n bool neg = false;\n int i = 0;\n int n = s.length();\n if (n == 0) {\n return 0;\n }\n while (i < n && s[i] == ' ') {\n i++;\n }\n if (i < n && s[i] == '-') {\n i++;\n neg = true;\n } else if (i < n && s[i] == '+') {\n i++;\n }\n while (i < n && s[i] == '0') {\n i++;\n }\n int j = i;\n for (; i < n; i++) {\n if (!isdigit(s[i])) {\n break;\n }\n }\n if (i == j) {\n return 0;\n }\n if (i - j > 10) {\n if(neg){\n return INT_MIN;\n }\n else{\n return INT_MAX;\n }\n }\n if (i - j == 10) {\n if (neg) {\n string str = \"2147483648\";\n int k = 0;\n while (k < 10) {\n if (str[k] == s[j + k]) {\n k++;\n if (k == 10) {\n return INT_MIN;\n }\n continue;\n } else if (s[j + k] > str[k]) {\n return INT_MIN;\n } else {\n break;\n }\n }\n } else {\n string str = \"2147483647\";\n int k = 0;\n while (k < 10) {\n if (str[k] == s[j + k]) {\n k++;\n if (k == 10) {\n return INT_MAX;\n }\n continue;\n } else if (s[j + k] > str[k]) {\n return INT_MAX;\n } else {\n break;\n }\n }\n }\n }\n string str = s.substr(j, i - j);\n int res = stoi(str);\n if (neg) {\n res = -res;\n }\n return res;\n }\n};", "memory": "9600" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int myAtoi(string s) {\n int reading = 0;\n int negative = 0;\n int overone = 0;\n string temp;\n for(int i = 0; i < s.length(); i++){\n if(s[i] == ' ' && !reading) continue;\n if(s[i] == '+' && !reading){\n reading = 1;\n continue;\n }\n if(s[i] == '-' && !reading){\n negative = 1;\n reading = 1;\n continue;\n }\n if(s[i] < '0' || s[i] > '9') break;\n if(!reading) reading = 1;\n if(s[i] == '0' && !overone) continue;\n if(!overone) overone = 1;\n temp += s[i];\n }\n if(!overone) temp += '0';\n cout << temp;\n if(temp.length() > 10){\n if(negative) return INT_MIN;\n else return INT_MAX;\n }\n if(temp.length() == 10){\n string MIN = \"2147483648\";\n string MAX = \"2147483647\";\n if(negative){\n for(int i = 0; i < 10; i++){\n if(temp[i] < MIN[i]) break;\n if(temp[i] == MIN[i]) continue;\n return INT_MIN;\n }\n }\n else{\n for(int i = 0; i < 10; i++){\n if(temp[i] < MAX[i]) break;\n if(temp[i] == MAX[i]) continue;\n return INT_MAX;\n }\n }\n }\n if(negative) temp = '-' + temp;\n return atoi(temp.c_str());\n }\n};", "memory": "9700" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n string starttrim(const string &s) {\n size_t start = find_if_not(s.begin(), s.end(), [](unsigned char ch) { return isspace(ch); }) - s.begin();\n return s.substr(start);\n }\n\n string endtrim(const string &s) {\n size_t end = find_if_not(s.rbegin(), s.rend(), [](unsigned char ch) { return isspace(ch); }).base() - s.begin();\n return s.substr(0, end);\n }\n\n string trim(const string &s) {\n return endtrim(starttrim(s));\n }\n\n int myAtoi(string s) {\n s = trim(s);\n\n int len = s.length();\n int result = 0;\n int flag = 1;\n\n if (len == 0) return 0;\n\n int i = 0;\n\n // Handle sign\n if (s[i] == '+' || s[i] == '-') {\n flag = (s[i] == '-') ? -1 : 1;\n i++;\n }\n\n // Parse digits\n while (i < len && s[i] >= '0' && s[i] <= '9') {\n int digit = s[i] - '0';\n\n // Check for overflow\n if (result > (INT_MAX - digit) / 10) {\n return (flag == 1) ? INT_MAX : INT_MIN;\n }\n\n result = result * 10 + digit;\n i++;\n }\n\n return flag * result;\n }\n};\n\n\n\n\n\n\n\n\n\n\n// class Solution {\n// public:\n// string starttrim(const string &s){\n// size_t start = find_if_not(s.begin(),s.end(), [](unsigned char\n// ch){return isspace(ch);}) - s.begin(); return s.substr(start);\n// }\n\n// string endtrim(const string &s){\n// size_t end = find_if_not(s.rbegin(),s.rend(),[](unsigned char\n// ch){return isspace(ch);}).base() - s.begin();\n\n// return s.substr(0,end);\n// }\n\n// string trim(string &s) {\n// return endtrim(starttrim(s));\n// }\n// int output(int &result){\n// int finaloutput = 0;\n// int val = 0;\n// while(result != 0){\n// val = result % 10;\n// finaloutput = finaloutput * 10 + val;\n// result /= 10;\n// }\n// return finaloutput;\n// }\n\n// int myAtoi(string s) {\n// s = trim(s);\n\n// int len = s.length();\n// int result = 0, val = 0;\n// int flag = 1;\n// char sign = '\\0';\n\n// if (len == 0) return 0;\n\n// if(s[0] == '+' || s[0] == '-'){\n// flag = (s[0] == '+') ? 1: -1;\n// }\n\n// for(int i=0; i<len; i++){\n\n// if (result > (INT_MAX - val) / 10) {\n// return (flag == 1) ? INT_MAX : INT_MIN;\n// }\n\n// else if(s[i] >='0' && s[i] <='9' ){\n// val = s[i] -'0'; \n// result = result * 10 + val;\n// }\n\n// else if((s[i] == '+' || s[i] == '-' || (s[i] >= 'a' && s[i] <= 'z')) && result >= 0){\n// return result;\n// }\n\n// }\n// result = output(result);\n// int result1 = output(result);\n// return flag * result1;\n// }\n// };\n", "memory": "9700" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int myAtoi(string s) {\n bool flag = true, state = true;\n int tmp = 0;\n string str = \"\";\n int res = 0;\n\n for (int i=0; i<s.size(); ++i) {\n if (state && s[i]==' ')\n continue;\n else if (state && s[i]=='+') {\n state = false;\n continue;\n }\n else if (state && s[i]=='-') {\n flag = false;\n state = false;\n continue;\n }\n if (s[i] <= 57 && s[i] >= 48){\n state = false;\n str = str + s[i];\n ++tmp;\n }\n else\n break;\n }\n \n for (int i=0; i<tmp; ++i) {\n if (res > (INT_MAX - (str[i] - '0')) / 10)\n return flag ? INT_MAX : INT_MIN;\n res = res * 10 + (str[i] - '0');\n }\n return flag ? res : -res;\n }\n};", "memory": "9800" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int myAtoi(string s) {\n try\n {\n return std::stoi(s);\n }\n catch(const std::invalid_argument& e)\n {\n return 0;\n }\n catch(...)\n {\n if (s.find(\"-\") != std::string::npos)\n {\n return -2147483648;\n }\n return 2147483647;\n }\n }\n};", "memory": "9800" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int myAtoi(string s) {\n //whitespace\n int sol = 0;\n bool minus = false;\n while(s.size() && s[0] ==' ')\n s=s.substr(1);\n cout<<s;\n if(s[0] == '-')\n {\n minus = true;\n s=s.substr(1);\n }\n else if(s[0] == '+') s=s.substr(1);\n while(s.size() && s[0] =='0')\n {\n s=s.substr(1);\n }\n if(s.size()==0 || s[0] < '0' || s[0] > '9') \n return 0;\n\n while(s.size() && s[0] >= '0' && s[0] <= '9')\n { \n if(INT_MAX/10 < sol) return INT_MAX;\n else if(INT_MAX/10 == sol)\n {\n if(s[0] >='8') return INT_MAX;\n }\n else if(INT_MIN/10 > sol) return INT_MIN;\n else if(INT_MIN/10 == sol)\n {\n if(s[0] >='8') return INT_MIN;\n }\n sol*=10;\n if(minus)\n sol -=s[0]-'0';\n else\n sol += s[0] - '0';\n s=s.substr(1);\n\n }\n\n\n return sol;\n }\n};", "memory": "10300" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int myAtoi(string s) {\n\n if (s.empty())\n return 0;\n\n // Ignore any leading whitespace\n std::size_t found = s.find_first_not_of(\" \"); // Find the position of 1st non-whitespace character\n if (found != std::string::npos)\n s = s.substr(found);\n\n // Determine the sign by checking if the next character is '-' or '+'\n int number = 1;\n if (s.at(0) == '-') // Negative number\n {\n number = -1;\n s = s.substr(1);\n }\n else if (s.at(0) == '+') // Positive number, if a number exists\n s = s.substr(1);\n\n // Read the integer (skip leading zeros) until a non-digit character is found or the end of the string is reached\n found = s.find_first_not_of(\"0123456789\");\n s = s.substr(0, found);\n if (!s.empty())\n {\n try\n {\n number *= stoi(s);\n }\n catch (const std::out_of_range& oor )\n {\n if (number == 1)\n number *= pow(2,31) - 1;\n else\n number *= pow(2,31);\n }\n return number;\n }\n\n return 0;\n }\n};", "memory": "10400" }
8
<p>Implement the <code>myAtoi(string s)</code> function, which converts a string to a 32-bit signed integer.</p> <p>The algorithm for <code>myAtoi(string s)</code> is as follows:</p> <ol> <li><strong>Whitespace</strong>: Ignore any leading whitespace (<code>&quot; &quot;</code>).</li> <li><strong>Signedness</strong>: Determine the sign by checking if the next character is <code>&#39;-&#39;</code> or <code>&#39;+&#39;</code>, assuming positivity is neither present.</li> <li><strong>Conversion</strong>: Read the integer by skipping leading zeros&nbsp;until a non-digit character is encountered or the end of the string is reached. If no digits were read, then the result is 0.</li> <li><strong>Rounding</strong>: If the integer is out of the 32-bit signed integer range <code>[-2<sup>31</sup>, 2<sup>31</sup> - 1]</code>, then round the integer to remain in the range. Specifically, integers less than <code>-2<sup>31</sup></code> should be rounded to <code>-2<sup>31</sup></code>, and integers greater than <code>2<sup>31</sup> - 1</code> should be rounded to <code>2<sup>31</sup> - 1</code>.</li> </ol> <p>Return the integer as the final result.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;42&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">42</span></p> <p><strong>Explanation:</strong></p> <pre> The underlined characters are what is read in and the caret is the current reader position. Step 1: &quot;42&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;42&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>42</u>&quot; (&quot;42&quot; is read in) ^ </pre> </div> <p><strong class="example">Example 2:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot; -042&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">-42</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;<u> </u>-042&quot; (leading whitespace is read and ignored) ^ Step 2: &quot; <u>-</u>042&quot; (&#39;-&#39; is read, so the result should be negative) ^ Step 3: &quot; -<u>042</u>&quot; (&quot;042&quot; is read in, leading zeros ignored in the result) ^ </pre> </div> <p><strong class="example">Example 3:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;1337c0d3&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">1337</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;1337c0d3&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;1337c0d3&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>1337</u>c0d3&quot; (&quot;1337&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 4:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;0-1&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <pre> Step 1: &quot;0-1&quot; (no characters read because there is no leading whitespace) ^ Step 2: &quot;0-1&quot; (no characters read because there is neither a &#39;-&#39; nor &#39;+&#39;) ^ Step 3: &quot;<u>0</u>-1&quot; (&quot;0&quot; is read in; reading stops because the next character is a non-digit) ^ </pre> </div> <p><strong class="example">Example 5:</strong></p> <div class="example-block"> <p><strong>Input:</strong> <span class="example-io">s = &quot;words and 987&quot;</span></p> <p><strong>Output:</strong> <span class="example-io">0</span></p> <p><strong>Explanation:</strong></p> <p>Reading stops at the first non-digit character &#39;w&#39;.</p> </div> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>0 &lt;= s.length &lt;= 200</code></li> <li><code>s</code> consists of English letters (lower-case and upper-case), digits (<code>0-9</code>), <code>&#39; &#39;</code>, <code>&#39;+&#39;</code>, <code>&#39;-&#39;</code>, and <code>&#39;.&#39;</code>.</li> </ul>
3
{ "code": "class Solution {\npublic:\n int myAtoi(string s) {\n // Remove whitespace\n while (s != \"\" and s[0] == ' ') {\n s = s.substr(1);\n }\n\n if (s == \"\") {\n return 0;\n }\n\n // Remove sign\n auto sign = 1;\n if (s[0] == '-') {\n sign = -1;\n s = s.substr(1);\n } else if (s[0] == '+') {\n s = s.substr(1);\n }\n\n // Remove leading 0's\n while (s != \"\" and s[0] == '0') {\n s = s.substr(1);\n }\n\n // Conversion\n long result = 0;\n while (s != \"\") {\n char digit = s[0];\n s = s.substr(1);\n\n if (digit < '0' or digit > '9') {\n return result * sign;\n }\n\n int index = digit - '0';\n\n result *= 10;\n result += index;\n\n if (result * sign < -2147483648) {\n return -2147483648;\n } else if (result * sign > 2147483647) {\n return 2147483647;\n }\n }\n\n return result * sign;\n }\n};", "memory": "10500" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
0
{ "code": "int init = [] {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n ofstream out(\"user.out\");\n for (string s; getline(cin, s);) {\n if(s[0]=='-'){\n out<<\"false\\n\";\n continue;\n }\n int i =0;\n int j = s.length()-1;\n bool ans = 1;\n while(i<j){\n if(s[i]!=s[j]){\n ans = false;break;}\n i++;\n j--;\n }\n if(ans){\n out<<\"true\\n\";\n }else{\n out<<\n \"false\\n\";\n }\n // out << ans;\n }\n out.flush();\n exit(0);\n return 0;\n}();\nclass Solution {\npublic:\n bool isPalindrome(int x) {\n return 0;\n }\n};", "memory": "7200" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
0
{ "code": "int init = [] {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n ofstream out(\"user.out\");\n for (string s; getline(cin, s);) {\n if(s[0]=='-'){\n out<<\"false\\n\";\n continue;\n }\n int i =0;\n int j = s.length()-1;\n bool ans = 1;\n while(i<j){\n if(s[i]!=s[j]){\n ans = false;break;}\n i++;\n j--;\n }\n if(ans){\n out<<\"true\\n\";\n }else{\n out<<\n \"false\\n\";\n }\n // out << ans;\n }\n out.flush();\n exit(0);\n return 0;\n}();\nclass Solution {\npublic:\n bool isPalindrome(int x) {\n return 0;\n }\n};", "memory": "7300" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
0
{ "code": "int init = [] {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n ofstream out(\"user.out\");\n for (string s; getline(cin, s);) {\n if(s[0]=='-'){\n out<<\"false\\n\";\n continue;\n }\n int i =0;\n int j = s.length()-1;\n bool ans = 1;\n while(i<j){\n if(s[i]!=s[j]){\n ans = false;break;}\n i++;\n j--;\n }\n if(ans){\n out<<\"true\\n\";\n }else{\n out<<\n \"false\\n\";\n }\n // out << ans;\n }\n out.flush();\n exit(0);\n return 0;\n}();\nclass Solution {\npublic:\n bool isPalindrome(int x) {\n return 0;\n }\n};", "memory": "7400" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
0
{ "code": "int init = [] {\n ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n ofstream out(\"user.out\");\n for (string s; getline(cin, s);) {\n if(s[0]=='-'){\n out<<\"false\\n\";\n continue;\n }\n int i =0;\n int j = s.length()-1;\n bool ans = 1;\n while(i<j){\n if(s[i]!=s[j]){\n ans = false;break;}\n i++;\n j--;\n }\n if(ans){\n out<<\"true\\n\";\n }else{\n out<<\n \"false\\n\";\n }\n // out << ans;\n }\n out.flush();\n exit(0);\n return 0;\n}();\nclass Solution {\npublic:\n bool isPalindrome(int x) {\n return 0;\n }\n};", "memory": "7400" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
0
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if(x<0) return false;\n int temp=x;\n long sum=0;\n\n while(temp!=0){\n sum=sum*10+(temp%10);\n temp/=10;\n }\n\n return x==sum;\n }\n};", "memory": "8100" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
0
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if(x<0){\n return false;\n }\n int original = x;\n double reversed = 0;\n double a ;\n while(original>0){\n \n a = original % 10;\n reversed = reversed * 10 + a;\n original = original/10;\n\n\n }\n if(reversed == x){\n return true;\n }\n else{\n return false;\n }\n }\n};", "memory": "8100" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
0
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if(x<0){\n return false;\n }\n long long t=x;\n long long y = 0;\n long long z = t;\n while(z){\n y = y*10 + z%10;\n z = z/10;\n }\n \n if(t==y)\n return true;\n else\n return false;\n }\n};", "memory": "8200" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
0
{ "code": "class Solution {\npublic:\n int reverseNumber(int x)\n {\n long long reverse = 0;\n int digit;\n while(x>0)\n {\n digit = x%10;\n x/=10;\n reverse = reverse*10 + digit;\n } \n return reverse;\n }\n \n bool isPalindrome(int x) {\n if(x<0)\n return false;\n long reverse = reverseNumber(x);\n if(x==reverse)\n return true;\n return false; \n }\n};", "memory": "8200" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
0
{ "code": "class Solution {\npublic:\n int reverse(int x){\n long r = 0;\n while(x){\n r = r*10 + x%10;\n x = x/10;\n }\n if(r<INT_MIN || r>INT_MAX)\n return 0;\n \n return int(r);\n }\n\n bool isPalindrome(int x) {\n if(x<0)\n return false;\n int rev = reverse(x);\n if(rev == x)\n return true;\n \n return false;\n }\n};", "memory": "8300" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
0
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n // If x is negative, it can't be a palindrome\n if (x < 0) {\n return false;\n }\n \n int dp = x; // Store the original number\n int rv = 0; // To store the reversed number\n \n while (x > 0) {\n int ld = x % 10; // Get the last digit\n \n // Check if reversing will cause overflow\n if (rv > (INT_MAX - ld) / 10) {\n return false; // Return false if overflow is detected\n }\n \n rv = (rv * 10) + ld; // Append the last digit to reversed number\n x /= 10; // Remove the last digit from original number\n }\n \n // Compare the reversed number with the original\n return dp == rv;\n }\n};\n", "memory": "8300" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
0
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if(x<0) return false;\n long long num = x;\n long long ans = 0;\n while(x){\n int digit = x%10;\n ans = ans*10 + digit;\n x /= 10; \n }\n return ans == num;\n }\n};\n \n", "memory": "8400" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
0
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if(x<0) return false;\n long long aux = x;\n long long inv = 0;\n while(aux!=0)\n {\n int c = aux % 10;\n inv = inv*10 + c;\n aux = aux / 10;\n }\n if(inv == x) return true;\n else return false;\n }\n};", "memory": "8400" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
0
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if(x<0) return false;\n long long num = x;\n long long ans = 0;\n while(x){\n int digit = x%10;\n ans = ans*10 + digit;\n x /= 10; \n }\n return ans == num;\n }\n};", "memory": "8500" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
0
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n long y=0;\n if(x<0){\n return false;\n }\n long c=x;\n while(c!=0){\n y=y*10;\n y=(c%10)+y;\n c=c/10;\n }\n if(y==x){\n return true;\n }\n return false;\n }\n};", "memory": "8500" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
2
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if (x == 0)\n return true;\n int y = x;\n long long n = 0;\n while (x > 0)\n {\n n = n * 10 + x % 10;\n x /= 10;\n }\n if (n == y)\n return true;\n else\n return false;\n }\n};", "memory": "8600" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
2
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n long long reversed = 0;\n if(x == 0){\n return true;\n }\n if(x<0){\n return false;\n }\n long long temp = x;\n while(temp != 0){\n reversed = (reversed * 10) + (temp % 10);\n temp /= 10;\n }\n return reversed == x;\n\n }\n};", "memory": "8600" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
2
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n int cnt = 0;\n int y = x;\n if (x < 0)\n return false;\n while (y) {\n y = y / 10;\n cnt++;\n }\n int cnt1 = cnt;\n int z = x / (pow(10, (cnt - 1)));\n for (int i = 0; i < cnt1/2; i++) {\n if (z != (x % 10)) {\n return false;\n } else {\n x = x % (int(pow(10, (cnt - 1))));\n x = x / 10;\n cnt = cnt - 2;\n if (int(x / (pow(10, cnt-1))) == 0) {\n z = 0;\n } else {\n z = x / (pow(10, (cnt - 1)));\n }\n \n }\n }\n return true;\n }\n};", "memory": "8700" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
2
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n int b=x;\n int n=0;\n if (b<0)\n return false;\n if (b==0)\n return true;\n while (b>0){\n b=b/10;\n n++;\n }\n b=x;\n int rer = 0;\n while (b>0){\n rer = pow(10,n-1);\n if (b%10!=b/rer)\n return false;\n b=b%rer;\n b=b/10;\n n-=2;\n }\n return true;\n }\n};", "memory": "8700" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
2
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n int b=x;\n int n=0;\n if (b<0)\n return false;\n if (b==0)\n return true;\n while (b>0){\n b=b/10;\n n++;\n }\n b=x;\n while (b>0){\n int rer = pow(10,n-1);\n if (b%10!=b/rer)\n return false;\n b=b%rer;\n b=b/10;\n n-=2;\n }\n return true;\n }\n};", "memory": "8800" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution\n{\npublic:\n bool isPalindrome(int x)\n {\n if (x < 0)\n return false;\n bool flag = true;\n int size = 0;\n int num = x;\n while (num > 0)\n {\n size++;\n num = num / 10;\n }\n int* arr;\n arr = new int[size];\n num = x;\n for (int i = size - 1;i >= 0;i--)\n {\n arr[i] = num % 10;\n num = num / 10;\n }\n for (int i = 0, j = size - 1; i<size / 2, j>=size / 2; i++, j--)\n {\n if (arr[i] != arr[j])\n {\n flag = false;\n }\n }\n return flag;\n }\n};", "memory": "9300" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if(x<0) return 0;\n string s = \"\";\n int prod=1;\n while(x){\n s.push_back(char(x%10+48));\n x = (x-(x%10))/10;\n }\n for(int i =0,j=s.size()-1; i<=j; i++,j--){\n prod *= (s[i]==s[j])? 1: 0;\n }\n return prod;\n }\n};", "memory": "9300" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if (x<0) return false;\n int n =0;\n int y = 0;\n string s= \"\";\n if(x==0) return true;\n while(x>0){\n s+= x%10+48;\n x/=10;\n n++;\n }\n if(n%2){\n for(int i=0;i<n/2;i++){\n if(s[i]!=s[n-i-1]) return false;\n }\n }\n else{\n for(int i=0;i<=n/2;i++){\n if(s[i]!=s[n-i-1]) return false;\n }\n }\n \n return true;\n }\n};", "memory": "9400" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n string reverse=\"\";\n bool palindrome=true;\n int dummy=x;\n while(x!=0){\n int last_digit=x%10;\n reverse+=(char)(last_digit+'0');\n x=x/10;\n }\n if(dummy>0){\n for(int i=0;i<reverse.size()/2;i++){\n if(reverse[i]!=reverse[reverse.size()-1-i]){\n palindrome=false;\n break;\n }\n }\n }\n else if(dummy<0){\n palindrome=false;\n }\n else{\n palindrome=true;\n }\n return palindrome;\n }\n};", "memory": "9400" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n\tif (x < 0) \n\t\treturn false;\n\t\n\tint head = x;\n\tint head2 = x;\n\tint tail = 0;\n\tint count = 0;\n\twhile (head > 0) {\n\t\thead /= 10;\n\t\tcount++;\n\t}\n\tint *arr = new int[count];\n\tfor (int i = 0; i < count; i++) {\n\t\ttail = head2 % 10;\n\t\thead2 /= 10;\n\t\tarr[i] = tail;\n\t}\n\tfor (int i = 0; i < count; i++) {\n\t\tif (arr[i] != arr[count - (i + 1)])\n\t\t\treturn false;\n\t}\n\tdelete[] arr;\n\treturn true;\n\n }\n};", "memory": "9500" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if (x < 0) {\n return false; // Negative numbers are not palindromes\n }\n\n int arr[50];\n int counter = 0;\n int original = x; // Save the original value of x for later use\n\n // Extract digits into the array\n while (x > 0) {\n arr[counter++] = x % 10;\n x /= 10;\n }\n\n // Compare the digits from start and end\n for (int i = 0; i < counter / 2; i++) {\n if (arr[i] != arr[counter - i - 1]) {\n return false; // Early return if a mismatch is found\n }\n }\n\n return true; // If all digits match, it's a palindrome\n }\n};\n", "memory": "9600" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n //check for negative int\n if(x<0){\n return false;\n }\n int digits[100];\n int index=0;\n while(x>0){\n digits[index]=x%10;\n x=x/10;\n index++;\n }\n int left=0;\n int right=index-1;\n\n while(left<right){\n if(digits[left]!=digits[right]){\n return false;\n }\n left++;\n right--;\n }\n\n return true;\n }\n};", "memory": "9600" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if(x<0){return false;}\n int a[31];\n int count = 0;\n while(x>9){\n a[count] = x % 10;\n count++;\n x=x/10;\n }\n a[count] = x;\n int i =0;\n bool detected = false;\n for(i; i<=count/2; i++){\n if(a[i]==a[count-i]){\n continue;\n } else {\n detected = true;\n break;\n }\n }\n if(detected){\n return false;\n } else {\n return true;\n }\n }\n};", "memory": "9700" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n int arr[100];\n int rem=x;\n int count=0;\n if(x<0){\n return false;\n }\n while(rem>0){\n arr[count]=rem%10;\n rem=rem/10;\n count++;\n }\n for (int l = 0, r = count - 1; l < r; l++, r--) {\n if (arr[l] != arr[r]) {\n return false;\n }\n }\n \n return true;\n }\n};", "memory": "9700" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n\tbool isPalindrome(int x) {\n\n\t\tif ((x < 0) || (x == 10)||x == 100 || x == 1000 || x == 10000 || x == 100000 || x == 1000000) {\n\t\t\treturn false;\n\t\t}\n\t\tif ((x < 10) && (x > 0)) {\n\t\t\treturn true;\n\t\t}\n \n\n\t\tint b = x;\n\t\tint k = 1;\n\t\twhile (b > 9) {\n\t\t\tb = b / 10;\n\t\t\tk += 1;\n\t\t}\n\t\tint num1 = x;\n\t\tint i = 0;\n\t\tint array[99];\n\t\twhile (x > 0) {\n\t\t\tnum1 = x % 10;\n\t\t\tx = x / 10;\n\t\t\tarray[i] = num1;\n\t\t\ti++;\n\t\t}\n\t\tint n = 0;\n\t\tint m = 0;\n\t\tint res = k / 2;\n\t\twhile (n < res) {\n\t\t\tint testn1 = array[n];\n\t\t\tint testn2 = array[k - 1];\n\t\t\tif (testn1 == testn2) {\n\t\t\t\tk -= 1;\n\t\t\t\tn += 1;\n\t\t\t}\n\t\t\telse {\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t}\n\t\treturn true;\n\t}\n};", "memory": "9800" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if (x < 0)\n return false;\n if (x > -1 && x < 10)\n return true;\n \n vector<int> digits;\n digits.reserve(16);\n\n while(x > 0){\n digits.push_back(x % 10);\n x = x / 10;\n }\n \n vector<int>::iterator rising = digits.begin();\n vector<int>::iterator falling = digits.end() - 1;\n\n while (rising <= falling) {\n if (*rising != *falling)\n return false;\n \n ++rising;\n --falling;\n }\n\n return true;\n }\n};", "memory": "9900" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n\tbool isPalindrome(int x) {\n\n\t\tif ((x < 0) || (x == 10) || x == 100 || x == 1000 || x == 10000 || x == 100000 || x == 1000000) {\n\t\t\treturn false;\n\t\t}\n\t\tif ((x < 10) && (x > 0)) {\n\t\t\treturn true;\n\t\t}\n\n\n\t\tint b = x;\n\t\tint k = 1;\n\t\twhile (b > 9) {\n\t\t\tb = b / 10;\n\t\t\tk += 1;\n\t\t}\n\t\tint num1 = x;\n\t\tint i = 0;\n\t\tint *array = new int[20];\n\t\twhile (x > 0) {\n\t\t\tnum1 = x % 10;\n\t\t\tx = x / 10;\n\t\t\tarray[i] = num1;\n\t\t\ti++;\n\t\t}\n\t\tint n = 0;\n\t\tint m = 0;\n\t\tint res = k / 2;\n\t\twhile (n < res) {\n\t\t\tint testn1 = array[n];\n\t\t\tint testn2 = array[k - 1];\n\t\t\tif (testn1 == testn2) {\n\t\t\t\tk -= 1;\n\t\t\t\tn += 1;\n\t\t\t}\n\t\t\telse {\n\t\t\t\tdelete[] array;\n\t\t\t\tarray = nullptr;\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t}\n\t\treturn true;\n\t}\n};", "memory": "10000" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if(x < 0)\n return false;\n \n string str = to_string(x);\n size_t i{}, len = str.length();\n --len;\n while(i < len) {\n if(str[i] != str[len]) {\n return false;\n }\n ++i;\n --len;\n }\n\n return true;\n }\n};", "memory": "10100" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if (x < 0) return false; // minus\n if (x == 0) return true; // 0\n\n // 1. store digits of integer to array;\n //std::vector<int> digits;\n\n // To string\n std::string str = std::to_string(x);\n //std::reverse(str.begin(), str.end());\n\n int leftPos = 0;\n int rightPos = str.length() - 1;\n\n while(leftPos < rightPos)\n {\n if (str[leftPos] != str[rightPos])\n return false;\n\n leftPos++;\n rightPos--;\n }\n\n return true;\n }\n};", "memory": "10200" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n string str=to_string(x);\n int s=0;\n int e=str.size()-1;\n\n while(s<e){\n if(str[s]!=str[e]){\n return false;\n }\n else {\n s++;\n e--;\n }\n }\n return true;\n\n }\n};", "memory": "10200" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n string s=to_string(x);\n for(int i=0;i<=(s.size()-1)/2;i++){\n if(s[i]!=s[s.size()-1-i]){\n return false;\n }\n }\n return true;\n }\n};", "memory": "10300" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n\n if(x<0) return false;\n\n std::string s = std::to_string(x);\n int size = s.size();\n int end = size-1;\n int start1 = 0;\n \n while(start1<end){\n if(isalnum(s[start1]) !=0 && isalnum(s[end]) !=0 ){\n if(isalpha(s[start1]) !=0 && isalpha(s[end]) !=0 ){\n if((char)tolower(s[start1]) == (char)tolower(s[end])){\n start1++;\n end--;\n }\n else{\n return false;\n }\n }\n else if(s[start1]==s[end]){\n start1++;\n end--;\n }\n else{\n return false;\n }\n }\n if(isalnum(s[start1]) == 0){\n start1++;\n }\n if(isalnum(s[end]) == 0){\n end--;\n }\n }\n\n return true;\n \n }\n};", "memory": "10300" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if (x < 0) return false; // minus\n if (x == 0) return true; // 0\n\n // 1. store digits of integer to array;\n //std::vector<int> digits;\n\n // To string\n std::string str = std::to_string(x);\n std::reverse(str.begin(), str.end());\n\n int leftPos = 0;\n int rightPos = str.length() - 1;\n\n while(leftPos < rightPos)\n {\n if (str[leftPos] != str[rightPos])\n return false;\n\n leftPos++;\n rightPos--;\n }\n\n return true;\n }\n};", "memory": "10400" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n string number = to_string(x);\n for(int i=0 ; i<number.size() ; i++){\n if(number[i] != number[number.size()-i-1]){\n //std::cout<<number[i]<<\" \"<<number[number.size()-i];\n return(false);\n }\n }\n return(true);\n }\n};", "memory": "10400" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n string s=std::to_string(x);\n int left=0;\n int right=s.length()-1;\n while(left<right){\n if(s[left]!=s[right]){\n return false;\n }\n left+=1;\n right-=1;\n }\n return true;\n }\n};", "memory": "10500" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n string s = to_string(x);\n int l = 0;\n int r = s.length()-1;\n while(l<r){\n if(s[l] != s[r]){\n return false;\n } \n l++;\n r--;\n }\n return true;\n \n\n }\n};", "memory": "10500" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if(x < 0) return false;\n if(x < 10) return true;\n string s = to_string(x);\n int len = s.length();\n for(int i = 0; i < len/2; i++){\n if(s[i] != s[len - i - 1]) return false;\n }\n return true;\n\n /*\n int y = x, digits = 0;\n while(y > 0){\n y /= 10;\n digits++;\n }\n int left = pow(10, digits), right = 1;\n for(int i = 0; i < digits/2; i++){\n\n }\n */\n }\n};", "memory": "10600" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "/*\nOriginal solution. JV.\n*/\n\nclass Solution {\npublic:\n bool isPalindrome(int x) {\n ios::sync_with_stdio(false); \n cin.tie(NULL);\n cout.tie(NULL);\n \n string y = to_string(x);\n\n for(int i = 0, j = y.size() - 1; i < j; i++, j--){\n if(y[i] != y[j]){\n return false;\n }\n }\n\n return true;\n }\n};", "memory": "10600" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n string num = to_string(x);\n int arrlen = num.length();\n if (arrlen % 2 == 0 && x % 11 != 0) {\n return false;\n }\n for(int i = 0; i < arrlen /2 ; i++) {\n char temp = num[i];\n num[i] = num[arrlen - i - 1];\n num[arrlen - 1 - i] = temp;\n cout<<num<<endl;\n\n }\n try {\n int number = std::stoi(num);\n }catch (const std::out_of_range& e) {\n return false;\n }\n int x2 = std::stoi(num);\n if(x2 > 2147483647 || x2 < -2147483648){\n return false;\n }\n if(x2 == x) {\n // cout<<\"Palindrome\"<<endl;\n return true;\n }\n // cout<<\"Not a palindrome\"<<endl;\n return false;\n }\n};", "memory": "10700" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if (x < 0){\n return false;\n }\n\n string num = to_string(x);\n int len = num.length();\n int half = len / 2;\n if (len % 2 == 0){ // even\n reverse(num.begin(), num.begin() + half);\n if (num.substr(0, half) == num.substr(half, half)){\n return true;\n }\n }\n else { // odd\n reverse(num.begin(), num.begin() + half);\n if (num.substr(0, half) == num.substr(half + 1, half)){\n return true;\n }\n }\n \n\n return false;\n }\n};", "memory": "10700" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if(x < 0)\n return 0;\n \n string nums = \"\";\n while(x)\n {\n nums.push_back(char(x%10));\n x /= 10;\n }\n string temp = nums;\n reverse(nums.begin(), nums.end());\n return nums == temp;\n }\n};", "memory": "10800" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "#include <string>\n\nclass Solution {\npublic:\n bool isPalindrome(int x) {\n std::string num = std::to_string(x);\n int length = num.length();\n std::string num1 = num.substr(0, length/2);\n std::string num2;\n if(length % 2 == 0)\n num2 = num.substr(length/2, length);\n else\n num2 = num.substr(length/2+1, length);\n for (int i = 0; i < num1.length(); i ++)\n {\n if (num1[i] != num2[num1.length()-1-i])\n return false;\n }\n return true;\n }\n};", "memory": "10900" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n if(x < 0)\n return 0;\n \n string nums = \"\";\n while(x)\n {\n nums.push_back(char(x%10));\n x /= 10;\n }\n string temp = nums;\n reverse(nums.begin(), nums.end());\n return nums == temp;\n }\n};", "memory": "10900" }
9
<p>Given an integer <code>x</code>, return <code>true</code><em> if </em><code>x</code><em> is a </em><span data-keyword="palindrome-integer"><em><strong>palindrome</strong></em></span><em>, and </em><code>false</code><em> otherwise</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> x = 121 <strong>Output:</strong> true <strong>Explanation:</strong> 121 reads as 121 from left to right and from right to left. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> x = -121 <strong>Output:</strong> false <strong>Explanation:</strong> From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> x = 10 <strong>Output:</strong> false <strong>Explanation:</strong> Reads 01 from right to left. Therefore it is not a palindrome. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>-2<sup>31</sup>&nbsp;&lt;= x &lt;= 2<sup>31</sup>&nbsp;- 1</code></li> </ul> <p>&nbsp;</p> <strong>Follow up:</strong> Could you solve it without converting the integer to a string?
3
{ "code": "class Solution {\npublic:\n bool isPalindrome(int x) {\n long long reversed = 0;\n long long temp = abs(x);\n while(temp!=0)\n {\n reversed+= (temp%10)*pow(10,std::to_string(temp).length()-1);\n temp/=10;\n }\n if(reversed == x) return true;\n \n return false;\n }\n};", "memory": "11000" }