int[] res = new int[]{-1, -1};
String res = “”;
输出类型List<List>如何被初始化?List<List> res = new ArrayList<>(); 有关这些复杂类型的初始化还有待于进一步研究
List res = new ArrayList<>();//不确定长度的list
在Java中 是不是List用[] 但是Array用{}
二维数组: <code>boolean[][] dp = new boolean[s.length()][s.length()];</code>//二维数组也是array,因此初始化的时候要么长度给定 要么直接初始化元素,见下:
二维数组:<code>int[][] a = {{1,2}{3,4}}</code>
HashMap<Integer, Integer> map = new HashMap<>();
值得注意的是 hashmap不能直接用值进行初始化(?)只能用map.put(k,v)进行放入值
HashSet set = new HashSet<>();
array的几种初始化方式:<code>int[] a; int b[]; //数组的声明 int[] array1 = {4,5,6}; int[] array2 = new int[3]; int[] array3 = new int[]{1,2,3}//数组的初始化</code>
输出链表初始化:<code>ListNode dummy = new ListNode(0);</code> 而且要注意的是 链表的使用一般都是要链表指针的 如下:<code>ListNode cur = dummy;//链表的遍历是通过指针来完成</code>
StringBuilder类型数据初始化: <code>StringBuilder res = new StringBuilder();</code>
数组型StringBuilder类型数据初始化:<code>StringBuilder[] res = new StringBuilder[number_of_rows];//与数组结合</code>
Stack初始化<code>Stack<Character> stack = new Stack<Character>();</code>
PQ初始化<code>PriorityQueue<ListNode> queue = new PriorityQueue<>(lists.length, (a, b) -> a.val - b.val);</code>或者<code>Queue<TreeNode> queue = new LinkedList<>();</code> OR: <code>PriorityQueue<Integer> heap = new PriorityQueue<Integer>((n1, n2) -> map.get(n1) - map.get(n2));</code>//and heap will be map with sorted in its value
Deque的初始化:<code>Deque<Integer> deque = new LinkedList<>();</code>
二维数据的初始化总结如下:(理论上来说 能用Array的都可以用ArrayList去做 只是有时候输出的要求已经定好 最后必须进行相关转化)
内层外层都需要ArrayList(即内外层长度都不定): 这种情况很常见,直接<code>List<List<Integer>> res = new ArrayList<>(); List<Integer> list = new ArrayList<>();</code>
内层外层都需要Array(内层外层长度都固定): <code>int[][] res = new int[length1][length2]; int[] list = new int[length];</code>
外层ArrayList,内层Array(外层长度不固定 内层长度固定 比如LC56): <code>List<int[]> list = new ArrayList<>();</code>
外层Array,内层ArrayList(外层长度固定 内层长度不固定,比如LC347 Top K frequency elements): <code>List<Integer>[] array = new List[length];</code>//it’s like int[] but instead of int,we have List
此外 如何把这些二维数据的格式之间相互转化?Arrays.asList(), var.toArray()看起来没什么用处
random object: ‘Random rand = new Random();’
sort String array based on it length(small->larger):
Arrays.sort(words, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {return s1.length() - s2.length();}
});
sort 2d array based on the first column:
Arrays.sort(words, new Comparator<int[]>() {
public int compare(int[] interval1, int[] interval2) {return interval[0] - interval[1];}
}); //not sure if this gonna work
another way:
Arrays.sort(intervals, (a, b) -> (a[0]- b[0]));
Min heap and max Heap:<code>PriorityQueue<Integer> minheap = new PriorityQueue<>()</code> <code>PriorityQueue<Integer> maxheap = new PriorityQueue<>(Comparator.reverseOrder());</code> or maxheap can be initialize as :
<code>PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>( new Comparator<Integer>() { public int compare(Integer i1, Integer i2) { return i2.compareTo(i1); } } );</code>
convert hashmap key set to list: <code>List<String> res_files = new ArrayList<>(t.files.keySet())</code>
we want to sort a list of Node, and nodes have attribite of times and sentence. we want all those node sort in times first, if they are equal, then we sort them in sentence: <code>Collections.sort(list, (a, b) -> a.times == b.times? a.sentence.compareTo(b.sentence): b.times - a.times);</code>
initialize a hashset with values: <code>List<Character> list = Arrays.asList('a','e','i','o','u'); HashSet<Character> set = new HashSet<>(list);</code> or you can use Collections.addAll(), or you can initialize a set and add items one by one.
String to character array: s.toCharArray(), charArray to String: new String(char[] chars)
how to traverse hashset? just use for each, or use iterator.