Skip to content

Instantly share code, notes, and snippets.

class Solution {
TreeNode a = null, b = null, pre = null;
public void recoverTree(TreeNode root) {
if (root == null)
return;
recover(root);
TreeNode temp = new TreeNode(0);
temp.val = a.val;
a.val = b.val;
@Jocelyn9
Jocelyn9 / gist:d288fd5e38e7313ebb65
Last active August 29, 2015 14:02
1.3 Given two strings, write a method to decide if one is a permutation of the other.
/* Time complexity O(nlogn), Space complexity O(1) */
class Solution1{
boolean isPerm(String s1, String s2){
if(s1==null||s2==null) return false;
if(s1.length()!=s2.length()) return false;
char[] arr1 = s1.toCharArray();
char[] arr2 = s2.toCharArray();
Arrays.sort(arr1);
@Jocelyn9
Jocelyn9 / gist:fb24c7201d4586ba2652
Last active August 29, 2015 14:02
1.1 Implement an algorithm to determine if a string has all unique characters. Whatif you cannot use additional data structures?
/*Solution 1:
Brute force.
Complexity: O(n^2)
Space complexity O(1)
*/
public class Solution1{
boolean isUnique(String str){
// boundary condition
if(str==null||str=="") return true;