注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
892. Surface Area of 3D Shapes
On a N * N grid, we place some 1 * 1 * 1 cubes. Each value v = grid[j] represents a tower of v cubes placed on top of grid cell (i, j). Return the total surface area of the resulting shapes.
- class Solution {
- public int surfaceArea(int[][] grid) {
- if (grid == null || grid.length == 0 || grid[0].length == 0) return 0;
- int R = grid.length;
- int C = grid[0].length;
- int area = 0;
- for (int r = 0; r < R; r++) {
- for (int c = 0; c < C; c++) {
- int h = grid[r][c];
- if (h > 0) {
- area += (h * 4 + 2);
- area -= overlapped(r, c, R, C, grid);
- }
- }
- }
- return area;
- }
-
- public int height(int r, int c, int R, int C, int[][] grid) {
- if (r >= 0 && r < R && c >= 0 && c < C) {
- return grid[r][c];
- } else {
- return 0;
- }
- }
-
- public int overlapped(int r, int c, int R, int C, int[][] grid) {
- int h = grid[r][c];
- return Math.min(h, height(r + 1, c, R, C, grid)) +
- Math.min(h, height(r -1, c, R, C, grid)) +
- Math.min(h, height(r, c + 1, R, C, grid)) +
- Math.min(h, height(r, c - 1, R, C, grid));
- }
- }
复制代码
893. Groups of Special-Equivalent Strings
You are given an array A of strings. Two strings S and T are [i]special-equivalent if after any number of moves, S == T. A move consists of choosing two indices i and j with i % 2 == j % 2, and swapping S with S[j]. Now, a [i]group of special-equivalent strings from A is a non-empty subset of A such that any string not in A is not special-equivalent with any string in A. Return the number of groups of special-equivalent strings from A. - import java.util.Arrays;
- class Solution {
- public int numSpecialEquivGroups(String[] A) {
- if (A == null || A.length == 0) return 0;
- Set<String> set = new HashSet<String>();
- for (String s: A) {
- set.add(min(s));
- }
- return set.size();
- }
-
- public String min(String s) {
- if (s == null) return null;
- if (s.length() == 0) return "";
- int len = s.length();
- char[] even = new char[(len +1) / 2];
- for (int i = 0; i < len; i += 2) even[i / 2] = s.charAt(i);
- char[] odd = new char[len / 2];
- for (int i = 1; i < len; i += 2) odd[i / 2] = s.charAt(i);
- Arrays.sort(even);
- Arrays.sort(odd);
- char[] result = new char[len];
- for (int i = 0; i < len; i += 2) result[i] = even[i / 2];
- for (int i = 1; i < len; i += 2) result[i] = odd[i / 2];
- return new String(result);
- }
- }
复制代码
894. All Possible Full Binary Trees A full binary tree is a binary tree where each node has exactly 0 or 2 children. Return a list of all possible full binary trees with N nodes. Each element of the answer is the root node of one possible tree. Each node of each tree in the answer must have node.val = 0. You may return the final list of trees in any order. - /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode(int x) { val = x; }
- * }
- */
- class Solution {
- static Map<Integer, List<TreeNode>> dic = new HashMap<>();
-
-
- static private void buildDic(int N) {
- List<TreeNode> one = new ArrayList<TreeNode>();
- one.add(newTreeNode());
- dic.put(1, one);
- for (int n = 1; n <= N; n += 2) {
- if (dic.containsKey(n)) continue;
- List<TreeNode> list = new ArrayList<>();
- for (int l = 1; l <= n - 2; l += 2) {
- int r = n - l - 1;
- // System.out.println("" + l + ", " + r);
- for (TreeNode left : dic.get(l)) {
- for (TreeNode right: dic.get(r)) {
- TreeNode root = newTreeNode();
- root.left = copy(left);
- root.right = copy(right);
- list.add(root);
- }
- }
- }
- dic.put(n, list);
- }
- }
-
- static private TreeNode newTreeNode() { return new TreeNode(0); }
-
- public List<TreeNode> allPossibleFBT(int N) {
- if (N <= 0 || N % 2 == 0) return new ArrayList<TreeNode>();
- buildDic(N);
- return dic.get(N);
- }
-
- static public TreeNode copy(TreeNode origin) {
- if (origin == null) return null;
- TreeNode copied = new TreeNode(origin.val);
- copied.left = copy(origin.left);
- copied.right = copy(origin.right);
- return copied;
- }
- }
复制代码
895. Maximum Frequency Stack Implement FreqStack, a class which simulates the operation of a stack-like data structure. FreqStack has two functions: - push(int x), which pushes an integer x onto the stack.
- pop(), which removes and returns the most frequent element in the stack.
- If there is a tie for most frequent element, the element closest to the top of the stack is removed and returned.
- class FreqStack {
-
- static class Node implements Comparable {
- int val;
- int freq;
- int order;
- public Node(int val, int freq, int order) {
- this.val = val;
- this.freq = freq;
- this.order = order;
- }
- @Override
- public int compareTo(Object o) {
- Node that = (Node) o;
- if (this.freq != that.freq) return that.freq - this.freq;
- return that.order - this.order;
- }
- }
-
- Map<Integer, Integer> freqs;
- PriorityQueue<Node> pq;
- int order = 0;
-
- public FreqStack() {
- freqs = new HashMap<>();
- pq = new PriorityQueue<>();
- }
-
- public void push(int x) {
- order += 1;
- int freq = freqs.getOrDefault(x, 0) + 1;
- freqs.put(x, freq);
- pq.offer(new Node(x, freq, order));
- }
-
- public int pop() {
- Node node = pq.poll();
- if (node.freq == 1) {
- freqs.remove(node.val);
- } else {
- freqs.put(node.val, node.freq - 1);
- }
- return node.val;
- }
- }
- /**
- * Your FreqStack object will be instantiated and called as such:
- * FreqStack obj = new FreqStack();
- * obj.push(x);
- * int param_2 = obj.pop();
- */
复制代码
|