原题在此 Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists. Example 1: Input: l1 = [1,2,4], l2 = [1,3,4] Output: [1,1,2,3,4,4]
Example 2: Input: l1 = [], l2 = [] Output: []
Example 3: Input: l1 = [], l2 = [0] Output: [0]
Constraints:
The number of nodes in both lists is in the range [0, 50].
-100 <= Node.val <= 100
Both l1 and l2 are sorted in non-decreasing order.
原题在此 You are given an array of distinct integers arr and an array of integer arrays pieces, where the integers in pieces are distinct. Your goal is to form arr by concatenating the arrays in piecesin any order. However, you are not allowed to reorder the integers in each array pieces[i]. Return true if it is possible to form the array arr from pieces. Otherwise, return false.
classSolution { public: boolcanFormArray(vector<int>& arr, vector<vector<int>>& pieces){ // mp<"first number of a piece", "index of this piece in pieces"> vector<int> mp(101, -1); for(int i = 0; i < pieces.size(); ++i) mp[pieces[i][0]] = i; for(int i = 0; i < arr.size();) { int pindex = mp[arr[i]]; if(pindex == -1) returnfalse; // no piece start with this number; int j = 0; while(j < pieces[pindex].size()) if(arr[i++] != pieces[pindex][j++]) returnfalse; } returntrue; } };
题目链接 Given n non-negative integers representing the histogram’s bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].
The largest rectangle is shown in the shaded area, which has area = 10 unit. Example:
原题在此 According to Wikipedia’s article: “The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970.”
The board is made up of an m x n grid of cells, where each cell has an initial state: live (represented by a 1) or dead (represented by a 0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
Any live cell with fewer than two live neighbors dies as if caused by under-population. Any live cell with two or three live neighbors lives on to the next generation. Any live cell with more than three live neighbors dies, as if by over-population. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction. The next state is created by applying the above rules simultaneously to every cell in the current state, where births and deaths occur simultaneously. Given the current state of the m x n grid board, return the next state.
Could you solve it in-place? Remember that the board needs to be updated simultaneously: You cannot update some cells first and then use their updated values to update other cells.
In this question, we represent the board using a 2D array. In principle, the board is infinite, which would cause problems when the active area encroaches upon the border of the array (i.e., live cells reach the border). How would you address these problems?
原题在此 Given a binary tree where node values are digits from 1 to 9. A path in the binary tree is said to be pseudo-palindromic if at least one permutation of the node values in the path is a palindrome. Return the number of pseudo-palindromic paths going from the root node to leaf nodes. Example 1:
Input: root = [2,3,1,3,1,null,1] Output: 2 Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the red path [2,3,3], the green path [2,1,1], and the path [2,3,1]. Among these paths only red path and green path are pseudo-palindromic paths since the red path [2,3,3] can be rearranged in [3,2,3] (palindrome) and the green path [2,1,1] can be rearranged in [1,2,1] (palindrome). Example 2:
Input: root = [2,1,1,1,3,null,null,null,null,null,1] Output: 1 Explanation: The figure above represents the given binary tree. There are three paths going from the root node to leaf nodes: the green path [2,1,1], the path [2,1,3,1], and the path [2,1]. Among these paths only the green path is pseudo-palindromic since [2,1,1] can be rearranged in [1,2,1] (palindrome). Example 3:
Input: root = [9] Output: 1
Constraints: The given binary tree will have between 1 and 10^5 nodes. Node values are digits from 1 to 9.
inthammingWeight(uint32_t i){ i = (i & 0x55555555) + ((i >> 1) & 0x55555555); i = (i & 0x33333333) + ((i >> 2) & 0x33333333); i = (i + (i >> 4)) & 0x0f0f0f0f; i = (i + (i >> 8)) & 0x00ff00ff; i = i + (i >>16) & 0x0000ffff; return (int)i; } };
题目链接 You are standing at position 0 on an infinite number line. There is a goal at position target. On each move, you can either go left or right. During the n-th move (starting from 1), you take n steps. Return the minimum number of steps required to reach the destination.
Example 1:
Input: target = 3 Output: 2 Explanation: On the first move we step from 0 to 1. On the second step we step from 1 to 3.
Example 2:
Input: target = 2 Output: 3 Explanation: On the first move we step from 0 to 1. On the second move we step from 1 to -1. On the third move we step from -1 to 2. Note: target will be a non-zero integer in the range [-10^9, 10^9].
分析
target的正负不影响计算结果,因为数轴是对称的target = abs(target).
找到整数n,使得$(\sum_{i=1}^n i) \geq target$.
$有求和公式: target = \frac{n*(n+1)}{2}$.
$整形得到一元二次方程: n^2+n-2*target=0$.
$公式求解: n = \frac{(-1 + \sqrt{(1 + 8*target)})}{2}$.
n可能不为整,向上取整即可.
取$sum = \frac{n*(n+1)}{2}$, sum 与 target 的差 diff = sum - target
此时,如果 sum == target,直接返回n即可. 又或者差值diff是偶数,那么只要把$\frac{\text{diff}}{2}$取反即可得到target,所以也是n.
若差值diff为奇数,则有如下两种情况:
n为奇数,则n+1为偶数,需要$\sum_{i=1}^{n+2} i$ 来改变diff的奇偶.
n为偶数,则n+1为奇数,$\sum_{i=1}^{n+1} i$ 来改变diff的奇偶.
代码
1 2 3 4 5 6 7 8 9 10 11
classSolution { public: intreachNumber(int target){ longlong t = abs(target); longlong n = ceil((-1 + sqrt(1 + 8*t)) / 2); longlong sum = (n * (n + 1)) / 2; longlong diff = sum - t; if (sum == target || !(diff % 2)) return n; return n + ((n % 2) ? 2 : 1); } };