今天面试了,关于字符串截取的

chuanshuo1935597491 2013-06-29 12:38:57
有一个数组,String [] a={ads,awww,fdgfdf,fvdsfc,hyjutfrdf},
题目要求,例如,数组a={ads}中,在a-z共二十六个字母中,字母a出现了1次,d出现了1次,s出现了1次,其他字母出现了0次,求出在以上数组中,打印出各个字母都出现了多少次。
呵呵,求高人给大家解答分享下。
...全文
233 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
飞舞的锄头 2013-06-30
  • 打赏
  • 举报
回复
public class Test {
	public static void main(String[] args) {

		String[] a = { "ads", "awww", "fdgfdf", "fvdsfc", "hyjutfrdf" };
		Map<Character, Integer> map = new HashMap<Character, Integer>();
		for (int n = 0; n < a.length; n++) {
			for (int i = 0; i < a[n].length(); i++) {
				Integer fre = map.get(a[n].charAt(i));//不存在返回null否则返回当前出现的次数
				map.put(a[n].charAt(i), fre == null ? 1 : fre + 1);
			}
		}
		System.out.println(map);
	}
}
飞舞的锄头 2013-06-30
  • 打赏
  • 举报
回复
仿照Thanking in Java的例子写的

public class Test {
	public static void main(String[] args) {
		
		String[] a={"ads","awww","fdgfdf","fvdsfc","hyjutfrdf"};
		Map<String,Integer>map=new HashMap<String,Integer>();
		for(int n=0;n<a.length;n++){
			for(int i=0;i<a[n].length();i++){
				Integer fre=map.get(String.valueOf(a[n].charAt(i)));
				map.put(String.valueOf(a[n].charAt(i)), fre==null ? 1:fre+1);
			}
		}
		System.out.println(map);
	}
	
}

运行结果{f=7, g=1, d=5, c=1, a=2, j=1, h=1, w=3, v=1, u=1, t=1, s=2, r=1, y=1}
oh_Maxy 2013-06-29
  • 打赏
  • 举报
回复
2L的思维很巧妙啊~ 优化下:根据题目要求,a-z 26个,初始化数组26即可,我们可以循环的时候判断下,小于'a'或大于'z'就跳过好了~
引用 2 楼 Inhibitory 的回复:
利用ascii码值的范围很小[0, 255],直接定义一个大小为256的数组,这样解决办法可以简化很多。
public class Hello {
    public static void main(String[] args) {
        String [] a = {"ads", "awww", "fdgfdf", "fvdsfc", "hyjutfrdf"};
        int[] frequence = new int[256];

        for (String str : a) {
            for (int i = 0; i < str.length(); ++i) {
                frequence[str.charAt(i)]++;
            }
        }

        for (int i = 0; i < frequence.length; ++i) {
            if (frequence[i] != 0) {
                System.out.printf("%s: %d\n", (char)i, frequence[i]);
            }
        }
    }
}
a: 2
c: 1
d: 5
f: 7
g: 1
h: 1
j: 1
r: 1
s: 2
t: 1
u: 1
v: 1
w: 3
y: 1
Candylibin 2013-06-29
  • 打赏
  • 举报
回复
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

public class StringCount
{
	public static void main(String[] args)
	{
		
		String[] arr ={"ads","awww","fdgfdf","fdgfdf","fdgfdf"};
		
		System.out.println(getCount(arr));;
	}
	
	private static String getCount(String[] strs)
	{
		StringBuilder builder = new StringBuilder();
		for(int x=0; x<strs.length; x++)
		{
			builder.append(strs[x]);
		}
		
		String s = builder.toString();
		
		char[] arr = s.toCharArray();
		
		Map<Character,Integer> map = new TreeMap<Character,Integer>();
		int count = 1;
		
		for(int x=0; x<arr.length; x++)
		{
			if(!(arr[x]>='a' && arr[x]<='z' || arr[x]>='A' && arr[x]<='Z'))
				continue;
			
			if(!(map.containsKey(arr[x])))
				map.put(arr[x], count);
			else
				count = map.get(arr[x]) + 1;
				map.put(arr[x], count);
				count = 1;
		}
		
		StringBuilder sb = new StringBuilder();
		
		Iterator<Map.Entry<Character, Integer>> it = map.entrySet().iterator();
		
		while(it.hasNext())
		{
			Map.Entry<Character, Integer> me = it.next();
			char key = me.getKey();
			int value =  me.getValue();
			
			sb.append(key+":("+value+")");
		}
		return sb.toString();
	}
}
Inhibitory 2013-06-29
  • 打赏
  • 举报
回复
利用ascii码值的范围很小[0, 255],直接定义一个大小为256的数组,这样解决办法可以简化很多。
public class Hello {
    public static void main(String[] args) {
        String [] a = {"ads", "awww", "fdgfdf", "fvdsfc", "hyjutfrdf"};
        int[] frequence = new int[256];

        for (String str : a) {
            for (int i = 0; i < str.length(); ++i) {
                frequence[str.charAt(i)]++;
            }
        }

        for (int i = 0; i < frequence.length; ++i) {
            if (frequence[i] != 0) {
                System.out.printf("%s: %d\n", (char)i, frequence[i]);
            }
        }
    }
}
a: 2
c: 1
d: 5
f: 7
g: 1
h: 1
j: 1
r: 1
s: 2
t: 1
u: 1
v: 1
w: 3
y: 1
StevenLoveMaggie 2013-06-29
  • 打赏
  • 举报
回复
引用 2 楼 Inhibitory 的回复:
利用ascii码值的范围很小[0, 255],直接定义一个大小为256的数组,这样解决办法可以简化很多。
public class Hello {
    public static void main(String[] args) {
        String [] a = {"ads", "awww", "fdgfdf", "fvdsfc", "hyjutfrdf"};
        int[] frequence = new int[256];

        for (String str : a) {
            for (int i = 0; i < str.length(); ++i) {
                frequence[str.charAt(i)]++;
            }
        }

        for (int i = 0; i < frequence.length; ++i) {
            if (frequence[i] != 0) {
                System.out.printf("%s: %d\n", (char)i, frequence[i]);
            }
        }
    }
}
a: 2
c: 1
d: 5
f: 7
g: 1
h: 1
j: 1
r: 1
s: 2
t: 1
u: 1
v: 1
w: 3
y: 1
用这个即可。
qianeraben 2013-06-29
  • 打赏
  • 举报
回复
2楼思路很好~~
cansou 2013-06-29
  • 打赏
  • 举报
回复
用hashmap保存 key值是字符 value是次数
地下室森林 2013-06-29
  • 打赏
  • 举报
回复
二楼的代码确实精简。楼主可以结贴了
zyhUTTP 2013-06-29
  • 打赏
  • 举报
回复
public class times {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String[] s = {"ads","awww","fdgfdf","fvdsfc","hyjutfrdf"};
		int[] len = new int[26];
		for(int i = 0;i < s.length;i++){
			for(int j = 0;j <s[i].length();j++){
				len[s[i].charAt(j) - 'a']++;
			}
		}
		for(int i = 0;i < 26;i++){
			char temp;
			temp = (char)('a' + i);
			System.out.print(temp);
			System.out.println(":"+len[i]);
		}

	}

}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧