I have to check pallindrome string.I am getting a wrong answer when I enter String having numeric values.For ex."0P"

huangapple 未分类评论47阅读模式
英文:

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=&quot;&quot;;
        String s2=&quot;&quot;;
        s=s.trim();
        s=s.toLowerCase();
        if(s==&quot;&quot;)
            return true;
        for(int i=0;i&lt;s.length();i++)
        {
            c=s.charAt(i);
            if(c&gt;=97&amp;&amp;c&lt;=122||c&gt;=0&amp;&amp;c&lt;=9)
                s1=s1+c;
        }
        for(int j=s1.length()-1;j&gt;=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 = &quot;@ Test!#@!#!@92432432&quot;;
String tmp = str.replaceAll(&quot;[^a-zA-Z0-9]&quot;, &quot;&quot;);
System.out.println(tmp);  

Prints

Test92432432

Ref:
https://stackoverflow.com/questions/31716511/how-to-ignore-special-characters-and-spaces-in-string

答案2

得分: 0

根据我理解,您正在使用 c&gt;=0&amp;&amp;c&lt;=9 来检查 c 是否为数字。这是错误的,因为 &#39;0&#39; == 48,而 &#39;9&#39; == 57,您应该使用 c&gt;=48 &amp;&amp; c&lt;=57


以下是您代码部分的翻译:

我想为您的代码提供一些评论

 1. 在 Java 中字符串是不可变对象大量的字符串连接是一种非常不好的做法请使用 StringBuilder
 2. 您可以使用 `s.isEmpty()` 来代替 `s==&quot;&quot;`。
 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&gt;=0&amp;&amp;c&lt;=9 for checking c for digit. It is wrong, cause &#39;0&#39; == 48 and &#39;9&#39; == 57 and you have to use c>=48 && c<=57


And I want to give you some comments about your code:

  1. String is an immutable object in java and a lot of string
    concatenating - is a very bad practice. Please use StringBuilder.
  2. You can use s.isEmpty() instead of s==&quot;&quot;
  3. Character class has static methods isDigit and isAlphabetic, whic are checks char for digit or alphabetic
  4. If you will use StringBuilder, you can invert string just by stringBuilder.reverse() method
  5. At the end of the method, you return true if s1.equals(s2) and false - overwise. You can just use return s1.equals(s2);
  6. 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 &lt; j) { 
		if (s.charAt(i) != s.charAt(j)) 
			return false; 
		i++; 
		j--; 
	}
	return true; 
}

huangapple
  • 本文由 发表于 2020年3月16日 02:00:05
  • 转载请务必保留本文链接:https://java.coder-hub.com/60696002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定