1. Find the 3-bit even number
Give you an integer array digits, where each element is a number (0 - 9). There may be duplicate elements in the array. You need to find all integers that meet the following conditions and are different from each other:
The integer consists of three elements in digits connected in any order.
The integer does not contain leading zeros
The integer is an even number
For example, given digits is [1, 2, 3], integers 132 and 312 satisfy all the conditions listed above. All found different integers are arranged in ascending order and returned as an array.
Input: digits = [2,1,3,0]
Output: [102120130132210230302312320]
code:
Java
class Solution { public int[] findEvenNumbers(int[] d) { int n = d.length; TreeSet<Integer> h = new TreeSet<>(); for (int i = 0; i < n; i++) if (d[i] > 0) for (int j = 0; j < n; j++) if (j != i) for (int k = 0; k < n; k++) if (k != i && k != j) { int x = d[i] * 100 + d[j] * 10 + d[k]; if (x % 2 == 0) h.add(x); } int[] ans = new int[h.size()]; int p = 0; for (int x : h) ans[p++] = x; return ans; } }
C++
class Solution { public: vector<int> findEvenNumbers(vector<int>& a) { int n = a.size(); set<int> s; for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) for (int k = 0; k < n; ++k) { if (i == j || j == k || i == k || a[i] == 0 || a[k] % 2) continue; s.insert(a[i] * 100 + a[j] * 10 + a[k]); } vector<int> ans; for (auto x : s) ans.push_back(x); return ans; } };
2. Delete the intermediate node of the linked list
Give you a head node of the linked list. Delete the intermediate node of the linked list and return the head node of the modified linked list. The intermediate node of the linked list with length n is the n/2 node from the beginning (subscript starts from 0).
For n=1, 2, 3, 4 and 5, the subscripts of the intermediate node are 0, 1, 1, 2 and 2 respectively.
Input: 1 - > 3 - > 4 - > 7 - > 1 - > 2 - > 6
Output: 1 - > 3 - > 4 - > 1 - > 2 - > 6
Input: 1 - > 2 - > 3 - > 4
Output: 1 - > 2 - > 4
Input: 2 - > 1
Output: 2
Code implementation:
java
class Solution { public ListNode deleteMiddle(ListNode head) { if (head.next == null) { return null; } ListNode p = head; ListNode q = head; ListNode prev = null; while (p != null && p.next != null) { prev = q; q = q.next; p = p.next.next; } prev.next = prev.next.next; q.next = null; return head; } }
C++
class Solution { public: ListNode* deleteMiddle(ListNode* head) { vector<int> a; for (auto p = head; p; p = p->next) a.push_back(p->val); auto ans = new ListNode(0); auto cur = ans; int n = a.size(); for (int i = 0; i < n; ++i) { if (i == n / 2) continue; cur->next = new ListNode(a[i]); cur = cur->next; } return ans->next; } };
3. The direction of each step from one node to another in a binary tree
Give you the root node of a binary tree, root. This binary tree has a total of n nodes. The value of each node is an integer from 1 to n and is different from each other. Give you an integer startValue, which represents the value of the starting node s, and a different integer destValue, which represents the value of the ending node t.
Find the shortest path from node s to node t and return the direction of each step as a string. Each step is represented by capital letters' L ',' R 'and' U ':
'L' means going from a node to its left child node.
'R' means going from a node to its right child node.
'U' means going from a node to its parent node.
Please return to the direction of each step of the shortest path from s to t.
Example:
Input: root = [5,1,2,3,null,6,4], startValue = 3, destValue = 6
Output: "UURL"
Explanation: the shortest path is: 3 → 1 → 5 → 2 → 6.
code
Java
class Solution { public String getDirections(TreeNode root, int startValue, int destValue) { List<Node> startList = new ArrayList<>(); List<Node> destList = new ArrayList<>(); List<Node> tmp = new ArrayList<>(); tmp.add(new Node(root.val, 'X')); dfs(startList, tmp, root, startValue); dfs(destList, tmp, root, destValue); StringBuilder sb = new StringBuilder(); int n = startList.size(); int m = destList.size(); int min = Math.min(n, m); int i = 0; for ( ; i < min; i++) { if (startList.get(i).val == destList.get(i).val) { continue; } break; } for (int j = i; j < n; j++) { sb.append('U'); } for (int j = i; j < m; j++) { sb.append(destList.get(j).dir); } return sb.toString(); } private void dfs(List<Node> list, List<Node> tmp, TreeNode cur, int target) { if (cur.val == target) { list.addAll(tmp); return; } if (cur.left != null) { tmp.add(new Node(cur.left.val, 'L')); dfs(list, tmp, cur.left, target); tmp.remove(tmp.size() - 1); } if (cur.right != null) { tmp.add(new Node(cur.right.val, 'R')); dfs(list, tmp, cur.right, target); tmp.remove(tmp.size() - 1); } } private class Node { int val; char dir; public Node(int val, char dir) { this.val = val; this.dir = dir; } } }
C++
class Solution { public: string getDirections(TreeNode* root, int startValue, int destValue) { string cur; string r[2]; function<void(TreeNode*, int, int)> dfs = [&](TreeNode* root, int target, int idx) { if(root) { if(root->val == target) { r[idx] = cur; } else { cur.push_back('L'); dfs(root->left, target, idx); cur.pop_back(); cur.push_back('R'); dfs(root->right, target, idx); cur.pop_back(); } } }; dfs(root, startValue, 0); dfs(root, destValue, 1); int p = 0; while(p < r[0].size() && p < r[1].size() && r[0][p] == r[1][p]) ++p; r[0].erase(r[0].begin(), r[0].begin() + p); r[1].erase(r[1].begin(), r[1].begin() + p); for(char& c : r[0]) { c = 'U'; } return r[0] + r[1]; } };
Forget the fourth question, No