一道JAVA笔试题求解答,关于选字符串的?
发布网友
发布时间:2022-07-15 05:37
我来回答
共6个回答
热心网友
时间:2023-11-22 21:58
这题好,看你对string掌握得扎实不,如果扎实,很容易
public class Test10097 {
public static void main(String args[]) {
String str = "abaaccddkkdjfjj";
String uniqueCarc = getFirstUniqueCharactre(str);
System.out.println(uniqueCarc);
}
private static String getFirstUniqueCharactre(String str) {
for(int i = 0; i < str.length(); i++){
int firstIndex = str.indexOf(str.charAt(i));
int lastIndex = str.lastIndexOf(str.charAt(i));
if(firstIndex == lastIndex){
return String.valueOf(str.charAt(i));
}
}
return null;
}
}
-----------
b
热心网友
时间:2023-11-22 21:58
使用String类的contains(),replaceFirst()等方法
import java.util.Scanner; //从键盘输入
public class StringTest1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in) ;
String str = sc.nextLine() ;
int j = 0 ;
while(j < str.length()) {
char ch = str.charAt(j) ;
String s = String.valueOf(ch) ;
String str1 = str.replaceFirst(s, "") ;
if(str1.contains(s)) {
//System.out.println(s) ;
j++ ;
System.out.println(j) ;
}else {
char nCh = ch ;
System.out.println(nCh) ;
break ;
}
}
}
}
热心网友
时间:2023-11-22 21:59
import java.io.*;
public class StringTest {
//only for test, do nothing to the Exception
public static void main(String[] args)throws Exception {
System.out.print("please input a string:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
str = br.readLine();
char ch = '\u0000';
//int len = str.length();
for(int i=0; i<str.length(); i++)
{
ch = str.charAt(0);
str = str.substring(1);
if(str.indexOf(ch) == -1)
{
System.out.println("you want " + ch);
break;
}
}
}
}
热心网友
时间:2023-11-22 21:59
就写个主要方法,其他自己补全哦
public static void find(String str)
{
char ch = 'a';
int index = 0;
int[] a = new int[N];
for(int i=0; i<a.length; i++)
a[i] = 0;
for(int i=0; i<str.length(); i++)
{
ch = str.charAt(i);
index = 25 - 'z' + ch;
a[index]++;
}
int min = Integer.MAX_VALUE;
for(int i=0; i<a.length; i++)
{
if(a[i] == 1)
{
ch = letters.charAt(i);
index = str.indexOf(ch);
if(index < min)
min = index;
}
}
if(min < Integer.MAX_VALUE)
{
System.out.println(str.charAt(min));
}
else
{
System.out.println("字符串中没有唯一不同的字符!");
}
}
热心网友
时间:2023-11-22 22:00
import java.awt.*;
import javax.swing.*;
public class test {
public static void main(String args[]){
String str = "abaaccddkkdjfjj";
char []arr = new char[26];
int []num = new int[26];
for(int i = 0;i<26;i++){
num[i] = 0;
}
int total = 1;
arr[0] = str.charAt(0);
num[0] = 1;
for(int i = 1;i<str.length();i++){
boolean b = false;
for(int j = 0;j<total;j++){
if(str.charAt(i)==arr[j]){
num[j]++;
b = true;
break;
}
}
if(b == false){
arr[total]=str.charAt(i);
num[total]=1;
total++;
}
}
for(int i = 0 ;i<total;i++){
if(num[i]==1){
System.out.print(arr[i]);
break;
}
}
}
}
思路:创建两个数据,一个是char型的,一个是int型的。然后把字符串分解,读取每一个字母,如果当前字母以前出现过,查出是第几个出现的,并对应出现次数加1。如果没出现,则在char数组中加上一个新的元素,出现次数为1。读完整个字符串后,对int数据进行检索,第一个出现1对应在arr数据中的字母为最早出现且只出现一次。
热心网友
时间:2023-11-22 22:01
flyingFish211的方法很好!哈哈!!