- 博客(225)
- 收藏
- 关注
原创 归并排序demo
public class Merge_sort{ public static void main(String[] args) { // TODO Auto-generated method stub int[] num = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; sort(num, 0, num.length - 1); for (int i = 0; i < num.length; i++) { System.out.println(num[i])
2021-09-14 11:32:26
297
原创 并查集问题
不带权并查集问题class Solution { public boolean equationsPossible(String[] equations) { int[] parent = new int[26]; for (int i = 0; i < 26; i++) { parent[i] = i; } for (String s : equations) { if (s
2021-07-04 17:31:12
394
原创 List 和 Array 之间的转换
List 转 Array方法一 :toArray()无参方法:返回一个Object[ ]ArrayList<Integer> list = new ArrayList<Integer>();list.add(1);list.add(2);list.add(3);Object[] arr = list.toArray();for (Object o : arr) { System.out.println(o);}方法二:toArray(T[ ] a)带泛型参数:
2021-07-03 15:23:44
1514
原创 Redis 学习
发展背景:1. 单机Mysql这个时候网站的访问量不是很大,单个数据库够用,服务器压力不大瓶颈:数据量太大,一个机器放不下数据的索引,一个机器的内存也放不下访问量(读写混合),一个服务器承受不了2. 缓存 + Mysql + 垂直拆分(读写分离)网站 80% 的情况都是在读操作,每次都去查询数据库的话效率很低,因此可以使用缓存来保证效率。3. 分库分表 + 水平拆分 + Mysql集群每个集群存储一部分数据4. 非关系型数据库目前一个互联网项目什么是 NoSql???
2021-05-28 23:23:08
208
1
原创 单调栈问题
暴力解法:(会超时)class Solution { public int largestRectangleArea(int[] heights) { if (heights == null || heights.length == 0) { return 0; } int len = heights.length; if (len == 1) { return heights[0]..
2021-05-26 19:38:43
148
原创 组合问题
1. 选取 k 个数回溯法:以选取的个数作为条件跳出class Solution { public static void backtracking(List<Integer> combineList, List<List<Integer>> combinations, int start, int k, int n) { if (k == 0) { combinations.add(new ArrayList<>(
2021-05-17 15:45:45
346
原创 剑指offer-3.26-49
暴力法:会超时class Solution { public int nthUglyNumber(int n) { int[] mark = new int[n]; int index = 0; for (int i = 1; ; i++) { int temp = i; while (temp != 1 && temp % 5 == 0) { temp.
2021-03-26 10:24:45
124
原创 剑指offer-3.25-48
class Solution { public int getIndex(char target, char[] c_s, int start, int end) { // 包含 start 和 end for (int i = end; i >= start; i--) { if (c_s[i] == target) { return i; } } return..
2021-03-25 16:55:56
119
原创 剑指offer-3.25-47
dp 数组:class Solution { public int maxValue(int[][] grid) { int m = grid.length, n = grid[0].length; int[][] dp= new int[m][n]; dp[0][0] = grid[0][0]; for (int i = 0; i < m; i++) { for (int j = 0; j < .
2021-03-25 15:37:41
145
原创 剑指offer-3.24-42
class Solution { public int maxSubArray(int[] nums) { int pre = nums[0], max = nums[0]; for (int i = 1; i < nums.length; i++) { pre = Math.max(pre + nums[i], nums[i]); max = Math.max(pre, max); } .
2021-03-24 13:06:12
143
原创 剑指offer-3.24-10.4-牛客
public class Solution { public int jumpFloorII(int target) { int[] dp = new int[target + 1]; dp[0] = 1; dp[1] = 1; for (int i = 2; i <= target; i++) { for (int j = 0; j < i; j++) { dp[i.
2021-03-24 11:07:42
151
原创 剑指offer-3.24-10.2-牛客
public class Solution { public int rectCover(int target) { if (target == 0 || target == 1 || target == 2) { return target; } int a = 1, b = 2, c = 0; for (int i = 3; i <= target; i++) { c = a..
2021-03-24 10:58:23
182
原创 剑指offer-3.24-10.2
class Solution { public int numWays(int n) { if (n == 0 || n == 1) { return 1; } int[] dp = new int[n + 1]; dp[1] = 1; dp[2] = 2; for (int i = 3; i <= n; i++) { dp[i] = (dp[i ..
2021-03-24 10:09:44
138
原创 剑指offer-3.24-10.1
class Solution { public int fib(int n) { int a = 0, b = 1, sum = 0; for (int i = 0; i < n; i++) { sum = (a + b) % 1000000007; a = b; b = sum; } return a; }}
2021-03-24 09:57:51
143
原创 剑指offer-3.22-51
暴力法:(超时)class Solution { public int reversePairs(int[] nums) { int sum = 0; for (int i = 0; i < nums.length - 1; i++) { for (int j = i + 1; j < nums.length; j++) { if (nums[i] > nums[j]) { .
2021-03-22 14:25:32
127
原创 剑指offer-3.20-45
选择排序:class Solution { public void swap(String[] str, int i, int j) { String temp = str[i]; str[i] = str[j]; str[j] = temp; } public String minNumber(int[] nums) { int len = nums.length; String[] str = n.
2021-03-20 17:54:57
122
原创 剑指offer-3.20-21
class Solution { public void swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } public int[] exchange(int[] nums) { int i = 0, j = nums.length - 1; while (i < j).
2021-03-20 16:50:42
136
原创 剑指offer-3.19-38
容易想的方法:class Solution { public void dfs (char[] c_str, int[] used, int depth, int len, StringBuffer sb, List<String> res) { if (depth == len) { String temp = new String(sb); if (!res.contains(temp)) { .
2021-03-19 15:25:16
261
原创 剑指offer-3.18-13
深度优先DFS:class Solution { private int cnt = 0; public int byteSum (int i, int j) { int sum = 0; while (i != 0) { sum += i %10; i /= 10; } while (j != 0) { sum += j % 10; .
2021-03-18 10:38:10
130
原创 剑指offer-3.17-12
class Solution { public boolean dfs(char[][] board, int i, int j, char[] words, int k) { if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || board[i][j] != words[k]) { return false; } if..
2021-03-17 11:04:52
103
原创 剑指offer-3.16-16
class Solution { public double myPow(double x, int n) { double res = 1.0; long b = n; if (x == 0) { return 0; } if (b < 0) { x = 1 / x; b = -b; } while (b ..
2021-03-16 15:02:20
110
原创 Java控制输出小数点后几位数
import java.text.DecimalFormat;public class test { public static void main(String[] args) { DecimalFormat df = new DecimalFormat("#.000");// float a = 12.3456789f; float 也可以 double a = 12.3456789; System.out.println(df.format(a)); // 输出的是
2021-03-16 14:41:58
3702
原创 剑指offer-3.16-53.2
class Solution { public int missingNumber(int[] nums) { int i = 0, j = nums.length - 1; while (i <= j) { int m = (i + j) / 2; if (nums[m] == m) { i = m + 1; } else{ .
2021-03-16 14:22:42
87
原创 剑指offer-3.15-53.1
二分:class Solution { public int search(int[] nums, int target) { int i = 0, j = nums.length - 1; while (i <= j) { int m = (i + j) / 2; if (nums[m] <= target) { i = m + 1; } else.
2021-03-15 09:58:10
107
原创 剑指offer-2.12-11
class Solution { public int minArray(int[] numbers) { for (int i = 0; i < numbers.length - 1; i++) { if (numbers[i] > numbers[i + 1]) { return numbers[i + 1]; } } return numbers[0];.
2021-03-12 09:29:38
104
原创 剑指offer-3.11-63
class Solution { public int maxProfit(int[] prices) { int maxProfit = 0; for (int i = 0; i < prices.length - 1; i++) { for (int j = i + 1; j < prices.length; j++) { if (prices[j] - prices[i] > maxP..
2021-03-11 14:49:55
105
原创 剑指offer-3.10-14.1
class Solution { public int cuttingRope(int n) { if (n <= 3) { return n - 1; } int a = n / 3, b = n % 3; if (b == 0) { return (int)Math.pow(3, a); } if (b == 1) { .
2021-03-10 10:00:33
97
原创 剑指offer-3.9-68.2
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNo..
2021-03-09 15:33:30
98
原创 剑指offer-3.9-68.1
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNo..
2021-03-09 11:26:03
112
原创 剑指offer-3.9-55.2
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public int recur(TreeNode root) { if (root == null..
2021-03-09 10:32:54
125
原创 剑指offer-3.8-55.1
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { private int depth = 0, maxDepth = 0; public void dfs(Tr.
2021-03-08 13:23:19
106
原创 剑指offer-3.8-54
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { private int res, k; public void dfs(TreeNode root) { ..
2021-03-08 11:08:08
91
原创 剑指offer-3.8-37
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Codec { // Encodes a tree to a single string. public Strin.
2021-03-08 10:22:14
104
原创 剑指offer-3.5-36
/*// Definition for a Node.class Node { public int val; public Node left; public Node right; public Node() {} public Node(int _val) { val = _val; } public Node(int _val,Node _left,Node _right) { val = _val..
2021-03-05 10:09:01
200
1
原创 剑指offer-3.4-34
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public void backtracking(TreeNode node, int target, ArrayL..
2021-03-04 10:26:59
198
1
原创 剑指offer-3.4-33
class Solution { public boolean verify(int[] postorder, int start, int end) { if (start >= end) { return true; } int i = start; while (postorder[i] < postorder[end]) { i++; } ..
2021-03-04 10:05:38
168
1
原创 剑指offer-3.3-32.3
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public List<List<Integer>> levelOrder(TreeNode..
2021-03-03 14:54:24
241
2
原创 剑指offer-3.3-32.2
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public List<List<Integer>> levelOrder(TreeNode..
2021-03-03 11:36:13
128
原创 剑指offer-3.3-32.1
// return res.stream().mapToInt(Integer::valueOf).toArray();/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Sol...
2021-03-03 10:24:41
104
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人