【问题标题】:Debug: How to delete consecutively occurring characters in a string?调试:如何删除字符串中连续出现的字符?
【发布时间】:2020-11-22 16:18:07
【问题描述】:

下面是我删除字符串中连续出现的字符但没有得到预期结果的代码...

import java.util.*;
import java.lang.*;
import java.io.*;
           
class GFG {
    public static void main (String[] args)  {
    
        Scanner sc= new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0){
            String name= sc.next();
            char[] c = new char[name.length()];
            int j=0;
            boolean check=true;
            //looping through the array to find duplicates 
            for(int i=0;i<name.length()-1;i++){
                if(name.charAt(i)==name.charAt(i+1)){
                     continue;
                }
                else{
                   c[j]=name.charAt(i);
                   j++;
                   check=false;
                }
            }
            //printing the char array
            if(check==true){
                System.out.println(name);
            }else{
                for(int i=0;i<j+1;i++){
                    System.out.print(c[i]);
                }
                System.out.print(name.charAt(name.length()-1));
                System.out.println();
            }
        }
    }
}
       

预期结果:我的代码应该删除连续出现的字符并打印结果,例如如果输入为caaaabaaad,则输出应为cabad

【问题讨论】:

  • 请使用问题主体提出问题并简要介绍您面临的问题?
  • 您能描述一下您的代码所面临的问题吗?它是否无法编译,返回与预期不同的结果(对于什么输入,什么是不正确的结果)?
  • 我的代码的问题是它不会在多个测试用例的情况下产生结果。代码在产生一些测试用例的输出后停止。
  • 请勿在 cmets 中提供此类信息!请阅读minimal reproducible example 并相应地完善您的问题。
  • 并提示:为变量使用多个字符。这不是数学公式!代码需要人类阅读,这样的缩写没有意义。

标签: java string algorithm loops duplicates


【解决方案1】:

您可以使用这种方法:

class GFG {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        String name = sc.next();
        String result = "";

        if (name.length() > 0)
            result += name.charAt(0);
        //looping through the array to find duplicates
        for (int i = 1; i < name.length(); i++){
            if (name.charAt(i) == name.charAt(i - 1)){
                continue;
            } else {
                result+=name.charAt(i);
            }
        }
        //printing the result
        System.out.print(result);
    }
}

【讨论】:

    【解决方案2】:

    另一种选择是使用 java 8 流。

    步骤:

    1. index = 1 开始一个IntStream 直到结束。 (因为结果值最初等于索引 0 处的字符)
    2. 如果字符与最后看到的字符不匹配,则将字符添加到最终结果中。
    import java.util.*;
    import java.util.stream.IntStream;
    class GFG {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int t = sc.nextInt();
            while (t-- > 0) {
                String name = sc.next();
                String result = IntStream.range(1, name.length())
                                        .mapToObj(i -> name.charAt(i) + "")
                                        .reduce(name.charAt(0) + "", (p, s) -> p.lastIndexOf(s) == p.length() - 1 ? p : p + s);
                System.out.println(result);
            }
        }
    }
    

    注意:它使用简单的字符串连接。

    【讨论】:

      【解决方案3】:

      好的,你的代码有一些问题。

      让我们将行号标记如下:

      01  char[] c = new char[name.length()];
      02  int j=0;
      03  boolean check=true;
      04  //looping through the array to find duplicates 
      05  for(int i=0;i<name.length()-1;i++){
      06      if(name.charAt(i)==name.charAt(i+1)){
      07           continue;
      08      }
      09      else{
      10         c[j]=name.charAt(i);
      11         j++;
      12         check=false;
      13      }
      14  }
      15  //printing the char array
      16  if(check==true){
      17      System.out.println(name);
      18  }else{
      19      for(int i=0;i<j+1;i++){
      20          System.out.print(c[i]);
      21      }
      22      System.out.print(name.charAt(name.length()-1));
      23      System.out.println();
      24  }
      

      第一个问题是第 1、19、20、21、22 行的组合。 我知道这些行是这样放置的,因为您不知道缩减字符串的长度。因此,您使用 for 循环使事情复杂化,然后从原始字符串中获取最后一个字符。但是,如果您的字符串以 2 个相同的字符结尾怎么办?

      如果我没记错的话,它会打印两次。

      第二个问题是您在不同的块中处理不同的案例。 这些都不需要。

      让我们同时处理所有这些问题。

      01  StringBuilder sb = new StringBuilder();
      02  int lastChar = 65537; // This char will never exist as char is 16 bits in Java.
      03  for (int i = 0; i < name.length(); i++) {
      04      char c = name.charAt(i);
      05      if (c != lastChar) // perform int comparison so first char will be picked up.
      06      {
      07          sb.append(c);
      08          lastChar = c; // widens c into an int.
      09      }
      10  }
      11  System.out.println(sb.toString());
      

      我们可以通过使用 StringBuilder、ArrayList 等来简单地解决未知长度问题... StringBuilder 将是最简单的。

      我们也可以使用 int 而不是 char 来跟踪我们处理的最后一个字符。我们在这里使用 int 而不是 char,因为无论遇到什么字符,我们都需要初始状态为不匹配。此处可以使用的另一种技术是使用 Character,但它会比在基元上使用 int 扩展更昂贵(堆对象)。

      尝试一下,如果您有理解困难,请告诉我。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-09-02
        • 1970-01-01
        • 1970-01-01
        • 2011-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多