543 Diameter of Binary Tree
687 Longest Univalue Path
863 All Nodes distance K in Binary Tree
Tree -> Undirected Graph BFS Graph find distance k
| if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPad){ | |
| if (@available(iOS 11, *)) { | |
| self.accountTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; | |
| } | |
| else { | |
| self.accountTableView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0); | |
| } | |
| } |
| // binary tree to undirected graph | |
| // treeToGraph(null, root); | |
| public void treeToGraph(TreeNode parent, TreeNode child) { | |
| if (parent != null) { | |
| List<TreeNode> pNeighbors = g.getOrDefault(parent, new ArrayList<TreeNode>()); | |
| pNeighbors.add(child); | |
| g.put(parent, pNeighbors); | |
| List<TreeNode> cNeighbors = g.getOrDefault(child, new ArrayList<TreeNode>()); | |
| cNeighbors.add(parent); |
| // 2 union find, better for checking connected components in dymanic graph | |
| public int findCircleNum(int[][] M) { | |
| int[] id = new int[M.length]; | |
| for (int i = 0; i < id.length; i++) id[i] = i; | |
| int[] count = new int[1]; | |
| count[0] = id.length; | |
| for (int i = 0; i < M.length; i++) { | |
| for (int j = i; j < M[0].length; j++) { | |
| if (M[i][j] == 1 && i != j) union(i, j, count, id); |
| // Lt547 friend circle | |
| // 1 dfs, better for checking connected components in static graph | |
| public int findCircleNum(int[][] M) { | |
| if (M.length == 0 || M[0].length == 0) return 0; | |
| int m = M.length; | |
| int n = M[0].length; | |
| boolean[] visited = new boolean[m]; | |
| int ans = 0; | |
| for (int i = 0; i < m; i++) { |
| // if the device is running Android 6.0 (API level 23) | |
| // and the app's targetSdkVersion is 23 or higher, | |
| //the following system behavior applies when your app requests a dangerous permission: | |
| // 1. | |
| //If the app doesn't currently have any permissions in the permission group, | |
| //the system shows the permission request dialog to the user describing the permission group | |
| //that the app wants access to. The dialog doesn't describe the specific permission within | |
| //that group. For example, if an app requests the READ_CONTACTS permission, | |
| //the system dialog just says the app needs access to the device's contacts. |
| /** | |
| * Code shared by String and StringBuffer to do searches. The | |
| * source is the character array being searched, and the target | |
| * is the string being searched for. | |
| * | |
| * @param source the characters being searched. | |
| * @param sourceOffset offset of the source string. | |
| * @param sourceCount count of the source string. | |
| * @param target the characters being searched for. | |
| * @param targetOffset offset of the target string. |
| // check permissions | |
| private void requestForGpsPermission() { | |
| if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { | |
| ActivityCompat.requestPermissions(this, | |
| new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, | |
| REQUEST_ACCESS_FINE_LOCATION); | |
| } | |
| } |
| // Delay 2 seconds | |
| DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2)) { | |
| // Your code with delay | |
| } |
| /* | |
| * Copyright (C) 2014 skyfish.jy@gmail.com | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software |