美团网2014笔试算法题汇总
1.链表翻转。给出一个链表和一个数k,比如链表1→2→3→4→5→6,k=2,则翻转后2→1→4→3→6→5,若k=3,翻转后3→2→1→6→5→4,若k=4,翻转后4→3→2→1→5→6,用程序实现。
public class MaxConString { /** * 计算两字符串最大公共字符串长度 */ public static void main(String[] args) { char[] s1 = "jiajiangayaoyao".toCharArray(); //测试数据 char[] s2 = "jiangyaoyao".toCharArray(); int c = new MaxConString().getCount(s1, s2); System.out.println("两字符串的共同字符串长度为:"+c); } private int getSubCount(char[] s1,char[] s2, int i ,int j){//计算两字符串从s1的第i位置s2的第j位置的之后字符串长度 //如“abc”和“ab”则返回conut为2 int count=1; while(++i<s1.length&&++j<s2.length&&s1[i]==s2[j]){ count++; } return count; } private int getCount(char[]s1,char[]s2){ //计算两字符串的共同字符串长度 int count = 0; for(int i=0;i<s1.length;i++) for(int j=0;j<s2.length;j++) if(s1[i]==s2[j]){ if(this.getSubCount(s1, s2, i, j)>count) count = this.getSubCount(s1, s2, i, j); } return count; } }转载请注明原创链接:http://blog.csdn.net/wujunokay/article/details/12209101