N people in a circle, count off, multiples of k go away, find the last one.
private static int removeNM(int n, int k) { LinkedList<Integer> ll = new LinkedList<>(); for (int i = 1; i <= n; i++) { ll.add(i); } int removed = -1; While (ll.size() > 1) {// remove = (removed + k) % ll.size(); Remove (removed--); } return ll.get(0); // Return the remaining elements}Copy the code
Jump n steps problem: one can jump 1-3 steps, find the total number of ways to jump.
There are two different choices at the time of the first jump, the recursive idea: one is to jump only 1 step for the first time, and the number of jumps is equal to the number of jumps for the remaining N-1 steps, namely f(n-1); Another option is to jump 2 steps for the first time, where the number of jumps is equal to the number of jumps for the remaining N-2 steps, i.e. F (n-2)… So the total number of different jumps at n steps f(n)= F (n-1)+ F (n-2) + F (n-3).
private static int jump(int n) { if (n == 1) { return 1; } else if (n == 2) { return 2; } else if (n == 3) { return 4; } else { return jump(n - 1) + jump(n - 2) + jump(n - 3); }}Copy the code
Similarities and differences of dynamic programming and greedy algorithms
Binary tree traversal, breadth, depth, front, middle, back order
import java.util.ArrayDeque; public class BinaryTree { static class TreeNode { int value; TreeNode left; TreeNode right; public TreeNode(int value) { this.value = value; } } TreeNode root; public BinaryTree(int[] array) { root = makeBinaryTreeByArray(array, 1); } public static TreeNode makeBinaryTreeByArray(int[] array, const TreeNode makeBinaryTreeByArray, const TreeNode makeBinaryTreeByArray, const TreeNode makeBinaryTreeByArray, const TreeNode makeBinaryTreeByArray int index) { if (index < array.length) { int value = array[index]; if (value ! = 0) { TreeNode t = new TreeNode(value); array[index] = 0; t.left = makeBinaryTreeByArray(array, index * 2); t.right = makeBinaryTreeByArray(array, index * 2 + 1); return t; } } return null; } /** * depth-first traversal, equivalent to root traversal * non-recursive implementation * auxiliary data structure required: Public void depthOrderTraversal() {if (root == null) {system.out.println ("empty tree"); return; } ArrayDeque<TreeNode> stack = new ArrayDeque<>(); stack.push(root); while (! stack.isEmpty()) { TreeNode node = stack.pop(); System.out.print(node.value + " "); if (node.right ! = null) { stack.push(node.right); } if (node.left ! = null) { stack.push(node.left); } } System.out.print("\n"); } /** * breadth-first traversal * non-recursive implementation * requires auxiliary data structures: Public void levelOrderTraversal() {if (root == null) {system.out.println ("empty tree"); return; } ArrayDeque<TreeNode> queue = new ArrayDeque<>(); queue.add(root); while (! queue.isEmpty()) { TreeNode node = queue.remove(); System.out.print(node.value + " "); if (node.left ! = null) { queue.add(node.left); } if (node.right ! = null) { queue.add(node.right); } } System.out.print("\n"); } /** * 13 * / \ * 65 5 * / \ \ * 97 25 37 * / /\ / * 22 4 28 32 */ public static void main(String[] args) { int[] arr = {0, 13, 65, 5, 97, 25, 0, 37, 22, 0, 4, 28, 0, 0, 32, 0}; BinaryTree tree = new BinaryTree(arr); tree.depthOrderTraversal(); tree.levelOrderTraversal(); }}Copy the code
If you feel that my words are helpful to you, welcome to pay attention to my public number:
My group welcomes everyone to come in and discuss all kinds of technical and non-technical topics. If you are interested, please add my wechat huannan88 and I will take you into our group.