2017年亚信笔试测试第一题:判断字符串是否可以连接

由头:

虽然是测试的题目,当时觉得很简单,但是没有做出来。和某acmer交流之后,他说了一个更简单的方法,于是实践一下。

题目:

输入一串字符串,例如:abc,cde,edf,fffg ,问是否可以首位连接,形成一个字符串?

思路:

只需要取得每个字符串的首尾保存起来,判断每一个“首”,是否可以在“尾”里找到?如果超过两个找不到,那么说明不可以。

java代码:

package yaxin;

public class yaxin {

	public  static void main(String args[])
	{
		String []shuru ={"abc","eddf","cde","eee","aaa"};
		String before[] = new String[shuru.length];
		String after[] = new String[shuru.length];
		for(int i=0;i<shuru.length;i++)
		{
			int length = shuru[i].length();
			before[i] = shuru[i].substring(0,1);
			after[i] = shuru[i].substring(length-1,length);
			System.out.println(before[i]+" "+after[i]);
		}
		int count=0;
		for(int i=0;i<shuru.length;i++)
		{

			for(int j=0;j<shuru.length;j++)
			{
				if(before[i].equals(after[j])) // not ==
				{
					count++;
					//after[j]="-";//delete it
					j=shuru.length;//back
				}
			}
		}
		if(shuru.length-count<2)
			System.out.println("1");
		else
			System.out.println("0");
		
		
	}
}



  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值