英文:
I have to check pallindrome string.I am getting a wrong answer when I enter String having numeric values.For ex."0P"
问题
class Solution
{
public boolean isPalindrome(String s)
{
char c, ch;
String s1 = "";
String s2 = "";
s = s.trim();
s = s.toLowerCase();
if (s == "")
return true;
for (int i = 0; i < s.length(); i++)
{
c = s.charAt(i);
if ((c >= 97 && c <= 122) || (c >= 0 && c <= 9))
s1 = s1 + c;
}
for (int j = s1.length() - 1; j >= 0; j--)
{
ch = s1.charAt(j);
s2 = s2 + ch;
}
if (s1.equals(s2))
return true;
else
return false;
}
}
英文:
I have to ignore all special characters in the string.I have created a new string s1 including only alphanumerics characters.Then,made a reverse string s2 and then checked if they are pallindrome or not.
class Solution
{
public boolean isPalindrome(String s)
{
char c,ch;
String s1="";
String s2="";
s=s.trim();
s=s.toLowerCase();
if(s=="")
return true;
for(int i=0;i<s.length();i++)
{
c=s.charAt(i);
if(c>=97&&c<=122||c>=0&&c<=9)
s1=s1+c;
}
for(int j=s1.length()-1;j>=0;j--)
{
ch=s1.charAt(j);
s2=s2+ch;
}
if(s1.equals(s2))
return true;
else
return false;
}
}
答案1
得分: 1
String str = "@ Test!#@!#!@92432432";
String tmp = str.replaceAll("[^a-zA-Z0-9]", "");
System.out.println(tmp);
Prints
Test92432432
Ref:
https://stackoverflow.com/questions/31716511/how-to-ignore-special-characters-and-spaces-in-string
英文:
String str = "@ Test!#@!#!@92432432";
String tmp = str.replaceAll("[^a-zA-Z0-9]", "");
System.out.println(tmp);
Prints
Test92432432
Ref:
https://stackoverflow.com/questions/31716511/how-to-ignore-special-characters-and-spaces-in-string
答案2
得分: 0
根据我理解,您正在使用 c>=0&&c<=9
来检查 c
是否为数字。这是错误的,因为 '0' == 48
,而 '9' == 57
,您应该使用 c>=48 && c<=57
。
以下是您代码部分的翻译:
我想为您的代码提供一些评论:
1. 在 Java 中,字符串是不可变对象,大量的字符串连接是一种非常不好的做法。请使用 StringBuilder。
2. 您可以使用 `s.isEmpty()` 来代替 `s==""`。
3. `Character` 类有静态方法 `isDigit` 和 `isAlphabetic`,用于检查字符是否为数字或字母。
4. 如果您使用 StringBuilder,可以通过 `stringBuilder.reverse()` 方法来反转字符串。
5. 在方法的结尾,如果 `s1.equals(s2)` 则返回 true,否则返回 false。您可以直接使用 `return s1.equals(s2);`。
6. 您可以使用 `for (char c : s.toCharArray())` 循环来迭代遍历字符串。
最终的代码如下:
```java
public static boolean isPalindrome(String s)
{
s=s.trim();
s=s.toLowerCase();
if(s.isEmpty())
return true;
StringBuilder sanitizedString = new StringBuilder();
for (char c : s.toCharArray()) {
if(Character.isAlphabetic(c) || Character.isDigit(c))
sanitizedString.append(c);
}
String s1 = sanitizedString.toString();
String s2 = sanitizedString.reverse().toString();
return s1.equals(s2);
}
您还可以使用 @RR_IL 的答案中提到的正则表达式来避免循环。
英文:
As I understand, you are using c>=0&&c<=9
for checking c
for digit. It is wrong, cause '0' == 48
and '9' == 57
and you have to use c>=48 && c<=57
And I want to give you some comments about your code:
- String is an immutable object in java and a lot of string
concatenating - is a very bad practice. Please use StringBuilder. - You can use
s.isEmpty()
instead ofs==""
Character
class has static methodsisDigit
andisAlphabetic
, whic are checks char for digit or alphabetic- If you will use StringBuilder, you can invert string just by
stringBuilder.reverse()
method - At the end of the method, you return true if
s1.equals(s2)
and false - overwise. You can just usereturn s1.equals(s2);
- And you can iterating through the string with
for (char c : s.toCharArray())
cycle
And the final code is
public static boolean isPalindrome(String s)
{
s=s.trim();
s=s.toLowerCase();
if(s.isEmpty())
return true;
StringBuilder sanitizedString = new StringBuilder();
for (char c : s.toCharArray()) {
if(Character.isAlphabetic(c) || Character.isDigit(c))
sanitizedString.append(c);
}
String s1 = sanitizedString.toString();
String s2 = sanitizedString.reverse().toString();
return s1.equals(s2)
}
And you can use regexp from @RR_IL answer for avoiding the cycle.
答案3
得分: 0
以下是翻译好的代码:
public static boolean isPalindrome(String s)
{
int i = 0;
int j = s.length() - 1;
while (i < j) {
if (s.charAt(i) != s.charAt(j))
return false;
i++;
j--;
}
return true;
}
英文:
You can use something like this:
public static boolean isPalindrome(String s)
{
int i = 0;
int j = s.length() - 1;
while (i < j) {
if (s.charAt(i) != s.charAt(j))
return false;
i++;
j--;
}
return true;
}
专注分享java语言的经验与见解,让所有开发者获益!
评论