随笔分类 - 算法
摘要:38.外观数列 public String countAndSay(int n) { if(n < 1) { return ""; } if(n == 1) { return "1"; } char[] pre = countAndSay(n - 1).toCharArray(); int time
阅读全文
摘要:33.搜索旋转排序数组 public int search(int[] nums, int target) { if(nums == null || nums.length == 0) { return -1; } int left = 0; int mid = 0; int right = num
阅读全文
摘要:22.括号生成 public List<String> generateParenthesis(int n) { char path[] = new char[n * 2]; List<String> res = new ArrayList<>(); if(n == 0) { return res;
阅读全文
摘要:14.最长公共前缀 public String longestCommonPrefix(String[] strs) { String first = strs[0]; char f[] = first.toCharArray(); int res = first.length(); for(int
阅读全文
摘要:7.整数反转 public int reverse(int x) { boolean isFu = x < 0 ? true : false; x = isFu ? x : -x; int res = 0; int M = Integer.MIN_VALUE / 10; int N = Int
阅读全文
摘要:1.两数之和 public int[] twoSum(int[] nums, int target) { HashMap<Integer,Integer> hm = new HashMap<>(); int res[] = new int[2]; for(int i = 0;i < nums.len
阅读全文
摘要:1.机器人问题,一共有1-N N个位置,机器人可以在1-N上任意移动,初始位置为start,目标位置为aim,可以移动K次,求有几种移动方式 (1)递归实现 public int ways3(int N,int start,int aim,int K) { return process3(start
阅读全文
摘要:滑动窗口最大值 public int[] maxSlidingWindow(int[] nums, int k) { if(nums == null || k < 1 || nums.length < k) { return new int[0]; } LinkedList<Integer> lis
阅读全文
摘要:最长公共子序列 class Solution { public int longestCommonSubsequence(String text1, String text2) { char s1[] = text1.toCharArray(); char s2[] = text2.toCharAr
阅读全文
摘要:省份数量 class Solution { public int findCircleNum(int[][] isConnected) { int N = isConnected.length; Unionfind unionfind = new Unionfind(N); for(int i =
阅读全文
摘要:折绳子问题(一根绳子,自下向上折叠n次,依次打印展开之后折痕的凹凸) public void process(int i,int n,boolean flag) { // i : 当前层数,n : 最多层数 if(i > n) { return; } process(i + 1,n,true); S
阅读全文
摘要:1.堆排序 public class HeapSort { public void heapSort(int arr[]){ for(int i = arr.length - 1;i >= 0;i --) { heapify(arr,i,arr.length); } int heapSize = a
阅读全文
摘要:1.leetCode 327题 给你一个整数数组 nums 以及两个整数 lower 和 upper 。求数组中,值位于范围 [lower, upper] (包含 lower 和 upper)之内的 区间和的个数 。 区间和 S(i, j) 表示在 nums 中,位置从 i 到 j 的元素之和,包含
阅读全文
摘要:1.归并排序(非递归) public void mergeSort(int arr[]) { int size = arr.length; int mergeSize = 1; while(mergeSize < size) { int L = 0; while(L < size) { int M
阅读全文
摘要:1.用数组实现栈和队列 (1)栈 int arr[] = new int[5]; int index = -1; public void push(int val) { if(index == 5) { throw new RuntimeException(""); }else { arr[++ i
阅读全文
摘要:1.选择排序 public void selectSort(int arr[]) { if(arr == null || arr.length < 2){ return; } for(int i = 0;i < arr.length - 1;i ++) { int minIndex = i; for
阅读全文
摘要:把数组排成最小的数: public String PrintMinNumber(int [] numbers) { String str[] = new String[numbers.length]; for(int i = 0;i < numbers.length;i ++){ //先将int数组
阅读全文
摘要:数组中出现次数超过一半的数字:(摩尔投票法) public int MoreThanHalfNum_Solution(int [] array) { int size = array.length; int count = 0; int res = array[0]; for(int i = 0;i
阅读全文
摘要:斐波那契数列: public int Fibonacci(int n) { if(n <= 1){ return n; } int dp1 = 0; int dp2 = 1; int res = 0; //最后的返回值 for(int i = 2;i <= n;i ++){ res = dp1 +
阅读全文

浙公网安备 33010602011771号