0

これまで、文字列に連続した昇順または降順の文字があるかどうかを確認するために、このコードを作成しました。これらは、入力が連続しているか、または 89012 のような文字列ではないか、または xyZabc が連続としてカウントされるかを示す不変条件です。一方、09283dgdDDf は連続しているとは見なされません。AbCBa や 1abC などのコーナー ケースは false を返す必要があります。一方、DcbaZ は true を返す必要があります

これまでのところ、私のコードは文字部分だけのものであることに注意してください。この初心者が機能するように助けてください。エラーがあります

import java.util.Scanner;
public class generalizedorder {

    public static void main(String[] args)
    {
        java.util.Scanner reader = new java.util.Scanner(System.in);
                 System.out.println("Enter the string");
                 String s = reader.next();
    }
public boolean checkForAscendingOrDescendingPart(String s, int l)
{
    for (int i = 0; i <= s.length() - l; ++i)
    {
        boolean success = true;
        char c = s.charAt(i);
        for (int j = 1; j < l; ++j)
        {
            if (((char) c + j) != s.charAt(i + j))
            {
                success = false;
                break;
            }
        }
        if (success) return true;

        success = true;

        for (int j = 1; j < l; ++j)
        {
            if (((char) c - j) != s.charAt(i + j))
            {
                success = false;
                break;
            }
        }
        if (success) return true;
    }
    return false;

}}
system.out.println(checkForAscendingOrDescendingPart);

}}
4

1 に答える 1

0

私は次の仮定を行いました(あなたの説明から):

  • 89012 - 「012」のため連続
  • xyZabc - 「abc」のため連続です (「xyZ」ではありません - 大文字と小文字が区別されます)
  • 09283dgdDDf - ではない
  • AbCBa または 1abC - そうではありません (なぜですか? - 大文字と小文字を区別する PLUS は、最小 3 文字の連続を前提としています。)
  • DcbaZ - 「cba」で連続

したがって、最初から最後まで反復し、n最大許容連続出現数を考慮して簡単な計算を行うことで、それを行うことができます。

「1」の ASCI 差を持つ連続した文字。(ネガティブまたはポジティブ)。「abc」と「cba」を連続してレンタルしたいので、一般的なことは次のとおりです。それらの ASCI 差の合計は 2 またはマイナス 2 になります。「4 文字」を使用する場合、合計は 3 になります。またはマイナス3。

  • ab -> 最大 ASCI 差:1
  • abc -> 最大 ASCI 差:2
  • cba -> 最大 ASCI 差:-2
  • abcX012 -> 最大 ASCI 差異:2

|n-1|したがって、連続した文字を比較するときにヒットしたくありません。ここnで、連続した文字の最大許容出現回数は次のとおりです。

public boolean isConsecutive(String stringToTest, int n) {
    int sumOfDifferences = 0;
    int lastTemp = 0;
    Boolean consecutive = false;

    for (int i = 0; i < stringToTest.length() - 1; i++) {
        // Whenever the difference is 1 or minues 1, add it to sumOfDifferences.
        // when sumOfDifferences= |max -1| -> break: consecutive!
        // when difference > 1 or difference < -1 : Reset sumOfDifferences.
        // when temp != lastTemp : Reset sumOfDifferences.
        int temp = stringToTest.charAt(i) - stringToTest.charAt(i + 1);

        if (temp != lastTemp) {
            // ASCI directon change. reset. Otherwhise 214 will be consecutive. when comparing for 3 characters. (sumOfdifference would be 2 (-1 + 3)
            sumOfDifferences = 0;
        }

        if (temp == 1 || temp == -1) {
            sumOfDifferences += temp;
        } else {
            // way off. reset.
            sumOfDifferences = 0;
        }

        // sumOfDiff == |n-1| ?
        if (sumOfDifferences == n - 1 || sumOfDifferences == ((n - 1) * -1)) {
            consecutive = true;
            break;
        }

        lastTemp = temp;
    }
    return consecutive;
}

次のテストは陽性です。

@Test
public void testcons() {
    assertEquals(true, isConsecutive("89012", 3));
    assertEquals(true, isConsecutive("xyZabc", 3));
    assertEquals(false, isConsecutive("09283dgdDDf", 3));
    assertEquals(false, isConsecutive("AbCBa", 3));
    assertEquals(false, isConsecutive("1abC", 3));
    assertEquals(true, isConsecutive("DcbaZ", 3));

    assertEquals(true, isConsecutive("ab", 2));
    assertEquals(true, isConsecutive("abc", 3));
    assertEquals(true, isConsecutive("abcd", 4));
    assertEquals(true, isConsecutive("abcde", 5));
    assertEquals(true, isConsecutive("abcdef", 6));
}

これは、数値文字列とアルファベット文字列の両方で機能します。(英数字でも構いませんが、これを望まない場合は事前に除外できます)

于 2013-10-20T01:01:12.550 に答える