Skip to content

Commit 36168b4

Browse files
#324 solved
1 parent f37e145 commit 36168b4

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Arrays/add_reverse_integer.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
2+
3+
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
4+
5+
6+
7+
Example 1:
8+
9+
Input: x = 123
10+
Output: 321
11+
Example 2:
12+
13+
Input: x = -123
14+
Output: -321
15+
*/
16+
17+
18+
class Solution {
19+
public boolean validPalindrome(String s) {
20+
int i=0,j=s.length()-1; // initialize two pointers, i and j to the start and end of the string respectively
21+
while(i<j){ // loop until i is less than j
22+
if(s.charAt(i)!=s.charAt(j)) // if the characters at i and j are not equal
23+
return isPalindrome(s,i+1,j) || isPalindrome(s,i,j-1); // check if the string is a palindrome by removing either the character at i or j and return true if it is a palindrome
24+
j--;i++; // update the pointers
25+
}
26+
return true; // if the loop completes without returning false, the string is a palindrome
27+
}
28+
public static boolean isPalindrome(String ss,int i, int j){ // a helper function to check if a given substring is a palindrome
29+
while(i<j){
30+
if(ss.charAt(i)!= ss.charAt(j)) return false; // if the characters at i and j are not equal, the substring is not a palindrome
31+
i++;j--; // update the pointers
32+
}
33+
return true; // if the loop completes without returning false, the substring is a palindrome
34+
}
35+
}
36+

0 commit comments

Comments
 (0)