This repository is for me to study java, some interview questions are also included.
String x = "hello";
x.charAt(index);
x.equals("hello");
Integer.parseInt(string); //change string to integer
Integer.toString(num);
x.length();
char[] array = str.toCharArray(); //change string to char array
str = new String(array); //char array to string
str = new String(array, startposition, length);
String[] rets = list.toArray(new String[0]); //list of strings to string array
str.substring(i, j); //include i, exclude jStringBuilder sol = new StringBuilder();
sol.append(chr);
sol.toString();
sol.deleteCharAt(sol.length() - 1);
sol.delete(a, b); //a is included, b is not
StringBuilder sol = new StringBuilder(string);Deque<E> queue = new LinkedList<>();
queue.offerLast(var);
queue.pollFirst();
queue.peekFirst();
queue.isEmpty();
queue.size();
//or use queue interface
Queue<E> queue = new LinkedList<>();
queue.poll();
queue.peek();
queue.offer();Deque<E> stack = new LInkedList<>();
stack.offerFirst(var);
stack.pollFirst();
stack.peekFirst();List<E> myList = new ArrayList<>();
List<E> newList = new ArrayList<>(myList); //create a deep copy
myList.add(e);
myList.get(index);
myList.indexOf(e); //first occurrence or -1
myList.add(index, e);
myList.isEmpty();
myList.size();
myList.remove(index);
myList.set(index, e);
myList.subList(index1, index2); //[index1, index2]
myList.toArray();
List<List<Integer>> list = new ArrayList<>();
for (Integer a : myList) // iterate a listint[] array = new int[];
Arrays.toString(x)
char[] array;
//convert array to string/list
String a = new String(array);
ArrayList<Integer> a = new ArrayList<>(Arrays.asList(array));class myComparator implements Comparator<Cell>{
@Override
public int compare(Cell t1, Cell t2) {
if (t1.val == t2.val) {
return 0;
} else {
return t1.val < t2.val ? -1 : 1;
}
}
}
PriorityQueue<point> pQueue = new PriorityQueue<>(k, new myComparator()); //k need to be positive
PriorityQueue<point> pQueue = new PriorityQueue<>(k, Collections.reverseOrder());
pQueue.offer(e);
pQueue.poll();
pQueue.peek();HashMap<T1, T2> map = new HashMap<>();
map.put(key, value); //return old value
map.get(key); //return null if not inside
map.containsKey(key);
map.remove(key);
Set<Map.Entry<K,V>> entrySet(); //front is return type
Set<K> keySet();
Collections<V> values();
//HashMap is printbal HashSet<T> set = new HashSet<>();
set.add(key); //return false if already inside
set.contains(key);
set.remove(key);- 二分查找一要考虑会不会死循环,二要考虑会不会错过解
- LinkedList先搞个dummy head再说, 不要丧失头结点,确定cur.next, cur.key不会报错
- 递归不能忘记初始条件
- DFS别忘记加层数加一
- 谁小移谁的问题别忘记移!!!
- Java声明变量别忘记右边
- 所有问题先想为空的情况
- BFS: part1: push first node into queue, add it to visited part2: keep popping nodes from the queue (expand) push its neighbors to the queue, add them to visited list (generate) part3: repeat until the queue is empty or get results that we want
- DFS: part1: set nodes as visited, check termination condition part2: for neighbors that has not been visited, DFS recursively
- 内外循环千万别搞混了,i , j;
- c++ BST删除node需要删除吗?
- unsigned int 来接受 vec.size()