|
1 | 1 | packageAlgorithms.algorithm.interviews.uber;
|
2 | 2 |
|
| 3 | +importjava.security.MessageDigest; |
| 4 | +importjava.security.NoSuchAlgorithmException; |
3 | 5 | importjava.util.*;
|
| 6 | + |
| 7 | +importAlgorithms.tree.TreeNode; |
4 | 8 | publicclassExample {
|
5 |
| -publicstaticvoidmain(String[]strs) { |
| 9 | +publicstaticvoidmain(String[]strs)throwsNoSuchAlgorithmException{ |
6 | 10 | // printArray(sqrt2(100));
|
7 | 11 | // printArray(sqrt2(300));
|
8 | 12 | // printArray(sqrt2(400));
|
@@ -33,15 +37,53 @@ public static void main(String[] strs) {
|
33 | 37 | //
|
34 | 38 | // System.out.println(findPeak2(records));
|
35 | 39 |
|
36 |
| -int[]A = {-1,0,1,2,3,5,8,9}; |
37 |
| -System.out.println(findIndex2(A)); |
| 40 | +// int[] A = {-1,0,1,2,3,5,8,9}; |
| 41 | +// System.out.println(findIndex2(A)); |
| 42 | +// |
| 43 | +// int[] A3 = {0,0,1,2,3,5,8,9}; |
| 44 | +// System.out.println(findIndex3(A3)); |
| 45 | +// |
| 46 | +// System.out.println(stringShift2("abC", 24)); |
| 47 | +// |
| 48 | +// ArrayList<String> list = new ArrayList<String>(); |
| 49 | +// list.add("cba"); |
| 50 | +// list.add("abc"); |
| 51 | +// list.add("bcd"); |
| 52 | +// list.add("dogs"); |
| 53 | +// list.add("xyz"); |
| 54 | +// list.add("zab"); |
| 55 | +// list.add("c"); |
| 56 | +// list.add("d"); |
| 57 | +// getSame(list); |
| 58 | +System.out.println(getShort("www.uber.com")); |
| 59 | + |
| 60 | +System.out.println(getFullUrl("f626cf")); |
| 61 | + |
| 62 | +TreeNoderoot =newTreeNode(1); |
| 63 | +TreeNodenode1 =newTreeNode(5); |
| 64 | +TreeNodenode2 =newTreeNode(6); |
| 65 | +TreeNodenode3 =newTreeNode(4); |
| 66 | + |
| 67 | +TreeNodenode4 =newTreeNode(3); |
| 68 | +TreeNodenode5 =newTreeNode(2); |
| 69 | +TreeNodenode6 =newTreeNode(1); |
| 70 | + |
| 71 | +root.left =node1; |
| 72 | +root.right =node2; |
| 73 | + |
| 74 | +root.left.left =node3; |
38 | 75 |
|
39 |
| -int[]A3 = {0,0,1,2,3,5,8,9}; |
40 |
| -System.out.println(findIndex3(A3)); |
| 76 | +node2.left =node4; |
| 77 | +node2.right =node5; |
41 | 78 |
|
42 |
| -System.out.println(stringShift2("abC",24)); |
| 79 | +node5.right =node6; |
| 80 | + |
| 81 | +System.out.println(getPath(root,10)); |
43 | 82 | }
|
44 | 83 |
|
| 84 | +staticHashMap<String,String>map =newHashMap<String,String>(); |
| 85 | +staticHashMap<String,String>mapFull =newHashMap<String,String>(); |
| 86 | + |
45 | 87 | publicstaticvoidprintArray(int[]in) {
|
46 | 88 | for (inti =0;i <in.length;i++) {
|
47 | 89 | System.out.print(in[i] +" ");
|
@@ -917,4 +959,133 @@ public static String stringShift2(String s,int shift){
|
917 | 959 |
|
918 | 960 | returnsb.toString();
|
919 | 961 | }
|
| 962 | + |
| 963 | +publicstaticvoidgetSame(ArrayList<String>input) { |
| 964 | +ArrayList<ArrayList<String>>ret =newArrayList<ArrayList<String>>(); |
| 965 | +if (input ==null) { |
| 966 | +return; |
| 967 | + } |
| 968 | + |
| 969 | +HashMap<String,ArrayList<String>>map =newHashMap<String,ArrayList<String>>(); |
| 970 | + |
| 971 | +for (Stringstr:input) { |
| 972 | +charc1 =str.charAt(0); |
| 973 | + |
| 974 | +intshift =c1 -'a'; |
| 975 | + |
| 976 | +intlen =str.length(); |
| 977 | +StringBuildersb =newStringBuilder(); |
| 978 | +for (inti =0;i <len;i++) { |
| 979 | +charc =str.charAt(i); |
| 980 | +intnum =c -'a'; |
| 981 | + |
| 982 | +num -=shift; |
| 983 | +if (num <0) { |
| 984 | +num +=26; |
| 985 | + } |
| 986 | + |
| 987 | +num +='a'; |
| 988 | +sb.append((char)num); |
| 989 | + } |
| 990 | + |
| 991 | +StringstrNew =sb.toString(); |
| 992 | +if (map.containsKey(strNew)) { |
| 993 | +map.get(strNew).add(str); |
| 994 | + }else { |
| 995 | +ArrayList<String>list =newArrayList<String>(); |
| 996 | +list.add(str); |
| 997 | +map.put(strNew,list); |
| 998 | + } |
| 999 | + } |
| 1000 | + |
| 1001 | +System.out.println(map.toString()); |
| 1002 | + } |
| 1003 | + |
| 1004 | +publicstaticStringgetShort(Stringurl)throwsNoSuchAlgorithmException { |
| 1005 | +if (url ==null) { |
| 1006 | +return""; |
| 1007 | + } |
| 1008 | + |
| 1009 | +if (mapFull.containsKey(url)) { |
| 1010 | +returnmapFull.get(url); |
| 1011 | + } |
| 1012 | + |
| 1013 | +Stringoriginal =url; |
| 1014 | +MessageDigestmd =MessageDigest.getInstance("MD5"); |
| 1015 | +md.update(original.getBytes()); |
| 1016 | +byte[]digest =md.digest(); |
| 1017 | +StringBuffersb =newStringBuffer(); |
| 1018 | +for (byteb :digest) { |
| 1019 | +sb.append(String.format("%02x",b &0xff)); |
| 1020 | + } |
| 1021 | + |
| 1022 | +System.out.println("original:" +original); |
| 1023 | +System.out.println("digested(hex):" +sb.toString()); |
| 1024 | + |
| 1025 | +Stringmd5 =sb.toString(); |
| 1026 | + |
| 1027 | +StringshortUrl =md5.substring(0,6); |
| 1028 | +if (map.containsKey(shortUrl)) { |
| 1029 | +for (inti =0;i <shortUrl.length();i++) { |
| 1030 | +StringBuildersbChange =newStringBuilder(shortUrl); |
| 1031 | +for (charc ='a';c <='z';c++) { |
| 1032 | +sbChange.setCharAt(i,c); |
| 1033 | +shortUrl =sbChange.toString(); |
| 1034 | +if (!map.containsKey(shortUrl)) { |
| 1035 | +break; |
| 1036 | + } |
| 1037 | + } |
| 1038 | + } |
| 1039 | +map.put(shortUrl,url); |
| 1040 | + }else { |
| 1041 | +map.put(shortUrl,url); |
| 1042 | + } |
| 1043 | + |
| 1044 | +mapFull.put(url,shortUrl); |
| 1045 | + |
| 1046 | +return"http://uber.gl/" +shortUrl; |
| 1047 | + } |
| 1048 | + |
| 1049 | +publicstaticStringgetFullUrl(Stringurl){ |
| 1050 | +if (url ==null) { |
| 1051 | +return""; |
| 1052 | + } |
| 1053 | + |
| 1054 | +if (map.containsKey(url)) { |
| 1055 | +returnmap.get(url); |
| 1056 | + }else { |
| 1057 | +return""; |
| 1058 | + } |
| 1059 | + } |
| 1060 | + |
| 1061 | +publicstaticArrayList<ArrayList<Integer>>getPath(TreeNoderoot,inttarget) { |
| 1062 | +ArrayList<ArrayList<Integer>>ret =newArrayList<ArrayList<Integer>>(); |
| 1063 | + |
| 1064 | +getPath(root,newArrayList<Integer>(),target,ret); |
| 1065 | +returnret; |
| 1066 | + } |
| 1067 | + |
| 1068 | +publicstaticvoidgetPath(TreeNoderoot,ArrayList<Integer>path,inttarget, |
| 1069 | +ArrayList<ArrayList<Integer>>ret) { |
| 1070 | +if (root ==null) { |
| 1071 | +return; |
| 1072 | + } |
| 1073 | + |
| 1074 | +path.add(root.val); |
| 1075 | +target -=root.val; |
| 1076 | +if (target ==0 &&root.left ==null &&root.right ==null) { |
| 1077 | +ret.add(newArrayList<Integer>(path)); |
| 1078 | +path.remove(path.size() -1); |
| 1079 | +return; |
| 1080 | + } |
| 1081 | + |
| 1082 | +if (target <0) { |
| 1083 | +path.remove(path.size() -1); |
| 1084 | +return; |
| 1085 | + } |
| 1086 | + |
| 1087 | +getPath(root.left,path,target,ret); |
| 1088 | +getPath(root.right,path,target,ret); |
| 1089 | +path.remove(path.size() -1); |
| 1090 | + } |
920 | 1091 | }
|