1、给定一个字符串,请写出一段代码找出这个字符串中首先出现两次的那个字符。
例如:字符串为"qywyer23tdd",输出为y。
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String str = scan.next();
HashMap<Character,Integer> hashMap = new HashMap<> ();
char c = 0;
for(int i = 0; i < str.length();i++)
{
if(hashMap.containsKey(c = str.charAt(i)))
{
System.out.println(c);
break;
}
else
{
hashMap.put(c, 1);
}
}
}
2、写一段代码,尝试在以下文本中搜索并打印出包含单词"your"(不区分大小写)的句子,并按出现次序从高到低排序。
Make yourself at home
None of your business
I will be more careful
How about going to be a move?
Your life is your own affair
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
HashMap<String,Integer> hashMap = new HashMap<> ();
String str = null;
while(!(str = scan.nextLine()).equals("EOF"))
{
hashMap.put(str, getCount(str,"your"));
}
sort(hashMap);
}
//排序并降序输出
public static void sort(HashMap<String,Integer> hashMap)
{
ArrayList<Map.Entry<String, Integer>> list = new ArrayList<> (hashMap.entrySet());//hashMap.entrySet()返回映射的键值对
Collections.sort(list,new Comparator<Map.Entry<String, Integer>> () //通过实现Comparator接口,可实现对排序的精确控制
{
public int compare(Map.Entry<String,Integer> e1,Map.Entry<String,Integer> e2)
{
return e2.getValue() - e1.getValue();
}
});
for(Map.Entry<String, Integer> e : list) //遍历list
{
System.out.println(e.getKey() + "--->" + e.getValue());
}
}
//统计每行字符串中"your"出现次数
public static int getCount(String str,String sub)
{
int count = 0;
int start = 0;
int temp = 0;
String s = str.toLowerCase();
while((temp = s.indexOf(sub, start)) != -1)
{
count++;
start = temp + sub.length();
}
return count;
}
3、String padString(String string,int minLength,char padChar),就是在string前用padChar将string填充至minLength的长度,
如("7",3,'0')得到的结果是"007",("2012",3,'0')得到的结果是"2012"。minLength不能为负数。
public static void main(String[] args)
{
System.out.println(padString("7",3,'0'));
System.out.println(padString("2012",3,'0'));
}
public static String padString(String string,int minLength,char padChar)
{
int n = string.length();
StringBuffer currentString = new StringBuffer();
if(minLength <= n || minLength < 0)
return string;
else
{
int m = minLength - n;
for(int i = 0;i < m;i++)
{
currentString.append(padChar);
}
currentString.append(string);
}
return currentString.toString();
}
4、命令解析
对于命令:
要变成<pre name="code" class="java">-name jack -age 20 -address "HangZhou ZheDa Road"
命令由参数的值对组成,参数以-开头,参数和值之间,值和值之间,都用空格隔开,双引号之间的值当作一个整体来处理。[-name jack, -age 20, -address, "HangZhou ZheDa Road"]
所有参数和值只包括英文字母、数字、减号、双引号。
public static void main(String[] args)
{
System.out.println(parse("-name jack -age 20 -address \"HangZhou ZheDa Road\""));
}
public static String parse(String str)
{
String[] pars = str.split("-"); //将命令拆分
StringBuilder sb = new StringBuilder();
sb.append("[");
int n = pars.length;
for(int i = 0;i < n;i++)
{
if(pars[i].length() != 0)//pars[0]为空,过滤掉
{
sb.append("-");
sb.append(pars[i].trim());
if(i != n-1)
sb.append(", ");
}
}
sb.append("]");
return sb.toString();
}
5、dif(Stirng str1,String str2)
输出两个字符串中不同的字符,如果字符a在str1中出现,而没有在str2中出现,则输出-a;相反,则输出+a。字符串当中重复的字符串不输出。
abcde,bcde 输出 -a
dabc,aabcef 输出 +a,-d,+e,+f
abcdefe,aabcadef 输出 +a,+a,-e
public static void main(String[] args)
{
System.out.println(dif("abcde","bcde"));
System.out.println(dif("dabc","aabcef"));
System.out.println(dif("abcdefe","aabcadef"));
}
//把26个英文字母映射为size为26的整型数组flag,初始化为0,flag[0]对应a;若字母在str1中出现,相应flag[k]减一;若字母在str2中出现,相应flag[k]加一。
//最后根据flag[k]大小进行打印。
public static String dif(String str1,String str2)
{
int[] flag = new int[26];
String s1 = str1.toLowerCase();
String s2 = str2.toLowerCase();
StringBuilder sb = new StringBuilder();
for(int i = 0;i < s1.length();i++)
flag[s1.charAt(i) - 'a']--;
for(int j = 0;j < s2.length();j++)
flag[s2.charAt(j) - 'a']++;
for(int k = 0;k < flag.length;k++)
{
if(flag[k] < 0)
{
int temp = Math.abs(flag[k]);
while(temp != 0)
{
sb.append("-" + (char)('a' + k) + ",");
temp--;
}
}
else if(flag[k] > 0)
{
while(flag[k] != 0)
{
sb.append("+" + (char)('a' + k) + ",");
flag[k]--;
}
}
}
if(sb.lastIndexOf(",") == sb.length()-1)
{
sb.deleteCharAt(sb.length()-1);
}
return sb.toString();
}
6、实现一个字符串反转,规则如下:
输入一个字符串str,一个分隔符delim,要求实现对分隔符之间的字符作反转操作,但是其内部顺序不变。
接口:String reverseDelimited(string str,const char delim)
举例:
reverseDelimited(NULL, *) = NULL
reverseDelimited("", *) = ""
reverseDelimited("www.csdn.com", '.') = "com.csdn.www"
reverseDelimited("www.csdn.com", '*') = "www.csdn.com"
实现:
public static void main(String[] args)
{
System.out.println(reverseDelimited("www.taobao.com",'*'));
System.out.println(reverseDelimited(null,'*'));
System.out.println(reverseDelimited("",'*'));
System.out.println(reverseDelimited("www.csdn.com",'*'));
}
public static String reverseDelimited(String str,char delim)
{
if(str == null || str.length() == 0 || !str.contains(String.valueOf(delim)))
return str;
String[] paras = new String[20];
StringBuilder sb = new StringBuilder();
int index = 0; //delim在str中的索引
int i = 0 ; //数组索引
int j = 0; //要截取的字符串的初始索引
while(j < str.length() && (index = str.indexOf(delim,index)) != -1)
{
paras[i++] = str.substring(j,index); //将以delim分割的字符放入数组中
if(index == str.lastIndexOf(delim)) //将最后一个delim的位置到str最后的字串单独处理
paras[i] = str.substring(index + 1,str.length());
j = ++index;
}
//将数组中元素倒序拷贝
for(int k = i;k >= 0;k--)
{
sb.append(paras[k] + delim);
if(k == 0)
sb.deleteCharAt(sb.length()-1);
}
return sb.toString();
}
7、实现字符串去重
public static void main(String[] args)
{
System.out.println(delRepetition("aaasssdddzzz")); //运行时间:1147514纳秒
System.out.println(delRepetition2("aaasssdddzzz")); //运行时间:909019纳秒
System.out.println(delRepetition3("aaasssdddzzz")); //运行时间:317179纳秒
}
//使用LinkedHashSet
public static String delRepetition(String str)
{
Set<Character> set = new LinkedHashSet<>();
StringBuilder sb = new StringBuilder();
for(int i = 0;i < str.length();i++)
set.add(str.charAt(i));
for(Character c:set)
sb.append(c);
return sb.toString();
}
//使用List的contains方法
public static String delRepetition2(String str)
{
List<Character> list = new ArrayList<> ();
StringBuilder sb = new StringBuilder();
for(int i = 0;i < str.length();i++)
{
if(!list.contains(str.charAt(i)))
list.add(str.charAt(i));
}
for(Character c:list)
sb.append(c);
return sb.toString();
}
//双循环
public static String delRepetition3(String str)
{
StringBuilder sb = new StringBuilder(str);
for(int i = 0;i < sb.length()-1;i++)
{
for(int j = i + 1;j < sb.length();j++)
{
if(sb.charAt(i) == sb.charAt(j))
{
sb.deleteCharAt(j);
j--;
}
}
}
return sb.toString();
}
8、实现一个有max_size的cache系统,key为userName,value为info,如果cache满了,采用LRU规则删除最久没有访问的那个元素。
思路:本题考查使用LinkedHashMap实现LRU缓存算法,我们只需重写LinkedHashMap中的removeEldestEntry方法即可。
public class YourLinkedHashMap<K,V> extends LinkedHashMap<K,V>
{
private int cache;
public YourLinkedHashMap(int cache)
{
super(20, 0.75f, true);
this.cache = cache;
}
//每次添加新的元素到集合中(即调用put和putAll方法)后,会调用此方法,该方法若删除最旧的元素,则会返回true;否则返回false。
//在LinkedHashMap中,该方法的实现只有一条语句,即:return false; 因而HashMap中在添加元素时,是不会自己主动删除元素的。
@Override
public boolean removeEldestEntry(Map.Entry<K,V> eldest)
{
System.out.println("我是最老的元素哦:" + eldest.getKey() + "--->" + eldest.getValue());
return size() > cache; //集合中元素个数大于cache,则返回true。
}
public static void main(String[] args)
{
Map<Integer,String> map = new YourLinkedHashMap<> (3);
map.put(1, "test");
map.put(2, "admin");
map.put(3, "whc");
map.put(4, "just for fun");
//遍历集合元素
Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
while(it.hasNext())
{
Map.Entry<Integer, String> entry = it.next();
System.out.println(entry.getKey() + "------>" + entry.getValue());
}
}
}
9、我们每天都会上网,访问每个页面,浏览器中会记录用户的访问历史,大家可以通过浏览器的前进、后退进行浏览历史记录,请定义合适的数据结构,实现操作访问历史功能的代码,尽量高效。由于内存有限,历史记录只能保存有限记录MAX_NUM个,当记录数目超过MAX_NUM个时,删除最近未被使用的记录。
功能模块必须包含3个基本函数:
参考博客:http://blog.csdn.net/zheng0518/article/details/39529181
http://www.itmian4.com/forum.php?highlight=%C8%A5%C4%C4%B6%F9&mod=viewthread&tid=3704