java—字符流中的第一个非重复字符

zsohkypk  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(371)

这个问题要求我们找到新的字符串b。
b的格式是这样的,每次将一个字符插入到流中时,我们都必须找到第一个非重复字符,并将其附加到b的末尾。如果未找到非重复字符,则在b的末尾追加“#”。

Example:
    "a"      -   first non repeating character 'a'
    "ab"     -   first non repeating character 'a'
    "aba"    -   first non repeating character 'b'
    "abad"   -   first non repeating character 'b'
    "abadb"  -   first non repeating character 'd'
    "abadbc" -   first non repeating character 'd'

有人能帮我找出我的代码哪里出错了吗。我的逻辑是使用string的substring函数,找到唯一的字符并将其添加到arraylist,然后打印整个arraylist。

public class Solution
{
    public String solve(String A) 
    {
        ArrayList<Character>a=new ArrayList<Character>();
        String res="";

        for(int i=0;i<A.length();i++)
        { 
            String ss=A.substring(0,i+1);
            String ue=uniqueCharacters(ss);
           // System.out.println(ue);

           if(ue!="") a.add(ue.charAt(0));
           else a.add('#');
        } 

        for(Character j:a) res+=j;

        return res;
    } 

    public static String uniqueCharacters(String test)
    {
        String temp = "";

        for (int i = 0; i < test.length(); i++)
        {
            char current = test.charAt(i);
            if (temp.indexOf(current) < 0) temp = temp + current;
            else temp = temp.replace(String.valueOf(current), "");
        }

        return temp;
    }
}
p1tboqfb

p1tboqfb1#

当您第二次遇到字符时,您将其从唯一字符(1)中删除,但如果该字符有第三个字符,则会再次添加该字符(2)。

public static String uniqueCharacters(String test)
    {
        String temp = "";

        for (int i = 0; i < test.length(); i++)
        {
            char current = test.charAt(i);
            if (temp.indexOf(current) < 0) temp = temp + current; <---- (2)
            else temp = temp.replace(String.valueOf(current), ""); <----- (1)
        }

        return temp;
    }

solution count计算字符数,然后仅返回计数为1(一)的字符数。

k3bvogb1

k3bvogb12#

最好是使用 Set 使用的结果检测输入字符串中的唯一字符 Set::add 返回 false 如果未向集合中实际添加任何元素 Queue 保持非重复字符。
当检测到重复(非唯一)字符时,会将其从队列中删除,如有必要, "#" 作为占位符应用。如果检测到一个唯一的字符,它将被添加到队列中。
实施示例:

public static String solve(String str) {
    // verify the input
    if (null == str || str.isEmpty()) {
        return str;
    }

    Set<Character> previous = new LinkedHashSet<>();
    Queue<Character> nonRepeated = new LinkedList<>();

    StringBuilder sb = new StringBuilder();

    // use the first character
    char curr = str.charAt(0);
    previous.add(curr);
    nonRepeated.add(curr);
    sb.append(curr);

    for (int i = 1, n = str.length(); i < n; i++) {
        char c = str.charAt(i);

        if (!previous.add(c)) { // duplicate character found
            nonRepeated.remove(c);
            if (nonRepeated.isEmpty()) {
                curr = '#';
            } else { // get the next non-repeated character
                curr = nonRepeated.peek();
            }
        } else { // unique element is detected
            if (curr == '#') {
                curr = c;
            }
            nonRepeated.add(c);
        }

        sb.append(curr);
    }

    return sb.toString();
}

测验:

for (String t : Arrays.asList("abadbc", "abcdba", "aaabbbacab")) {
    System.out.printf("input: %s --> %s%n----%n", t, solve(t));
}

输出:

input: abadbc --> aabbdd
----
input: abcdba --> aaaaac
----
input: aaabbbacab --> a##b###ccc
----

相关问题