输入包括一个由字母和数字组成的字符串,其长度不超过100。
可能有多组测试数据,对于每组数据, 按照样例输出的格式将字符出现的位置标出。 1、下标从0开始。 2、相同的字母在一行表示出其出现过的位置。
abcaaAB12ab12
a:0,a:3,a:4,a:9 b:1,b:10 1:7,1:11 2:8,2:12
import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while(scanner.hasNext()) { String str = scanner.next(); String[] strs = new String[str.length()]; //记录字符以及对应出现的次数 Map<String, Integer> myMap = new LinkedHashMap<String, Integer>(); for (int i = 0; i < strs.length; i++) { strs[i] = Character.toString(str.charAt(i)); if (myMap.containsKey(strs[i])) { myMap.put(strs[i], myMap.get(strs[i]) + 1); }else { myMap.put(strs[i], 1); } } //将出现次数只有一次的删除 List<String> myList = new ArrayList<String>(); for(Map.Entry<String, Integer> entry : myMap.entrySet()) { if (entry.getValue() == 1) { //不能直接在这里删除 myList.add(entry.getKey()); } } for(int i = 0;i < myList.size() ; i++) { myMap.remove(myList.get(i)); } for(Map.Entry<String, Integer> entry : myMap.entrySet()) { String s = entry.getKey(); //记录第几次出现,最后一次的最后没有"," int sum = 1; for (int j = 0; j < strs.length; j++) { if(strs[j].equals(s)) { if (sum < entry.getValue()) { System.out.print(s+":"+j+","); sum++; }else { System.out.print(s+":"+j); } } } System.out.println(); } } } }
import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; /** * 对给定的一个字符串,找出有重复的字符,并给出其位置,如:abcaaAB12ab12 输出:a,1;a,4;a,5;a,10,b,2;b,11,1,8;1,12, 2,9;2,13。 */ import java.util.Scanner; public class Main{ //private static char[] array = "01234567".toCharArray(); public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNextLine()) { char[] ch = sc.nextLine().toCharArray(); //注意这里,因为需要按序输出,所以不能简单用hashMap Map<Character,List<Integer>>map=new LinkedHashMap<>(); //找出重复元素都有谁,用map统计 for(int i=0;i<ch.length;i++) { char c=ch[i]; List<Integer>list=map.getOrDefault(c, new ArrayList<>()); list.add(i);//只记录出现的位置。 map.put(c, list); } for(Map.Entry<Character, List<Integer>>entry:map.entrySet()) { List<Integer>list=entry.getValue(); if(list.size()>1) { char c=entry.getKey(); for(int i=0;i<list.size();i++) { //a:0, System.out.print(c+":"+list.get(i)); if(i!=list.size()-1) System.out.print(","); } System.out.println(); } } } } }
import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()){ char[] array = scanner.nextLine().toCharArray(); LinkedHashMap<Character, ArrayList<Integer>> map = new LinkedHashMap<>(); for (int i = 0; i < array.length; i++) { if (map.get(array[i])==null){ ArrayList<Integer> list = new ArrayList<>(); list.add(i); map.put(array[i],list); }else { ArrayList<Integer> list = map.get(array[i]); list.add(i); } } for (Map.Entry<Character, ArrayList<Integer>> entry : map.entrySet()) { ArrayList<Integer> value = entry.getValue(); if (value.size()>1){ Character key = entry.getKey(); for (int i = 0; i < value.size() - 1; i++) System.out.print(key+":"+value.get(i)+","); System.out.println(key+":"+value.get(value.size()-1)); } } } } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String str = sc.nextLine(); char[] ch = str.toCharArray(); for(int i=0;i<ch.length-1;i++){ if(str.indexOf(ch[i],i+1) != -1 && str.lastIndexOf(ch[i],i-1) == -1){ int index = i; while(index != -1){ System.out.print(ch[i]+":"+index); index = str.indexOf(ch[i],index+1); if(index != -1){ System.out.print(","); } } System.out.println(); } } } } }
运行时间:45ms
占用内存:10936k
a Java Solution
注释都写在代码里了
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
/**
* @author Allen_Hua
* @create_time 创建时间:May 11, 2018 9:57:56 AM 类说明
*/
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
String str = scan.nextLine();
Map<Character, Integer> map = new LinkedHashMap<>();
map.put(str.charAt(0), 1);// 第一个字符肯定是没有重复的
// 将字符和出现次数put进LinkedHashMap中
for (int i = 1; i < str.length(); i++) {
if (map.containsKey(str.charAt(i))) {
map.put(str.charAt(i), map.get(str.charAt(i)) + 1);
} else {
map.put(str.charAt(i), 1);
}
}
//使用Iterator遍历
Iterator<Entry<Character, Integer>> it = map.entrySet().iterator();
while (it.hasNext()) {
Entry<Character, Integer> entry = it.next();
//只输出出现次数大于1的字符和其索引位置
if (entry.getValue() > 1) {
//得到每个字符出现的频次以循环这么多次 方便输出
int geshu = countNumber(entry.getKey(), str);
//循环输出
for (int i = 0; i < geshu; i++) {
//得到该字符的索引 nextIndex方法中的第三个参数很重要,控制本次循环得到的是哪一个索引
int index = nextIndex(entry.getKey(), str, i);
if (i == geshu - 1) {
System.out.print(entry.getKey() + ":" + index);//如果是该行最后一对输出 那么不能添加逗号
} else {
System.out.print(entry.getKey() + ":" + index + ",");
}
}
System.out.println();
}
}
}
}
//得到每个字符出现的频次
private static int countNumber(Character key, String str) {
// TODO Auto-generated method stub
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == key) {
count++;
}
}
return count;
}
//得到该次输出的该字符的索引 使用临时变量temp和传入的index索引比对 返回正确的i
private static int nextIndex(Character key, String str, int index) {
// TODO Auto-generated method stub
int temp = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == key) {
temp++;
if (temp == index + 1)
return i;
}
}
return 0;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
String line=null;
while((line=input.readLine())!=null){
printf(line);
}
input.close();
}
static void printf(String line){
int num[] = new int[128];
for(int i=0;i<line.length()-1;i++){
if(num[line.charAt(i)]==0){
String str="";
for(int j=i+1;j<line.length();j++){
if(line.charAt(i)==line.charAt(j)){
str+=","+line.charAt(j)+":"+j;
}
}
if(!str.equals("")){
str=line.charAt(i)+":"+i+str;
System.out.println(str);
}
num[line.charAt(i)]++;
}
}
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
String s=sc.nextLine();
char[] ch=s.toCharArray();
ArrayList<Character> arr=new ArrayList<Character>();
for (int i = 0; i < ch.length; i++) {
if(!arr.contains(ch[i])&&s.indexOf(ch[i])!=s.lastIndexOf(ch[i])){
System.out.print(ch[i]+":"+i);
arr.add(ch[i]);
for(int j=i+1;j<ch.length;j++){
if(ch[j]==ch[i]){
System.out.print(',');
System.out.print(ch[j]+":"+j);
}
}
System.out.println();
}
}
}
}
}
/*运行时间:83ms,占用内存:1415k*/ import java.util.Scanner; /* * 功能:对给定的一个字符串,找出有重复的字符,并给出其位置 */ public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); String str = ""; StringBuffer sb = null; //保存输出的结果 while(sc.hasNext()){ //1.读取输入的字符串 str = sc.nextLine(); char[] chs = str.toCharArray(); for (int i=0;i<chs.length;i++) { if(chs[i] != ' '){ sb = new StringBuffer(); char indexChar = chs[i]; //2.已经找过的设为' ' chs[i] = ' '; boolean isAdd = false; sb.append(indexChar + ":" + i + ","); for (int j=0;j<chs.length;j++) { if (chs[j]!= ' ' && chs[j] == indexChar) { sb.append(indexChar + ":" + j + ","); chs[j] = ' '; isAdd = true; } } if(isAdd){ System.out.println(sb.substring(0, sb.length() - 1)); } } } } } }
import java.util.Scanner;
public class Main{
public static
void main(String[] arg){
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
String info = scan.nextLine();
for(int
i=0;i<info.length();i++){
boolean flag = false;
String a = info.charAt(i)+"";
if(info.contains(a)){
int l = info.indexOf(a);
if(l==i){
int k = i;
k =
info.indexOf(a,k+1);
if(k!=-1){
flag =
true;
System.out.print(a+":"+i);
}
while(k!=-1){
System.out.print(","+a+":"+k);
k
= info.indexOf(a,k+1);
}
if(flag){
System.out.println();
}
}
}
}
}
}
}
import java.util.LinkedHashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); while(input.hasNext()){ String str = input.nextLine(); Map<String,String> map = new LinkedHashMap<String,String>(); for (int i = 0; i < str.length(); i++) { String key = str.charAt(i)+""; if(map.get(key.toUpperCase()) != null){ map.put(key.toUpperCase(),map.get(key.toUpperCase())+ ","+i); }else if(map.get(key.toLowerCase()) != null){ map.put(key.toLowerCase(),map.get(key.toLowerCase())+ ","+i); }else{ map.put(key, ""+i); } } for(String k:map.keySet()){ if(map.get(k).indexOf(",") > 0) System.out.println(k+":"+map.get(k).replaceAll(",", ","+k+":")); } } } }