笔试模板
文章平均质量分 64
笔试题-算法模板
星空是梦想
广州程序员一枚~
展开
-
求数组中k个数的所有组合
/** * 求数组中 k个元素序列 的所有组合 * @param start * @param array * @param length * @param k * @param list * @param used * @param res */ public static void dfs(int start, int[] array, int length, int k, List<Integer> list, boolean[] .原创 2020-09-27 21:22:24 · 1417 阅读 · 0 评论 -
字符串替换
import java.util.Scanner;public class Main { public static void main(String[] args) { // 模式串中‘?’可以匹配目标串中的任何字符,模式串中的 ’*’可以匹配目标串中的任何长度的串 Scanner scanner = new Scanner(System.in); String pattern = scanner.next(); String input = scanner.next(); pa原创 2020-09-05 23:19:01 · 626 阅读 · 0 评论 -
BigDecimal 计算余数
Java计算大数据,使用BigDecimal,它提供了很多方法。通过BigDecimal取余:// 返回数组中,第一个元素是商,第二个元素是余数public BigDecimal[] divideAndRemainder(BigDecimal divisor);原创 2020-09-04 10:30:31 · 2317 阅读 · 0 评论 -
优先级队列——实现二维数组排序
PriorityQueue<Customer> queue = new PriorityQueue<>();class Customer implements Comparable<Customer> { public int num; public int fee; public Customer(int num, int fee) { this.num = num; this.fee = fee;原创 2020-09-03 21:52:06 · 873 阅读 · 0 评论 -
岛屿数量
class Solution { // 计算岛屿数量 public int numIslands(char[][] grid) { int islandNum = 0; for(int i = 0; i < grid.length; i++){ for(int j = 0; j < grid[0].length; j++){ if(grid[i][j] == '1'){原创 2020-09-03 09:13:32 · 152 阅读 · 0 评论 -
全排列
package 全排列;import java.util.*;/** * 回溯+深度优先遍历 */class Solution { public static void main(String[] args) { // int数组全排列调用 List<List<Integer>> result = permute(new int[]{1,2,3}); System.out.println(result);原创 2020-09-03 08:55:56 · 171 阅读 · 0 评论 -
背包问题——动态规划
package pack9jiang; import java.util.Scanner; /** * 使用二维数组非递归的方法求解0/1背包问题 */public class ZeroOnePack { // N表示物体的个数,V表示背包的载重 int N,V; //用于存储每个物体的重量,下标从1开始 private int[] weight; //存储每个物体的收益,下标从1开始 private int[] value; //二维数原创 2020-09-02 16:50:11 · 258 阅读 · 0 评论 -
螺旋打印二维数组
import java.util.ArrayList;import java.util.List;public class 螺旋打印二维数组 { public static List<Integer> spiralOrder(int[][] matrix) { if (matrix.length == 0) return new ArrayList<>(); if (matrix[0].length == 0) return new ArrayList<>原创 2020-09-02 10:06:59 · 440 阅读 · 0 评论 -
最大公因数和最小公倍数
import java.util.Scanner;public class 最大公因数最小公倍数 { // 最大公约数 public static int get_gcd(int n1, int n2) { int gcd = 0; if (n1 < n2) {// 交换n1、n2的值 n1 = n1 + n2; n2 = n1 - n2; n1 = n1 - n2; } if (n1 % n2 == 0) { gcd = n2; } whi原创 2020-09-02 10:06:36 · 235 阅读 · 0 评论 -
折半查找
public class 折半查找 { public static void main(String[] args) { 折半查找 su = new 折半查找(); int[] arr = {1,3,6,9,12,23,33,44,45,67,78,98,100,111,123,1234,2345,9999,11111,12345,666666,900000}; int res = su.binarySearch(arr, 78); S原创 2020-09-02 10:02:42 · 159 阅读 · 0 评论 -
快排
public class 快排 { /* * 快速排序 * * 参数说明: * a -- 待排序的数组 * start -- 数组的左边界(例如,从起始位置开始排序,则start=0) * end -- 数组的右边界(例如,排序截至到数组末尾,则end=a.length-1) */ private void quick_sort(int a[], int start, int end) { if (start < end) { int原创 2020-09-02 10:01:59 · 125 阅读 · 0 评论 -
归并排序
public class 归并排序 { public static void main(String[] args) { int[] arr = new int[]{3,2,5,1,6,7,8,0,20}; sort(arr, 0, 8); System.out.println(arr); } public static int[] sort(int[] a,int low,int high){ int mid = (low+high)/2; if(low<high){原创 2020-09-02 10:01:24 · 92 阅读 · 0 评论 -
子集
import java.util.LinkedList;import java.util.List;import java.util.Scanner;public class 子集 { // 返回子集的某个元素 public static void sort(int n,int s,String str, List<String> result) { StringBuilder builder = new StringBuilder(); for(int i=0;i<n原创 2020-09-02 10:00:56 · 133 阅读 · 0 评论 -
回溯
import java.util.ArrayList;import java.util.List;public class 回溯 { /** * 回溯算法模板 * * @param start 开始回溯的位置 * @param target 目标值 * @param candidates 待回溯数组 * @param result 结果集 * @param list 结果集子集 */ private void backTrack(int start, int tar原创 2020-09-02 10:00:23 · 120 阅读 · 0 评论 -
二叉树前中后序遍历
public class 二叉树前中后序遍历 { public static void main(String[] args) { } // 前序遍历 public void preOrderTraverse1(TreeNode2 root) { if (root != null) { System.out.print(root.val + "->"); preOrderTraverse1(root.left); preOrderTraverse1(root.righ原创 2020-09-02 09:59:54 · 146 阅读 · 0 评论 -
KMP字符串匹配
参考文章:https://www.cnblogs.com/Yintianhao/p/9996524.htmlpublic class KMP字符串匹配算法 { /** * 求出一个字符数组的next数组 * @param t 字符数组 * @return next数组 */ public static int[] getNextArray(char[] t) { int[] next = new int[t.length]; next[0] = -1; next[1] =原创 2020-09-02 09:58:21 · 130 阅读 · 0 评论