Parse HTML HTML document parsed and the DOM tree as we know it has been built.
Recalculate Styles All kind of stylesheets are parsed. Render tree is built. Render tree contains DOM tree nodes that are going to be displayed. Thus, head tag is excluded as well as style and script. Elements with CSS display property set to none are also ignored. On the other hand, pseudo elements, such as :after and :before, are included. Moreover, every text line becomes a single block.
Layout (also called reflow) A collection of blocks generated from the render tree. All the block dimensions, that are dependent on each other, are calculated.
Paint Rasterization of the blocks and texts. Images decoding and resize if necessary also happen here.
Layer Composition Composition of the visual layers that can be processed and painted independently.
/** * @param {number[]} nums * @return {number} */ var singleNumber = function (nums) { var result = nums[0]; for (var i = 1; i < nums.length; i++) { result ^= nums[i]; } return result; };
var a = "RTGDAFT", b = "GDA"; var start_a = 0, end_a = a.length - 1; var start_b = 0, end_b = b.length - 1; var match = false; while (start_a < end_a) { if (a[start_a] == b[start_b]) { // match = a[start_a]; match = true; while (start_b < end_b) { start_a++; start_b++; if (a[start_a] != b[start_b]) { match = false; break; } // match = match + a[start_a]; } if (match) break; } if (a[end_a] == b[end_b]) { // match = a[end_a]; match = true; while (start_b < end_b) { end_a--; end_b--; if (a[end_a] != b[end_b]) { match = false; break; } // match = a[end_a] + match; } if (match) break; } start_a++; end_a--; start_b = 0; end_b = b.length - 1; }
Time complexity: O(n / 2 * m), Space complexity: O(0)
/** * @param {string} s * @return {number[]} */ var partitionLabels = function (s) { let result = []; let temp = {}; for (let i = 0; i < s.length; i++) { const char = s[i]; if (!temp[char]) { temp[char] = { start: i, end: i }; } if (i > temp[char].end) { temp[char].end = i; } } let max = -1; let start = 0; for (let i in temp) { if (temp[i].start > max && max !== -1) { result.push(max - start + 1); start = temp[i].start; } max = Math.max(max, temp[i].end); } result.push(max - start + 1); return result; };
Time complexity: O(n + n), Space complexity: O(n)
LeetCode - 22. Generate Parentheses
my answer : 使用遞迴去組成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/** * @param {number} n * @return {string[]} */ var generateParenthesis = function (n) { let result = []; constdp = (str, openNum, closeNum) => { if (openNum < closeNum) return; if (openNum == n) { for (let i = str.length; i < n * 2; i++) str += ")"; result.push(str); return str; } dp(str + "(", openNum + 1, closeNum); dp(str + ")", openNum, closeNum + 1); }; dp("(", 1, 0); return result; };
// Iterate through each element till end of string // 1. find char (isNaN) to store to parameter // 2. couter 1 to this char // 3. if the next char is number, store to prevValue and combine to string; // 4. if loop to next char, store previous value to previous element // 5. loop to end functionstringToObject(input) { let temp = {}; let prev = ""; let val = ""; for (let i = 0; i < input.length; i++) { const curr = input[i]; if (isNaN(curr)) { //is char temp[curr] = temp[curr] ? Number(temp[curr]) + 1 : 1; prev = curr; val = ""; } else { //is number if (isNaN(val)) { val = curr; } else { val += curr; } temp[prev] = Number(val); } } return temp; }