java面试题 20170908
第一题 使用 list map 效果,会比我自己写的算法,快很多倍,所以算法应该是类似list map 的更快
package com.baoy.cn.study201707.test; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; public class Mainshi1 { public static void main(String[] args) { String[] a = { "a", "b", "a","c", "a", "aaa", "aaa" ,"a","b","c"}; long start1 = System.currentTimeMillis(); String[] b = compare(a); String resultbString = Arrays.toString(b); System.out.println(resultbString.substring(1, resultbString.length()-1).replace(" ","")); long end1 = System.currentTimeMillis(); long start2 = System.currentTimeMillis(); List<String> compare2 = compare2(a); String[] resultarr = (String[]) compare2.toArray(new String[compare2.size()]); String resultString = Arrays.toString(resultarr); System.out.println(resultString.substring(1, resultString.length()-1).replace(" ","")); long end2= System.currentTimeMillis(); System.out.println((end1-start1)+"\r\n"+(end2-start2)); } public static boolean compareLength(String[] param, int index, int current) { if (param[index].length() == param[current].length()) { return true; } return false; } public static boolean compareContent(String[] param, int index, int current) { if (compareLength(param, index, current)) { char[] indexChar = param[index].toCharArray(); int i = 0; while (i < indexChar.length) { if (param[current].charAt(i) != indexChar[i]) { return false; } i++; } return true; } else { return false; } } public static String[] compare(String[] param) { boolean[] flag = new boolean[param.length]; for (int i = 0; i < flag.length; i++) { flag[i] = true; } String[] temp = new String[param.length]; for (int i = 0; i < param.length; i++) { if (flag[i]) { temp[i] = param[i]; int k = 0; for (int j = i + 1; j < temp.length; j++) { if (compareContent(param, i, j)) { k++; flag[j] = false; temp[j] = param[j] + (k == 0 ? "" : k); } } } } return temp; } public static List<String> compare2(String[] param) { List<String> list = new ArrayList<String>(); Map<String,Integer> map = new HashMap<String, Integer>(); for (int i = 0; i < param.length; i++) { if (!map.containsKey(param[i])) { map.put(param[i],0); list.add(param[i]); }else{ map.put(param[i], map.get(param[i])+1); list.add(param[i]+map.get(param[i])); } } return list; } }
package com.baoy.cn.study201707.test; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class Mainshi2 { public static void main(String[] args) throws ParseException { String[] params = {"20170101 00:02:01", "20170101 05:04:02" ,"20170101 10:06:03", "20170101 15:08:04"}; test(params); } public static void test(String []param) throws ParseException{ SimpleDateFormat sdf = new SimpleDateFormat( "yyyyMMdd HH:mm:ss" ); Date date1 = sdf.parse(param[0]); Date date2 = sdf.parse(param[1]); long a = date2.getTime() -date1.getTime(); Calendar calendar=Calendar.getInstance(); calendar.setTimeInMillis(a); int hour =calendar.get(Calendar.HOUR); int minute =calendar.get(Calendar.MINUTE); int seconds =calendar.get(Calendar.SECOND); long day = a / (1000 * 60 * 60 * 24); long month= a / (1000 * 60 * 60 * 24) /30 ; long year= a / (1000 * 60 * 60 * 24)/365; System.out.println( (year > 0 ? year+"year":"")+ (month > 0 ? month+"month":"")+ (day > 0 ? day+"day":"")+ hour+"hour"+minute+"minute"+seconds+"second" ); } }
package com.baoy.cn.study201707.test; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Mainshi3 { public static void main(String[] args) { String[] params = { "A", "B", "C", "D" }; String[] rules = { "A->C", "B->C", "D->C" }; List<String> list = new ArrayList<String>(); order(list, "","", params); // [ ABCD, ABDC, ACBD, ACDB, ADBC, ADCB, // BACD, BADC, BCAD, BCDA, BDAC, BDCA, // CABD, CADB, CBAD, CBDA, CDAB, CDBA, // DABC, DACB, DBAC, DBCA, DCAB, DCBA] List<String> ordersort = ordersort(list, rules); String[] resultarr = (String[]) ordersort.toArray(new String[ordersort.size()]); String resultString = Arrays.toString(resultarr); System.out.println(resultString.substring(1, resultString.length()-1).replace(" ","")); } public static List<String> ordersort(List<String> res,String[] rules){ List<String> list = new ArrayList<String>(); for (int i = 0; i < res.size(); i++) { String line = res.get(i); boolean flag = true; for (int j = 0; j < rules.length; j++) { String[] rule = rules[j].split("->"); if (line.indexOf(rule[0]) > line.indexOf(rule[1])) { flag =false; } } if (flag) { list.add(line); } } return list; } public static void order(List<String> res,String preffix,String temp, String[] a) { preffix += temp ; if (a.length > 1) { for (int i = 0; i < a.length; i++) { List<String> list = Arrays.asList(a); List<String> list2 = new ArrayList<String>(Arrays.asList(new String[list.size()])); Collections.copy(list2, list); temp = a[i]; list2.remove(i); String[] b = (String[]) list2.toArray(new String[list2.size()]); order(res,preffix,temp, b); } } else { res.add(preffix+a[0]); } } }
package com.baoy.cn.study201707.test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Mainshi4 { public static void main(String[] args) { int N1 =2; int N2 =3 ; print(N1, N2); } public static void print(int n1 ,int n2){ if(n1 ==0 && n1 ==1 &&n2 ==0 && n2 ==1){ System.out.println("null"); } String n1s = getCode(n1); String n2s = getCode(n2); List<String> list = new ArrayList<String>(); for (int i = 0; i < n1s.length() ; i++) { String one= n1s.substring(i, i+1); for (int j = 0; j < n2s.length(); j++) { String two= n2s.substring(j, j+1); list.add(one+two); } } String[] arr = (String[]) list.toArray(new String[list.size()]); String resultString = Arrays.toString(arr); System.out.println(resultString.substring(1, resultString.length()-1).replace(" ","")); } public static String getCode(int key) { switch (key) { case 1: return ""; case 2: return "abc"; case 3: return "def"; case 4: return "ghi"; case 5: return "jlk"; case 6: return "mno"; case 7: return "pqrs"; case 8: return "tuv"; case 9: return "wxyz"; case 0: return ""; default: break; } return ""; } }
package com.baoy.cn.study201707.test; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Mainshi6 { public static void main(String[] args) { int[] weight =new int []{ 7, 5, 3, 2, 6, 1, 4, 9}; String[] s =new String []{"A","B","C","D","E","F","G","H"}; sort(weight, s, 5); } public static void sort(int[] weight,String[] s,int n){ List <Alphabet> list = new ArrayList<Alphabet>(); int []all = new int [weight.length]; for (int i = 0; i < s.length; i++) { list.add(new Alphabet(weight[i],s[i])); } Collections.sort(list); for (int i = 0; i < n; i++) { System.out.print(list.get(i).alphabet); } } } class Alphabet implements Comparable<Alphabet>{ public int allweight; public String alphabet; public Alphabet(){} public Alphabet(int allweight, String alphabet) { this.allweight = allweight; this.alphabet = alphabet; } @Override public int compareTo(Alphabet o) { if (this.allweight > o.allweight) { return 1; }else if (this.allweight == o.allweight) { return 0; } return -1; } }
package com.baoy.cn.study201707.test; import java.util.ArrayList; import java.util.List; public class Mianshi9 { public static void main(String[] args) { Node a = new Node("A", null); Node b = new Node("B", null); Node c = new Node("C", null); Node d = new Node("D", null); Node e = new Node("E", null); Node f = new Node("F", null); Node g = new Node("G", null); Node h = new Node("H", null); Node i = new Node("I", null); c.setChild(f); c.setChild(g); c.setChild(h); b.setChild(e); d.setChild(i); a.setChild(b); a.setChild(c); a.setChild(d); a.test(a, c); } } class Node{ public String data; public List<Node> list; public Node(){} public Node(String data, List<Node> list) { this.data = data; this.list = list; } public void setChild(Node node){ if (list == null ) { list = new ArrayList<Node>(); } list.add(node); } public void order(Node localroot,Node red){ if(localroot.list != null){ for (Node node : localroot.list) { if (!node.data.equals(red.data)) { System.out.print(node.data); } } for (Node node : localroot.list) { if (!node.data.equals(red.data)) { order(node, red); } } } } public void test(Node root ,Node red){ System.out.print(root.data); root.order(root, red); } }
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(支持支付宝和微信 以及扣扣群),没钱捧个人场,谢谢各位。
个人主页:http://knight-black-bob.iteye.com/
谢谢您的赞助,我会做的更好!