Coding Note

  • Model the problem
  • Find an algorithm to solve it
  • Fast enough? Fits in memory?
  • If not, figure out why.
  • Find a way to address the problem.
  • Iterate until satisfied

注意!!!

  1. 千萬不可以用for迴圈 poll queue同時, 用queue.size()作為終止條件,必須先用int 固定他的大小
  2. PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
  3. 一定要仔細看題目, 是binary search tree 還是 binary tree
  4. StringBuilder => deleteCharAt(int index)

  5. Boundary of 32-bit signed integer

2147483647 ~ -2147483648

  • Observation

How can we use the previous information (known and correct) to get the current result?

Convert

Int to CharArray

int -> string -> charArray

char[] a = String.valueOf(N).toCharArray();

charArray -> string -> int

Integer.valueOf(new String(a));

String to int

Integer.parseInt(s);

Integer.valueOf(s);

Integer.valueOf("")和Integer.parseInt("")内部实现是一样的,它们之间唯一的区别就是Integer.valueOf(“”)返回的是一个Integer对象,而Integer.parseInt(“”)返回的是一个基本类型的int

String to double

Double.parseDouble(s);

String

indexOf time complexity => O(m*n)

The complexity of Java's implementation of indexOf is O(m*n) where n and m are the length of the search string and pattern respectively.

String startsWith

boolean startsWith(String prefix) 
boolean startsWith(String prefix, int offset)

Tests if this string starts with the specified prefix.
Tests if the substring of this string beginning at the specified index starts with the specified prefix.

Join

 String gfg1 = String.join(" < ", "Four", "Five", "Six", "Seven");
 System.out.println(gfg1); 

Four < Five < Six < Seven
while(l<=r) && l= mid+1 && r= mid -1

if mid could be the answer until r and l meet,

while(l < r)  && l = mid+1 && r = mid

Regular expression for split

"Ram-sita-laxman".split("");

This splits your string on every zero-length string. There is a zero-length string between every character. Therefore, the result is:

["", "R", "a", "m", "-", "s", "i", "t", "a", "-", "l", "a", "x", "m", "a", "n"]

Now, I modify my regular expression ("") to only match zero-length strings if they are followed by a dash.

"Ram-sita-laxman".split("(?=-)");

["Ram", "-sita", "-laxman"]

In that example, the ?= means "lookahead". More specifically, it mean "positive lookahead". Why the "positive"? Because you can also have negative lookahead (?!) which will split on every zero-length string that is not followed by a dash:

"Ram-sita-laxman".split("(?!-)");

["", "R", "a", "m-", "s", "i", "t", "a-", "l", "a", "x", "m", "a", "n"]

You can also have positive lookbehind (?<=) which will split on every zero-length string that is preceded by a dash:

"Ram-sita-laxman".split("(?<=-)");

["Ram-", "sita-", "laxman"]

Finally, you can also have negative lookbehind (?<!) which will split on every zero-length string that is not preceded by a dash:

"Ram-sita-laxman".split("(?<!-)");

["", "R", "a", "m", "-s", "i", "t", "a", "-l", "a", "x", "m", "a", "n"]

These four expressions are collectively known as the lookaround expressions.

Comparator in PriorityQueue

a. Lambda

PriorityQueue<Integer> queue = new PriorityQueue<>(k, (a,b) -> b - a);

b. Overide

PriorityQueue<Integer> pQueue = new PriorityQueue<Integer>(k, new Comparator<Integer>() {  
  public int compare(Integer w1, Integer w2) {                         
      return w1.compareTo(w2);  
  }      
});

Implementation note: this implementation provides O(log(n)) time for the enqueing and dequeing methods (offer, poll, remove() and add); linear time O(n) for the remove(Object) and contains(Object) methods; and constant time O(1) for the retrieval methods (peek, element, and size).

c. Compare Returns:

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Get from Map

// get key
for(String key: map.keySet()){
  // do anything you want
}

// only get values
for(List<String> list : map.values()){
    if(list.size() > 1){
      res.add(list);
    }
}

// Entry Set
for(Map.Entry<String, Integer> e : mail.entrySet()){
   int value = e.getValue();
   int key = e.getKey();
}

Random

Random rand = new Random(); 
int value = rand.nextInt(50);

This will give value from 0 to 49.

For 1 to 50: rand.nextInt((max - min) + 1) + min;

Exception

Error: An Error indicates serious problem that a reasonable application should not try to catch.
Exception: Exception indicates conditions that a reasonable application might try to catch.

throw new IllegalArgumentException("divided by zero");

Tips

List as array value type

List<Integer>[] a = new ArrayList[n];

you must make sure that you add the initialization

a[i] = new ArrayList<>();

Set add can be used to be one of the if rules

  if(node.left != null && set.add(node.left)) {
     queue.offer(node.left);
  }

Alphanumeric

Judge whether it is a letter or digit : **Character.isLetterOrDigit(char ch)

Arrays binary search

static int    binarySearch(int[] a, int fromIndex, int toIndex, int key)

Searches a range of the specified array of ints for the specified value using the binary search algorithm.

Assign list value while declaration

List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");

Pronounce

O(2^n) : Two to the power of n

O(n^2) : n squared

O(n^3) : n cubed

results matching ""

    No results matching ""