旷视科技第二题什么鬼。

public class Main{
	public static void main(String args[])
	{
		Scanner sc=new Scanner(System.in);
		int k=sc.nextInt();
		sc.nextLine();
		String str=sc.nextLine();
		String res="";
		int maxNum=0;
		int count=0;   //不同字符数量
		int weizhi=0;
		for(int i=0;i<str.length();i++)
		{
			if(!res.contains(str.charAt(i)+""))
			{
				if(count<k)
				{
					count++;
					res+=str.charAt(i);
				}
				else {
					char c=res.charAt(0);
					res=res.substring(weizhi+1, res.length());
					res+=str.charAt(i);
				}
			}
			else {
				res+=str.charAt(i);
				if(str.charAt(i)==res.charAt(0))
					weizhi=i;
			}
			maxNum=Math.max(maxNum, res.length());
		}
		System.out.print(maxNum);
	}
}

这旷世科技第二题就离谱。刚开始使用ArrayList一个一个搞超时了,后来换成String用SubString还是超时。。难道有什么稀奇古怪的算法??


#旷视##笔试题目#
全部评论
这题得用lru
点赞 回复 分享
发布于 2019-09-09 22:05

相关推荐

秋国🐮🐴:拿到你简历编号然后让你知道世间险恶
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务