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.