Skip to content

Commit f299cda

Browse files
committed
example.com
1 parent 7adcc04 commit f299cda

File tree

1 file changed

+177
-6
lines changed

1 file changed

+177
-6
lines changed

algorithm/interviews/uber/Example.java

Lines changed: 177 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package Algorithms.algorithm.interviews.uber;
22

3+
import java.security.MessageDigest;
4+
import java.security.NoSuchAlgorithmException;
35
import java.util.*;
6+
7+
import Algorithms.tree.TreeNode;
48
public class Example {
5-
public static void main(String[] strs) {
9+
public static void main(String[] strs) throws NoSuchAlgorithmException {
610
// printArray(sqrt2(100));
711
// printArray(sqrt2(300));
812
// printArray(sqrt2(400));
@@ -33,15 +37,53 @@ public static void main(String[] strs) {
3337
//
3438
// System.out.println(findPeak2(records));
3539

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+
TreeNode root = new TreeNode(1);
63+
TreeNode node1 = new TreeNode(5);
64+
TreeNode node2 = new TreeNode(6);
65+
TreeNode node3 = new TreeNode(4);
66+
67+
TreeNode node4 = new TreeNode(3);
68+
TreeNode node5 = new TreeNode(2);
69+
TreeNode node6 = new TreeNode(1);
70+
71+
root.left = node1;
72+
root.right = node2;
73+
74+
root.left.left = node3;
3875

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;
4178

42-
System.out.println(stringShift2("abC", 24));
79+
node5.right = node6;
80+
81+
System.out.println(getPath(root, 10));
4382
}
4483

84+
static HashMap<String, String> map = new HashMap<String, String>();
85+
static HashMap<String, String> mapFull = new HashMap<String, String>();
86+
4587
public static void printArray(int[] in) {
4688
for (int i = 0; i < in.length; i++) {
4789
System.out.print(in[i] + " ");
@@ -917,4 +959,133 @@ public static String stringShift2(String s,int shift){
917959

918960
return sb.toString();
919961
}
962+
963+
public static void getSame(ArrayList<String> input) {
964+
ArrayList<ArrayList<String>> ret = new ArrayList<ArrayList<String>>();
965+
if (input == null) {
966+
return;
967+
}
968+
969+
HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
970+
971+
for (String str: input) {
972+
char c1 = str.charAt(0);
973+
974+
int shift = c1 - 'a';
975+
976+
int len = str.length();
977+
StringBuilder sb = new StringBuilder();
978+
for (int i = 0; i < len; i++) {
979+
char c = str.charAt(i);
980+
int num = 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+
String strNew = sb.toString();
992+
if (map.containsKey(strNew)) {
993+
map.get(strNew).add(str);
994+
} else {
995+
ArrayList<String> list = new ArrayList<String>();
996+
list.add(str);
997+
map.put(strNew, list);
998+
}
999+
}
1000+
1001+
System.out.println(map.toString());
1002+
}
1003+
1004+
public static String getShort(String url) throws NoSuchAlgorithmException {
1005+
if (url == null) {
1006+
return "";
1007+
}
1008+
1009+
if (mapFull.containsKey(url)) {
1010+
return mapFull.get(url);
1011+
}
1012+
1013+
String original = url;
1014+
MessageDigest md = MessageDigest.getInstance("MD5");
1015+
md.update(original.getBytes());
1016+
byte[] digest = md.digest();
1017+
StringBuffer sb = new StringBuffer();
1018+
for (byte b : 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+
String md5 = sb.toString();
1026+
1027+
String shortUrl = md5.substring(0, 6);
1028+
if (map.containsKey(shortUrl)) {
1029+
for (int i = 0; i < shortUrl.length(); i++) {
1030+
StringBuilder sbChange = new StringBuilder(shortUrl);
1031+
for (char c = '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+
public static String getFullUrl(String url){
1050+
if (url == null) {
1051+
return "";
1052+
}
1053+
1054+
if (map.containsKey(url)) {
1055+
return map.get(url);
1056+
} else {
1057+
return "";
1058+
}
1059+
}
1060+
1061+
public static ArrayList<ArrayList<Integer>> getPath(TreeNode root, int target) {
1062+
ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>();
1063+
1064+
getPath(root, new ArrayList<Integer>(), target, ret);
1065+
return ret;
1066+
}
1067+
1068+
public static void getPath(TreeNode root, ArrayList<Integer> path, int target,
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(new ArrayList<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+
}
9201091
}

0 commit comments

Comments
 (0)