id
stringlengths
36
36
text
stringlengths
1
1.25M
96264bba-4d86-4ac2-9056-970b47249d9a
private void processFields(Class<? extends Object> objectClass, Object object) throws StorageException, IllegalArgumentException, IllegalAccessException { Field[] fields = objectClass.getDeclaredFields(); for (Field field : fields) { if (isPersistant(field)) { field.setAccessible(true); writeField(field, object); } } }
53060dc9-c8a5-4ff6-a1ac-13ebbf3ef2eb
private boolean isSimpleType(Field field) { if (field.getType() == int.class || field.getType() == long.class || field.getType() == double.class || field.getType() == float.class || field.getType() == boolean.class || field.getType() == Integer.class || field.getType() == Double.class || field.getType() == Boolean.class || field.getType() == Long.class || field.getType() == Float.class || field.getType() == String.class) { return true; } return false; }
8c032a66-c8a7-471f-8ad5-ceae529551c1
private boolean isCollection(Object object) { return false; }
1d88f323-1718-418f-b585-9277d75475eb
private boolean isObjectReference(Object object) { // check to see if an instance in the object map matches this one, if it does return true return false; }
20b3f105-478d-4958-9ef7-ecf15a6d9460
private boolean isObject(Object object) { return false; }
9d3d76fa-5ec0-4536-9693-69dcf749344b
private boolean isPersistant(Field field) { // if has the @Persistant annotation return true Annotation[] annotations = field.getDeclaredAnnotations(); for (Annotation annotation : annotations) { if (annotation instanceof Persistant) { return true; } } return false; }
9c3fc3a3-223a-49cc-89c7-23f1d8aff83c
public TigerStorageStream() { stream = new String(); }
4136d2b6-1c07-43d7-a5b6-5da432f5bec9
public TigerStorageStream(String str) { stream = str; }
658cd824-04c9-4ad4-bdfd-be6eca22ef61
@Override public void startObject(String className, String instanceName) { // TODO: Do we need the internal separator if there is no object name stream += className; if (instanceName != null) { stream += INTERNAL_SEPARATOR + instanceName; } stream += OPEN_OBJ; }
0d34a734-dce2-43de-a296-d7565f478171
@Override public void endObject() { stream += CLOSE_OBJ; }
d65bee1b-3d08-4948-8305-d39c67b8160e
@Override public void writeField(Field field, Object parent) throws IllegalArgumentException, IllegalAccessException { if (stream.endsWith("{") != true) { stream += DEF_SEPARATOR; } stream += field.getName() + INTERNAL_SEPARATOR; if (field.getType() == int.class) { stream += field.getInt(parent); } else if (field.getType() == long.class) { stream += field.getLong(parent); } else if (field.getType() == double.class) { stream += field.getDouble(parent); } else if (field.getType() == float.class) { stream += field.getFloat(parent); } else if (field.getType() == boolean.class) { if (field.getBoolean(parent) == true) { stream += TRUE; } else { stream += FALSE; } } else if (field.getType() == Integer.class) { Integer value = (Integer)field.get(parent); if (value == null) { stream += NULL; } else { stream += value; } } else if (field.getType() == Double.class) { Double value = (Double)field.get(parent); if (value == null) { stream += NULL; } else { stream += value; } } else if (field.getType() == Boolean.class) { Boolean value = (Boolean)field.get(parent); if (value == null) { stream += NULL; } else { stream += value; } } else if (field.getType() == Long.class) { Long value = (Long)field.get(parent); if (value == null) { stream += NULL; } else { stream += value; } } else if (field.getType() == Float.class) { Float value = (Float)field.get(parent); if (value == null) { stream += NULL; } else { stream += value; } } else if (field.getType() == String.class) { String value = (String)field.get(parent); if (value == null) { stream += NULL; } else { stream += QUOTE + value + QUOTE; } } }
78927749-abb8-4d0f-9e80-8660c3880200
@Override public Object read() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, NoSuchFieldException { int index = stream.indexOf(OPEN_OBJ); String className = stream.substring(0, index); Class<?> theClass = Class.forName(className); Object obj = theClass.newInstance(); int comma = stream.indexOf(DEF_SEPARATOR, index); String fieldString = stream.substring(index + 1, comma); setField(theClass, obj, fieldString); System.out.println(fieldString); return obj; }
57cdaa07-cdf1-4cde-9c98-953e7d5f1080
private void setField(Class<?> theClass, Object obj, String fieldString) throws SecurityException, NoSuchFieldException, NumberFormatException, IllegalArgumentException, IllegalAccessException { int index = fieldString.indexOf(INTERNAL_SEPARATOR); String fieldName = fieldString.substring(0, index); String value = fieldString.substring(index + 1); System.out.println(value); Field field = theClass.getDeclaredField(fieldName); field.setAccessible(true); if (field.getType() == int.class) { field.setInt(obj, Integer.parseInt(value)); } }
fc313dce-44e1-4df7-ab68-44bcb916dc10
@Override public String toString() { return stream; }
4d2e32ff-b43b-4090-b11a-922b35a48a89
public void startWriteObject(String className, int id);
496b8258-ed23-426f-b880-6052add47d6e
public void endWriteObject();
180c2498-d085-46df-88d5-493ee7988ae0
public void writeIntField(String fieldName, int value);
3287973d-564d-4b10-bff7-3588fcab9853
public void writeStringField(String fieldName, String value);
baa196aa-9008-430f-a8fb-ee7470067088
public Object readObject() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, NoSuchFieldException;
567b7432-0779-43d7-94ea-6f8fe7949548
public SubjectClass() { myInt = 5; doNotPersist = 10; myString = "This is a string with a : in it."; nullString = null; }
d14790b4-6e21-43d7-9a3e-0d3392b7457e
public int getMyInt() { return myInt; }
2c798fe3-9fb0-4d7f-b5ea-8179c627f8e7
public String getMyString() { return myString; }
9f440175-890e-42f5-8b25-7071bccf3695
public String getNullString() { return nullString; }
dbb22e37-dadd-4534-a9e4-0f1c51982802
@Test public void testWriteSubjectClass() throws IllegalArgumentException, StorageException, IllegalAccessException { // object to save SubjectClass testObject = new SubjectClass(); // concrete stream to store object TigerStorageStream stream = new TigerStorageStream(); Storage storage = new Storage(stream); storage.write(testObject); Assert.assertTrue(stream.toString().compareTo("com.dungeontiger.Persist.SubjectClass{myInt:5,myString:\"This is a string with a : in it.\",nullString:null}") == 0); }
5545523e-12eb-4c3d-bc6b-9cad75c905da
@Test public void testReadSubjectClass() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, NoSuchFieldException { String testString = "com.dungeontiger.Persist.SubjectClass{myInt:26,myString:\"Some text\",nullString:null}"; // Intialize stream with test TigerStorageStream stream = new TigerStorageStream(testString); Storage storage = new Storage(stream); Object obj = storage.read(); Assert.assertTrue(obj instanceof SubjectClass); SubjectClass subject = (SubjectClass)obj; Assert.assertEquals(26, subject.getMyInt()); }
74c4cbf0-780a-434e-bb66-2156d0c5583a
@Test public void testSimplePersist() throws IllegalArgumentException, IllegalAccessException, StorageException { // object to save SubjectClass testObject = new SubjectClass(); // concrete stream to store object TigerStorageStream stream = new TigerStorageStream(); Storage storage = new Storage(stream); storage.write(testObject); // print out what was serialised System.out.println(stream.toString()); }
e2e1ca94-76df-4985-ac68-2e961087aaf5
@Test public void testSimpleLoad() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, NoSuchFieldException { PersistStreamSimple stream = new PersistStreamSimple("com.dungeontiger.Persist.SubjectClass:29132923;"); Persister persister = new Persister(stream); // do work Object obj = persister.read(); Assert.assertTrue(obj instanceof SubjectClass); }
3c64c063-adf8-414a-ae70-931cd04af703
@Test public void testSimpleLoadOneField() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, NoSuchFieldException { PersistStreamSimple stream = new PersistStreamSimple("com.dungeontiger.Persist.SubjectClass:3794357:myInt:int:26:;"); Persister persister = new Persister(stream); // do work Object obj = persister.read(); // test object Assert.assertTrue(obj instanceof SubjectClass); SubjectClass subject = (SubjectClass)obj; Assert.assertEquals(26, subject.getMyInt()); }
1728e001-9590-481f-9f26-e30c97dab32e
public static void main (String args[]) { System.out.println(); LRUCache testA =new LRUCache(2); testA.set(2,1); testA.get(2); testA.set(3,2); testA.get(2); testA.get(3); System.out.println(); }
0815b16e-91f8-4e7b-b1a6-09d4c20c781f
ListNode(int x, int y) { key = x; val = y; next = null; }
1064c9d9-015a-4eb9-ab6e-4ddeca7021c9
public LRUCache(int capacity) { _capacity = capacity; _start.next = _end; _end.prev = _start; }
9e0f38f4-753c-4947-848f-02e37790cb6a
public int get(int key) { if(_listLookup.containsKey(key)) { ListNode x = _listLookup.get(key); x.prev.next = x.next; x.next.prev = x.prev; x.prev = _end.prev; _end.prev.next = x; x.next = _end; _end.prev = x; return x.val; } return -1; }
5ef1ce4b-7aaf-4559-a87a-de9749ada7ef
public void set(int key, int value) { if(_listLookup.containsKey(key)) { ListNode x = _listLookup.get(key); x.val = value; x.prev.next = x.next; x.next.prev = x.prev; x.prev = _end.prev; _end.prev.next = x; x.next = _end; _end.prev = x; } else { if(_listLookup.size() < _capacity) { ListNode x = new ListNode(key, value); _listLookup.put(key, x); x.prev = _end.prev; _end.prev.next = x; x.next = _end; _end.prev = x; } else{ ListNode x = _start.next; _start.next = x.next; x.next.prev = _start; _listLookup.remove(x.key); ListNode y = new ListNode(key, value); _listLookup.put(key, y); y.prev = _end.prev; _end.prev.next = y; y.next = _end; _end.prev = y; } } }
89b7e251-839d-45b1-aa70-946d5ede0526
TreeNode(int x) { val = x; }
1828bc20-2c53-46f9-9053-5d41bf52f1d5
ListNode(int x) { val = x; next = null; }
e3cdb363-6722-4435-98b8-b5c3524ba1ac
public static void main (String args[]) { char C[][] = {{'1','1'}}; Tree testA =new Tree(); TreeNode A = testA.new TreeNode(1); A.left = testA.new TreeNode(0); //A.next = testA.new ListNode(2); //A.next.next = testA.new ListNode(3); //A.next.next.next = testA.new ListNode(4); int[] b={4,2,0,3,2,5}; testA.maximalRectangle(C); System.out.print("end"); }
ae228b49-f317-490e-a7be-4e402513ead4
public ArrayList<Integer> preorderTraversalRecur(TreeNode root) { TreeNode cur = root; ArrayList<Integer> res = new ArrayList<Integer>(); GetNode(root, res); return res; }
784eebfd-9795-47fe-91d1-fb3fd5eec26c
private void GetNode(TreeNode root, ArrayList<Integer> res) { if(root == null) return; res.add(root.val); GetNode(root.left, res); GetNode(root.right, res); }
f58418a7-d1e2-4dc2-8923-cb11862211fd
public ArrayList<Integer> preorderTraversal(TreeNode root) { TreeNode cur = root; Stack<TreeNode> path = new Stack<TreeNode>(); ArrayList<Integer> res = new ArrayList<Integer>(); if(root ==null) return res; path.add(root); while(!path.isEmpty()) { cur = path.pop(); res.add(cur.val); if(cur.right !=null) path.add(cur.right); if(cur.left !=null) path.add(cur.left); } return res; }
c75994d7-61c7-4dc8-a46d-a445acf5e281
public ArrayList<Integer> postorderTraversal(TreeNode root) { TreeNode cur = root; Stack<TreeNode> path = new Stack<TreeNode>(); ArrayList<Integer> res = new ArrayList<Integer>(); if(root ==null) return res; path.add(root); while(!path.isEmpty()) { cur = path.pop(); res.add(0, cur.val); if(cur.left !=null) path.add(cur.left); if(cur.right !=null) path.add(cur.right); } return res; }
c1422894-3506-4757-bfd7-e262c19226ab
public ArrayList<Integer> inorderTraversal(TreeNode root) { TreeNode cur = root; Stack<TreeNode> path = new Stack<TreeNode>(); ArrayList<Integer> res = new ArrayList<Integer>(); if(root ==null) return res; path.add(root); while(!path.isEmpty()) { cur = path.pop(); TreeNode left = cur.left; TreeNode right = cur.right; if(left == null && right == null) { res.add(cur.val); }else { cur.left =null; cur.right =null; if(right !=null) path.add(right); path.add(cur); if(left !=null) path.add(left); } } return res; }
c19e7304-9684-4cb4-8ec0-fbef56fd0606
public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) { ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); if(root == null) return res; GetNode(root, 0, res); return res; }
5b67bf67-b83f-4ac3-9980-a426c164575d
private void GetNode(TreeNode node, int level, ArrayList<ArrayList<Integer>> res) { if(res.size() == level) { res.add(new ArrayList<Integer>()); } res.get(level).add(node.val); if(node.left !=null) GetNode(node.left,level+1, res); if(node.right !=null) GetNode(node.right,level+1, res); }
97c2b795-3b8a-4520-9e0c-404b8c119132
public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root) { ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); if(root == null) return res; GetNode2(root, 0, res); return res; }
f2fba239-c099-4419-89d3-5d38eb90d4c0
private void GetNode2(TreeNode node, int level, ArrayList<ArrayList<Integer>> res) { if(res.size() == level) { res.add(new ArrayList<Integer>()); } if(level%2 ==0) res.get(level).add(node.val); else res.get(level).add(0, node.val); if(node.left !=null) GetNode2(node.left,level+1, res); if(node.right !=null) GetNode2(node.right,level+1, res); }
cfd0dffb-21a4-4601-8c47-30a7cfd0b933
public ArrayList<ArrayList<Integer>> levelOrderBottom(TreeNode root) { ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); if(root == null) return res; GetNode3(root, 0, res); return res; }
c852bc03-7f58-4dca-ad13-6460743b2508
private void GetNode3(TreeNode node, int level, ArrayList<ArrayList<Integer>> res) { if(res.size() == level) { res.add(0, new ArrayList<Integer>()); } res.get(res.size()-level-1).add(node.val); if(node.left !=null) GetNode3(node.left,level+1, res); if(node.right !=null) GetNode3(node.right,level+1, res); }
307a9212-abe4-4295-832c-7c678b91c74c
public boolean isSameTree(TreeNode p, TreeNode q) { if(p==null && q==null) return true; if(p!=null && q!=null && q.val==p.val) { return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); } else return false; }
74e7bd23-4417-4eb4-9b93-8aed699c462e
public boolean isSymmetric(TreeNode root) { if(root == null) return true; return isMirror(root.left, root.right); }
4e96f0ed-1386-410b-b712-3fd125e84af7
private boolean isMirror(TreeNode left, TreeNode right) { if(left== null && right == null) return true; if(left == null && right != null) return false; if(left != null && right == null) return false; if(left.val != right.val) return false; return isMirror(left.left, right.right) && isMirror(left.right, right.left); }
897aebf1-f4a3-4bc8-9766-8dbaf25f0407
public boolean isBalanced(TreeNode root) { if(root == null) return true; return isBalanced(root.left) && isBalanced(root.right) && isSameHeight(root.left, root.right); }
f33d4836-6b49-4d09-b8c4-0be08e110b2a
private boolean isSameHeight(TreeNode left, TreeNode right) { return Math.abs(getHeight(left) - getHeight(right))<=1; }
6fb2ebd8-f406-44af-90ad-a49ebf5dacda
private int getHeight(TreeNode root) { if(root==null) return 0; if(root.left == null && root.right ==null) return 1; if(root.left == null && root.right !=null) return getHeight(root.right) + 1; if(root.left != null && root.right ==null) return getHeight(root.left) + 1; return Math.max(getHeight(root.right), getHeight(root.left)) +1; }
46956422-22f5-4010-9fb1-2cb4dd611bfc
public void flatten(TreeNode root) { if(root == null) return; Stack<TreeNode> st = new Stack<TreeNode>(); st.add(root); TreeNode dummy = new TreeNode(-1); TreeNode curr=dummy; while(!st.isEmpty()) { TreeNode t = st.pop(); curr.right = t; curr.left = null; curr = t; if(t.right != null) st.add(t.right); if(t.left != null) st.add(t.left); } }
3584aed2-27c3-4d1e-8070-dc6ca18ce610
public int minDepth(TreeNode root) { if(root == null) return 0; if(root.left == null && root.right == null) return 1; if(root.left == null && root.right != null) return minDepth(root.right) +1; if(root.left != null && root.right == null) return minDepth(root.left) +1; return Math.min(minDepth(root.right), minDepth(root.left)) +1; }
7930b19a-a27c-4d29-a82d-875288b02067
public int maxDepth(TreeNode root) { if(root==null) return 0; if(root.left == null && root.right ==null) return 1; if(root.left == null && root.right !=null) return maxDepth(root.right) + 1; if(root.left != null && root.right ==null) return maxDepth(root.left) + 1; return Math.max(maxDepth(root.right), maxDepth(root.left)) +1; }
5d5733d7-6641-44b5-b342-c164271d7089
public TreeNode buildTree1(int[] preorder, int[] inorder) { if(preorder.length == 0 || inorder.length == 0) return null; TreeNode mid = new TreeNode(preorder[0]); if(preorder.length == 1) return mid; int midIndex = -1; for(int i=0; i< inorder.length; i++) { if(inorder[i] == mid.val) { midIndex = i; break; } } int[] leftInorder = {}, leftPreorder= {}; if(midIndex>0) { leftInorder = Arrays.copyOfRange(inorder, 0, midIndex); leftPreorder = Arrays.copyOfRange(preorder, 1, midIndex+1); } int[] rightInorder= {}, rightPreorder= {}; if(midIndex<inorder.length-1) { rightInorder = Arrays.copyOfRange(inorder, midIndex+1, inorder.length); rightPreorder = Arrays.copyOfRange(preorder, midIndex+1 , preorder.length); } mid.left = buildTree1(leftPreorder, leftInorder); mid.right = buildTree1(rightPreorder, rightInorder); return mid; }
d81bf708-f7f0-4bb9-a8a3-a3c5f874738f
public TreeNode buildTree(int[] inorder, int[] postorder) { if(postorder.length == 0 || inorder.length == 0) return null; TreeNode mid = new TreeNode(postorder[postorder.length-1]); if(postorder.length == 1) return mid; int midIndex = -1; for(int i=0; i< inorder.length; i++) { if(inorder[i] == mid.val) { midIndex = i; break; } } int[] leftInorder = {}, leftpostorder= {}; if(midIndex>0) { leftInorder = Arrays.copyOfRange(inorder, 0, midIndex); leftpostorder = Arrays.copyOfRange(postorder, 0, midIndex); } int[] rightInorder= {}, rightpostorder= {}; if(midIndex<inorder.length-1) { rightInorder = Arrays.copyOfRange(inorder, midIndex+1, inorder.length-1); rightpostorder = Arrays.copyOfRange(postorder, midIndex , postorder.length-2); } mid.left = buildTree(leftInorder, leftpostorder); mid.right = buildTree(rightInorder, rightpostorder); return mid; }
0f446f2c-68bf-4c0c-9b2a-dc318142ba7b
public int numTrees(int n) { if(n<=1) return 1; int res = 0; for(int i=1; i<=n; i++) { res+=numTrees(i-1) * numTrees(n-i); } return res; }
a68ee2c7-d3f3-405d-b32f-fb4ecf217c35
public boolean isValidBST(TreeNode root) { return isValid(root, Integer.MIN_VALUE, Integer.MAX_VALUE); }
e30494f0-093d-4c73-a53f-b9ac86907f41
private boolean isValid(TreeNode root, int min, int max) { if(root == null) return true; return root.val<max && min<root.val && isValid(root.left, min, root.val) && isValid(root.right, root.val, max); }
3ad25c90-131d-4468-a79f-7e8867f976ae
public TreeNode sortedArrayToBST(int[] num) { if(num.length == 0) return null; if(num.length == 1) return new TreeNode(num[0]); int midIndex = num.length/2; int[] left = Arrays.copyOfRange(num, 0, midIndex); int[] right = Arrays.copyOfRange(num, midIndex+1, num.length); TreeNode root = new TreeNode(num[midIndex]); root.left = sortedArrayToBST(left); root.right = sortedArrayToBST(right); return root; }
c9aac960-7a3b-4bdd-a3b3-f2987037c756
public TreeNode sortedListToBST(ListNode head) { ListNode p = head; List<Integer> li = new ArrayList<Integer>(); while(p!=null) { li.add(p.val); p=p.next; } return sortedListToBST(li); }
6c5fe457-a6a1-46df-8f08-35f3ac25311c
private TreeNode sortedListToBST(List<Integer> num) { if(num.size() == 0) return null; if(num.size() == 1) return new TreeNode(num.get(0)); int midIndex = num.size()/2; List<Integer> left = num.subList(0, midIndex); List<Integer> right =num.subList(midIndex+1, num.size()); TreeNode root = new TreeNode(num.get(midIndex)); root.left = sortedListToBST(left); root.right = sortedListToBST(right); return root; }
6237d29c-e964-4af2-9318-cd80474cd346
public void recoverTree(TreeNode root) { Stack<TreeNode> a = new Stack<TreeNode>(); TreeNode p = root; TreeNode lastNode=null, first=null, second=null; while(!a.isEmpty() || p!=null) { if(p==null) { p=a.pop(); if(lastNode!=null && p.val < lastNode.val) { if(first== null) { first = lastNode; second = p; } else second = p; } lastNode = p; p=p.right; }else { a.add(p); p=p.left; } } int t = first.val; first.val = second.val; second.val = t; }
97af73ac-8db6-41cd-8180-5a9bca837778
public boolean hasPathSum(TreeNode root, int sum) { if(root == null) return false; boolean bleft = false, bright=false; if(root.left==null && root.right==null) { if(sum==root.val) return true; else return false; } if(root.left != null) bleft =hasPathSum(root.left, sum-root.val); if(root.right != null) bright =hasPathSum(root.right, sum-root.val); return bleft||bright; }
b8053e87-c5fd-4098-ab08-f366bf144f9c
public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) { ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); if(root == null) return res; ArrayList<ArrayList<Integer>> listleft, listright; if(root.left==null && root.right==null) { if(sum==root.val) { ArrayList<Integer> t = new ArrayList<Integer>(); t.add(root.val); res.add(t); return res; } } if(root.left != null) { listleft =pathSum(root.left, sum-root.val); for(ArrayList<Integer> t:listleft) { t.add(0, root.val); res.add(t); } } if(root.right != null) { listright =pathSum(root.right, sum-root.val); for(ArrayList<Integer> t:listright) { t.add(0, root.val); res.add(t); } } return res; }
a45b63b0-f25a-4c68-8fa1-573fa647c760
public int maxPathSum(TreeNode root) { int[] res = {Integer.MIN_VALUE}; maxSum(root, res); return res[0]; }
c285eda9-fbf4-4fbf-892e-80ae64b050f7
private int maxSum(TreeNode root, int[] res) { if(root==null) return 0; int maxleft = maxSum(root.left, res); int maxright = maxSum(root.right, res); res[0] = Math.max(res[0], Math.max(Math.max(Math.max(maxleft+maxright, 0), maxleft),maxright) + root.val); return Math.max(0, Math.max(maxleft, maxright)) + root.val; }
028253f0-66ce-4ecc-b63c-01755da38116
public int sumNumbers(TreeNode root) { if(root == null) return 0; ArrayList<String> res = GetNumbers(root); int sum = 0; for(String e:res) { sum += Integer.parseInt(e); } return sum; }
9ca61902-06d2-40a3-b894-5c86d49a3af7
private ArrayList<String> GetNumbers(TreeNode root) { ArrayList<String> resString = new ArrayList<String>(); if(root.left==null && root.right == null) { resString.add(String.valueOf(root.val)); }else { if(root.left!=null) resString.addAll(GetNumbers(root.left)); if(root.right!=null) resString.addAll(GetNumbers(root.right)); for(int i=0; i<resString.size(); i++) { resString.set(i, String.valueOf(root.val) + resString.get(i)); } } return resString; }
2d04cd77-a8a3-4016-9226-fd216b72f6a9
public void connectBalanced(TreeLinkNode root) { if(root==null) return; if(root.left!=null) root.left.next = root.right; if(root.right!=null) { if(root.next != null) root.right.next = root.next.left; else root.right.next = null; } connectBalanced(root.left); connectBalanced(root.right); }
33065d5e-373a-41bd-b0d0-0ef2061fbfaf
public void connect(TreeLinkNode root) { if(root==null) return; TreeLinkNode cur = root; TreeLinkNode prev = null; TreeLinkNode leftMost = null; while(cur!=null) { if(cur.left!=null) { if(prev != null)prev.next = cur.left; prev = cur.left; if(leftMost == null)leftMost = cur.left; } if(cur.right!=null) { if(prev != null)prev.next = cur.right; prev = cur.right; if(leftMost == null)leftMost = cur.right; } cur = cur.next; } connect(leftMost); }
daaed654-ae55-4067-a5b3-1778a37afdd9
public ArrayList<TreeNode> generateTrees(int n) { ArrayList<TreeNode> res = new ArrayList<TreeNode>(); if(n==0){ res.add(null); }else res = makenode(1, n); return res; }
263ffe9c-62e0-4f39-ab81-2f34809d1a78
private ArrayList<TreeNode> makenode(int start, int end) { ArrayList<TreeNode> res = new ArrayList<TreeNode>(); if(start>end) { res.add(null); return res; } for(int i=start; i<=end; i++) { ArrayList<TreeNode> left = makenode(start, i-1); ArrayList<TreeNode> right = makenode(i+1, end); for(TreeNode l:left) { for(TreeNode r:right) { TreeNode node = new TreeNode(i); node.left = l; node.right = r; res.add(node); } } } return res; }
17e05bbe-490c-48e0-aa6a-324b81dec123
public int largestRectangleAreaTLE(int[] height) { int max = 0; for(int i=0; i<height.length; i++) { int min = height[i]; for(int j=i; j>=0; j--) { min = Math.min(height[j], min); max = Math.max(max, (i-j+1)*min); } } return max; }
55252287-f74e-448d-b5ab-4b3fde0d446e
public int maximalRectangle(char[][] matrix) { if(matrix.length == 0 || matrix[0].length==0) return 0; int[] height = new int[matrix[0].length]; int max = 0; for(int i=0; i<matrix.length; i++) { for(int j=0; j<matrix[i].length; j++) { if(matrix[i][j]=='0') height[j]=0; else { height[j]= height[j]+1; } } max = Math.max(max, largestRectangleAreaTLE(height)); } return max; }
47eb5eec-3bbb-4c8a-9395-5b5d6c66a9c2
public int largestRectangleArea(int[] height) { int max = 0; if(height.length ==0) return 0; int[] nh = Arrays.copyOf(height, height.length+1); nh[height.length] = 0; Stack<Integer> large = new Stack<Integer>(); for(int i=0; i<nh.length; i++) { if(large.isEmpty() ||nh[i]>=nh[large.peek()]) { large.add(i); }else { while(!large.isEmpty() && (nh[large.peek()]>nh[i] || large.peek() ==0)) { int j=large.pop(); max = Math.max(max, (large.isEmpty()?i:(i-j))*nh[j]); } large.add(i); } } return max; }
ba2b4f27-496f-438e-acdf-8b75fe630904
public TreeNode getTree(int[] numbers) { return getTreeNode(numbers, 0, numbers.length-1); }
0e3eb3a6-0c18-4a97-937f-d48ac803440e
private TreeNode getTreeNode(int[] numbers, int start, int end) { if(end<start) return null; int mid = (start+end)/2; int val = numbers[mid]; TreeNode root = new TreeNode(val); root.left = getTreeNode(numbers, start, mid-1); root.right = getTreeNode(numbers, mid+1, end); return root; }
ce454108-39f1-4039-bd94-c373a00aa0a1
public ArrayList<LinkedList<TreeNode>> getlistFromTree(TreeNode root) { ArrayList<LinkedList<TreeNode>> res = new ArrayList<LinkedList<TreeNode>>(); if(root==null) return res; LinkedList<TreeNode> l = new LinkedList<TreeNode>(); l.add(root); res.add(l); int level = 1; while(true) { LinkedList<TreeNode> pl = res.get(level); LinkedList<TreeNode> nl = res.get(level); for(TreeNode node:pl) { if(node.left!=null) nl.add(node.left); if(node.right!=null) nl.add(node.right); } if(nl.size()>0) res.add(nl); else return res; level++; } }
a4fda6e5-548c-4710-bca2-3ce10df12ef8
public TreeNode getancestor(TreeNode root, TreeNode p, TreeNode q) { if(cover(root.left, p) && cover(root.left, q)) return getancestor(root.left, p, q); if(cover(root.right, p) && cover(root.right, q)) return getancestor(root.right, p, q); return root; }
58922398-fa61-4202-8062-bea55e12159c
private boolean cover(TreeNode root, TreeNode p) { Stack<Integer> s = new Stack<Integer>(); if(root == null) return false; if(root == p) return true; return cover(root.left,p)||cover(root.right,p); }
9ac06f3b-be29-45a7-b589-46920dad052c
public boolean isSubTree(TreeNode p, TreeNode q) { if(q==null) return true; if(p==null) return false; if(p.val==q.val) if(isMatch(p, q)) return true; return isSubTree(p.left, q) || isSubTree(p.right, q); }
9caf1e41-5dcd-40d7-9f1f-443412638f31
private boolean isMatch(TreeNode p, TreeNode q) { if(p == null && q == null ) return true; if(p == null || q == null ) return false; if(p.val == q.val) return isMatch(p.left, q.left) && isMatch(p.right, q.right); return false; }
3d88bb1f-eea2-4b96-8b2c-d4392352fad0
private void getPath(TreeNode root, int sum, ArrayList<Integer> path, ArrayList<ArrayList<Integer>> res) { if(root.left==null && root.right ==null&& root.val == sum) { path.add(root.val); ArrayList<Integer> newpath=new ArrayList<Integer>(); newpath.addAll(path); res.add(newpath); path.remove(root.val); return; } path.add(root.val); if(root.left!=null) { getPath(root.left, sum-root.val, path, res); } if(root.right!=null) { getPath(root.right, sum-root.val, path, res); } path.remove(root.val); return; }
69ab3093-7c33-4771-8482-b03556311c0d
public static void main (String args[]) { int a = 20; Set<String> B = new HashSet<String>(); B.add("dog"); B.add("s"); B.add("gs"); char[][] board = new char[3][3]; String[] t = {"a","b","c","d","e"}; stringtest testA =new stringtest(); //testA.longestPalindrome(t); testA.fullJustify(t, 3); StringBuffer sb= new StringBuffer("init"); //sb.toString().insert(index, str, offset, len).delete(start, end).charAt(index).append(arg0) }
9dc010ce-92c7-4133-870c-b53fe199f4b9
public String reverseWords(String s) { String[] x = s.split(" "); String res = ""; for(int i=x.length-1; i>=0; i--) { String n = x[i]; if(n.trim().length()!= 0) { res += n + " "; } } return res.trim(); }
4b3a74db-5dd0-42d2-a1d6-9bffcd540e19
public boolean isPalindrome(String s) { s = s.replaceAll("[^A-Za-z0-9]", ""); char[] inputs = s.toCharArray(); int size = inputs.length; for(int i=0, j=size-1; i<j; i++,j--) { if(Character.toUpperCase(inputs[i])!=Character.toUpperCase(inputs[j])) return false; } return true; }
7224bebc-bfe7-42dd-8090-451fa23f9a96
public String strStr(String haystack, String needle) { int size1 = haystack.length(); int size2 = needle.length(); if(size2 ==0) return haystack; if(size1==0) return null; for(int i=0; i<size1-size2+1; i++) { for(int j=0; j<size2; j++) { if(haystack.charAt(i+j) != needle.charAt(j)) { break; } else { if(j==size2-1) { return haystack.substring(i); } } } } return null; }
1862ae5c-c380-4a86-820d-bfebef39fa65
public int atoi(String str) { str = str.trim(); if(str.length() ==0) return 0; int minus = 1; if(str.charAt(0) == '-') { minus = -1; str = str.substring(1); }else if (str.charAt(0) == '+') { str = str.substring(1); } if(str.length() ==0 || str.charAt(0)<'0'||str.charAt(0)>'9') return 0; int res = 0; for(int i=0; i<str.length(); i++) { if(str.charAt(i)<'0'||str.charAt(i)>'9') break; else { if(res<=Integer.MAX_VALUE/10 && Integer.MAX_VALUE - res*10 >=str.charAt(i) - '0') { res = res*10 + str.charAt(i) - '0'; }else { if( minus == 1) return Integer.MAX_VALUE; else return Integer.MIN_VALUE; } } } return res*minus; }
0cc602a1-939d-4410-8bd6-faf4b28c3b8b
public String addBinary(String a, String b) { int sizeA = a.length(); int sizeB = b.length(); if (sizeA ==0) return b; if (sizeB==0) return a; String res = ""; int r = 0; int carryover = 0; for(int i=sizeA-1, j = sizeB-1; i>=0 || j>=0; ) { char ca = '0'; char cb = '0'; if(i>=0) ca = a.charAt(i); if(j>=0) cb = b.charAt(j); if(ca=='0' && cb=='0') { r=carryover; carryover = 0; }else if((ca=='0' && cb=='1') || (ca=='1' && cb=='0')) { if(carryover==0) { r=1; carryover = 0; }else { r=0; carryover = 1; } }else if(ca=='1' && cb=='1') { r=carryover; carryover = 1; }else { return ""; } res = String.valueOf(r)+res; i--; j--; } if(carryover == 1) res = "1"+res; return res; }
9e32a5ea-5648-4491-8f6b-a8ee88cb1326
public String longestPalindrome(String s) { int longestSize = -1; String res = ""; if (s.length() ==0) return s; for(int i = 0; i<s.length(); i++) { //get the palindrome centered by i for(int j=0; j<=i&&j<s.length()-i; j++) { if(s.charAt(i-j) == s.charAt(i+j)) { if(j*2+1>longestSize) { longestSize = j*2+1; res = s.substring(i-j, i+j+1); } }else break; } for(int j=0; j<=i&&j<s.length()-i-1; j++) { if(s.charAt(i-j) == s.charAt(i+j+1)) { if(j*2+2>longestSize) { longestSize = j*2+2; res = s.substring(i-j, i+j+2); } }else break; } } return res; }
2d3e8514-d24e-40a3-b656-66980ec183be
public boolean isMatch2(String s, String p) { int sizep = p.length(); int sizes = s.length(); if(sizep==0) return sizes==0; if(sizep==1) { if(sizes ==1 && (p.charAt(0)=='.' || p.charAt(0) ==s.charAt(0))) { return true; }else return false; }else { if(p.charAt(1)=='*') { if(sizes > 0 && (p.charAt(0)=='.' || p.charAt(0) ==s.charAt(0))) { return isMatch(s.substring(1), p) || isMatch(s, p.substring(2)); }else return isMatch(s, p.substring(2)); }else { if(sizes > 0 && (p.charAt(0)=='.' || p.charAt(0) ==s.charAt(0))) { return isMatch(s.substring(1), p.substring(1)); } else return false; } } }
4076013b-6c92-4eaa-8360-7abf311025c6
public boolean isMatch(String s, String p) { int sizep = p.length(); int sizes = s.length(); if(sizep ==0) return sizes==0; if(sizes ==0) { if(p.charAt(0)=='*') return isMatch(s, p.substring(1)); else return false; }else { if(p.charAt(0)=='?'|| p.charAt(0) == s.charAt(0)) { return isMatch(s.substring(1), p.substring(1)); }else if(p.charAt(0)=='*') { while(p.length()>1 && p.charAt(1)=='*') { p=p.substring(1); } return isMatch(s, p.substring(1)) || isMatch(s.substring(1), p); }else return false; } }
a9948654-957f-4990-bd9e-c9d5e53a8108
public String longestCommonPrefix(String[] strs) { String res=""; if(strs.length==0) return res; for(int i=0;;i++) { if(strs[0].length() == i) return res; char r = strs[0].charAt(i); for(int j=0; j<strs.length; j++) { if(strs[j].length() == i || r != strs[j].charAt(i) ) return res; } res = res + strs[0].charAt(i); } }
539ea68d-1d27-4325-86f6-5721ee50513e
public boolean isNumber(String s) { if(s.trim() == "") return false; s = s.trim(); if(s.startsWith("-") || s.startsWith("+")) s=s.substring(1); if(s.length() == 0) return false; int point = -1; int science = -1; for(int i = 0; i< s.length(); i++) { if((s.charAt(i)>'9' || s.charAt(i)<'0') && s.charAt(i)!='e' && s.charAt(i)!='.' && s.charAt(i)!='+' && s.charAt(i)!='-') return false; if((s.charAt(i)=='+' || s.charAt(i)=='-') && ( i==0 || s.charAt(i-1) != 'e' || i==s.length()-1)) return false; if(s.charAt(i)=='.') { if(point != -1) return false; point = i; } if(s.charAt(i)=='e') { if(science != -1) return false; science =i; } } if(point == 0 && s.length()==1) { return false; } if(science == 0 || science==s.length()-1) { return false; } if(point > science && science!=-1) { return false; }else if(point == 0 && science==1) { return false; } return true; }
29ba02e6-8e2a-49fd-8088-9b2cae30a040
public String intToRoman(int num) { String str = ""; String symbol[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; int value[]= {1000,900,500,400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; for(int i=0; i< value.length; ) { if(num>=value[i]) { num = num - value[i]; str = str + symbol[i]; }else { i++; } } return str; }
e674ae46-6594-4c1e-b47b-1cfcfcd43921
public int romanToInt(String s) { int res = 0; for(int i =0; i<s.length();i++) { if(i<s.length()-1) { String x = s.substring(i, i+2); switch (x){ case "CM": res += 900;i++; break; case "CD": res += 400;i++;break; case "XC": res += 90;i++;break; case "XL": res += 40;i++;break; case "IX": res += 9;i++;break; case "IV": res += 4;i++;break; default: res += mapSingle(s.substring(i, i+1)); } }else res += mapSingle(s.substring(i, i+1)); } return res; }
501af123-f2da-4aad-8142-f0fe160a1bc1
private int mapSingle(String s) { switch (s){ case "M": return 1000; case "D": return 500; case "C": return 100; case "L": return 50; case "X": return 10; case "V": return 5; case "I": return 1; default: return 0; } }