waynelin

导航

leetcode

1. Binary Tree (each node has at most two children)

definition of Tree: a directed acyclic graph which has N nodes and N-1 vertices.

 

pre-order traversal: root-> left subtree -> right subtree

in-order traversal: left subtree-> root -> right subtree (for binary search tree, we can get all the data in sorted order by using in-order traversal)

post-order traversal: left subtree-> right subtree -> root (deletion process; mathematical expression)

 

2. Java Stack class

Stack<Object> stack = new Stack<Object>();
# Methods: push(), pop(), empty() ...

 

3. Java Queue class

// 队列是一种特殊的线性表,它只允许在表的前端进行删除操作,而在表的后端进行插入操作。

// LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用。

Queue<o> queue = new LinkedList<o>();

// Methods: add(), offer() with exception, remove() with exception, poll()

 

4. Java Map class (dictionary)

  An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value.

Map<object o1, object o2>  map = new HashMap<object o1, object o2>();


// Methods 1: map.containsKey(object key); map.containsValue(object value); return boolean
// Methods 2: map.get(object key); returns the value to which the specified key is mapped, or null if this map contains no mapping for the key
// Methods 3: map.put(K key, V value); Associates the specified value with the specified key in this map (optional operation)

 

5. Array/ String Problems can be solved by "sliding window" [i, j)

   1) Set<Character> or Map<Character, Integer>

public int lengthOfLongestSubstring(String s) {
    
    // Methods 2 sliding window
        int length = s.length();
        Set<Character> set = new HashSet<Character>();
        
        int i = 0, j = 0, result = 0;
        
    
        
        while(i < length && j < length){
            if(set.contains(s.charAt(j))){
                // result = Math.max(result, j - i);  特殊情况更新,不好
                set.remove(s.charAt(i));
                i++;
            }else{
                set.add(s.charAt(j));
                j++;
                result = Math.max(result, j - i);  // 随时更新
            }
        }
        //return Math.max(result, j - i);
        return result;
}

  2) when manipulating the character one by one, String.toCharArray() might be used. The first character is at 0 index.

 

6. StringBuffer

// Constructor and Description
StringBuffer()
//Constructs a string buffer with no characters in it and an initial capacity of 16 //characters.
StringBuffer(CharSequence seq)
//Constructs a string buffer that contains the same characters as the specified //CharSequence.
StringBuffer(int capacity)
//Constructs a string buffer with no characters in it and the specified initial //capacity.
StringBuffer(String str)
//Constructs a string buffer initialized to the contents of the specified string.

// Methods
StringBuffer.append(char c)
StringBuffer.appedn(StringBuffer sb)
String.toString(); // return String;

 

7. Integer overflow

  32-bit signed integer range is from [-2^31, 2^31-1]. When check is needed, we can define "long" type to store the number, and check whether it overflows.

  负数求余

 

8. The use of "Case Switch" can be replaced by Array, when the switch use number to determine specified case.

 

9. If you want to know whether a string is the prefix of another string, you can use this decision to check

if(b.indexOf(a) == 0){
    // a is the prefix of b
    }

 

10. List

List<List<Integer>> result = new ArrayList<List<Integer>>();
List<Integer> level = new ArrayList<Integer>();
level.add(Integer i);
result.add(level);

 

11. Class Arrays

method sort  ps: return void

Arrays.sort(int[] array); // the sorting algorithm is a Dual-Pivot Quicksort providing O(n*log(n))

method asList to initialize an ArrayList

// method asList

@SafeVarargs
// public static <T> List<T> asList(T... a)

// Returns a fixed-size list backed by the specified array. (Changes to the returned // list "write through" to the array.) This method acts as bridge between 
// array-based and collection-based APIs, in combination with Collection.toArray(). // The returned list is serializable and implements RandomAccess.

// This method also provides a convenient way to create a fixed-size list initialized // to contain several elements:

List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
 

 

12. When the comparison between numbers in an array is necessary, "Arrays.sort(int[] nums)" can be used. Then, using two pointers to compute is preferred.

 

13. Convert a char in a string to int

int index = Character.getNumericValue(digits.charAt(i)); // Point 4: char to int

  排列组合问题可以用bfs, queue

 

14. 一个个加字符,直接 str + "x"

 

15. LC22 回溯法剪枝 / LC39 回溯法

 

16. string concatenation

String.valueOf(i); // int to string

str.concat(s); //
str += s; //

+可以把任何类型的数据连接起来

concat:将指定字符串连接到此字符串的结尾。

 

17. 值传递(pass-by-value)和引用传递(pass-by-reference)

https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value/73021#73021

 

18. in-place algorithm: coder should handle the problem using the original variables' memory space and constant extra memory.

 

19. Use Comparator to sort complex array list (LC56)

/* Comparator for sorting the list by start*/
    public static Comparator<Interval> startComparator = new Comparator<Interval>() {
        public int compare(Interval i1, Interval i2) {
            int start_1 = i1.start;
            int start_2 = i2.start;
            
            return start_1 - start_2; // if start_1 - start_2 > 0 then i1 > i2
        }
    };

/* main() */
Collections.sort(intervals, startComparator);

 

posted on 2018-02-26 20:59  维尼林  阅读(237)  评论(0编辑  收藏  举报