文字のストリームから最初の非反復文字を検索するJavaプログラム
文字のストリームから最初の繰り返しのない文字を見つけるためのJavaコードは次のとおりです-
例
import java.util.ArrayList;
import java.util.List;
public class Demo{
final static int max_chars = 256;
static void non_repeating_char(){
List<Character> my_list = new ArrayList<Character>();
boolean[] repeat = new boolean[max_chars];
String my_str = "Thisisasample";
for (int i = 0; i < my_str.length(); i++){
char x = my_str.charAt(i);
if (!repeat[x]){
if (!(my_list.contains(x))){
my_list.add(x);
}
else{
my_list.remove((Character)x);
repeat[x] = true;
}
}
if (my_list.size() != 0){
System.out.print("The first non-repeating character of the string is ");
System.out.println(my_list.get(0));
}
}
}
public static void main(String[] args){
non_repeating_char();
}
} 出力
The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T The first non-repeating character of the string is T
Demoという名前のクラスには、「non_repeating_char」関数という名前の関数が含まれています。リストが作成され、文字列が定義されます。この文字列は繰り返され、すべての文字が検査され、そのカウントはブール変数の形式で「repeat」という名前の配列に格納されます。繰り返される場合は値がtrueになり、それ以外の場合はfalseになります。メイン関数では、関数が呼び出され、関連するメッセージがコンソールに表示されます。
-
Pythonの文字ストリームから最初の非反復文字を検索します
文字のストリームがある場合、または文字列を検討でき、文字列内の最初の非反復文字を検索する必要があるとします。したがって、文字列が「人」のようなものである場合、出現する最初の文字は「o」です。したがって、インデックスが返されます。ここでは2です。そのような文字がない場合は、-1を返します。 これを解決するには、次の手順に従います- 1つの頻度マップを作成する 文字列内の文字cごとに、次のようにします cが周波数にない場合は、それを周波数に挿入し、値1を入力します それ以外の場合は、頻度のカウントを増やします 頻度マップをスキャンします。特定のキーの値が1の場合
-
文字のストリームから最初の繰り返しのない文字を見つけるPythonプログラム?
このセクションでは、文字列または文字のストリームから最初の一意の文字または繰り返されない文字を見つけます。この問題を解決する方法は複数あります。同じキャラクターのストリームに対して2つの異なるプログラムを作成しようとします。 方法1:関数を使用する def firstNonRepeatingChar(str1): char_order = [] counts = {} for c in str1: if c in counts: &n