![](https://img-blog.csdnimg.cn/20201026233743427.jpg?x-oss-process=image/resize,m_fixed,h_224,w_224)
算法
主要为蓝桥杯和天梯赛为主的比赛进行的一些算法练习demo
Sky-木落
程序猿!热爱编程、旅行、摄影、轻音乐等、
展开
-
快速排序——Java
package com.muluo.test1007;import java.util.Arrays;public class QuickSort { public static void main(String[] args) { int[] arr = {5, 1, 9, 6, 7, 2, 4}; quickSort(arr, 0, arr.length - 1); System.out.println(Arrays.toString(arr));; } private st原创 2021-10-07 19:50:22 · 100 阅读 · 0 评论 -
5. 最长回文子串
1、暴力解法package leetcode;class Solution { public static void main(String[] args) { Solution solution = new Solution(); String str = "cbbd"; System.out.println(solution.longestPalindrome(str)); } public String longestPa原创 2021-10-04 17:32:23 · 85 阅读 · 0 评论 -
双向链表的实现(LinkedList)
package com.muluo.test1003;public class MyLinkedList { public static void main(String[] args) { MyLinkedList myLinkedList = new MyLinkedList(); myLinkedList.addLast(1); myLinkedList.addLast(5); myLinkedList.addLast(2); myLinkedList.addLast(8);原创 2021-10-04 13:17:32 · 172 阅读 · 0 评论 -
树的建立——Java
package com.muluo.tree;public class TreeNode { int value; TreeNode leftNode; TreeNode rightNode; public TreeNode(int value) { this.value = value; } public void setLeftNode(TreeNode leftNode) { this.leftNode = leftNode; } public void setRig原创 2021-10-04 12:34:29 · 1408 阅读 · 0 评论 -
归并排序——链表
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * }原创 2021-09-20 17:23:35 · 112 阅读 · 0 评论 -
冒泡排序—— 链表
package leetcode;class Solution { static class ListNode { int val; ListNode next; ListNode() { } ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) {原创 2021-09-20 16:13:45 · 262 阅读 · 0 评论 -
第 5 天 双指针
876. 链表的中间结点第一种方法快慢指针,当快的指针到最后一个元素时,那个慢的刚好到中间;第二种方法是直接遍历一遍,确定元素总个数,然后确定出中间元素/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } *原创 2021-08-09 21:19:45 · 53 阅读 · 0 评论 -
第 4 天 双指针
344. 反转字符串分别定义一个左指针,和右指针,while循环退出的条件是,当左右指针相遇时即可退出while循环class Solution { public void reverseString(char[] s) { int i = 0, j = s.length - 1; while (i < j) { char t = s[i]; s[i] = s[j]; s[j] =原创 2021-08-08 21:17:19 · 60 阅读 · 0 评论 -
第 3 天 双指针
未优化代码283. 移动零class Solution { public void moveZeroes(int[] nums) { for (int i = 0; i < nums.length; i++) { for (int j = i + 1; j < nums.length ; j++) { if (nums[i] == 0 && nums[j] != 0) { .原创 2021-08-03 19:37:44 · 55 阅读 · 0 评论 -
第 2 天 双指针
思想,直接平方后进行比较,然后另开辟一个数据,进行倒序存储数组最大元素class Solution { public int[] sortedSquares(int[] nums) { // for (int i = 0; i < nums.length; i++) { // nums[i] = nums[i] * nums[i]; // } // Arrays.sort(nums); // ret.原创 2021-08-02 22:17:22 · 46 阅读 · 0 评论 -
1337. 矩阵中战斗力最弱的 K 行
此题要求实际意思就是求一个数组中每一行的战力总和,如果,有两行相同,在后面出现的较强,由于题目,需要我们返回原数组小标,我们则可以用凑整法,来比较每一行,并且把下标加上,最后对数组的每个元素取余即可求出前k个下标class Solution { public int[] kWeakestRows(int[][] mat, int k) { // 首先统计出每一行的一的个数 // 如果出现重复行,则,后面出现的加下标(前面也加) // 进行排序.原创 2021-08-01 20:39:30 · 120 阅读 · 0 评论 -
第 1 天 二分查找
二分思想总结:确保题目是有序排列(升序,降序)都可以;二分可以解决的问题有,查找目标元素,寻找插入位置704. 二分查找class Solution { public int search(int[] nums, int target) { int l = 0, r = nums.length - 1; while (l <= r ) { int mid = l + (r - l) / 2; if (原创 2021-08-01 14:07:33 · 78 阅读 · 0 评论 -
83. 删除排序链表中的重复元素
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * }原创 2021-07-30 20:58:43 · 64 阅读 · 0 评论 -
71. Excel 表列序号
package leetcode;class Solution { public static void main(String[] args) { Solution solution = new Solution(); System.out.println(solution.titleToNumber("AB")); } public int titleToNumber(String columnTitle) { int s原创 2021-07-30 20:47:54 · 81 阅读 · 0 评论 -
67. 二进制求和
class Solution { public String addBinary(String a, String b) { StringBuffer sb = new StringBuffer(); // 使用Math函数中的Max方法求得最大长度 int n = Math.max(a.length(), b.length()), carry = 0; for (int i = 0; i < n; i++) {原创 2021-07-29 22:02:23 · 64 阅读 · 0 评论 -
66. 加一
class Solution { public int[] plusOne(int[] digits) { int[] arr = new int[digits.length + 1]; for (int i = digits.length - 1; i >= 0; i--) { // 最后一位不是9就digits[i]++;推出就行,否则的话要将末尾写为0,重新判断 if (digits[i] != 9) {原创 2021-07-29 20:44:56 · 56 阅读 · 0 评论 -
671. 二叉树中第二小的节点
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { *原创 2021-07-27 20:18:39 · 51 阅读 · 0 评论 -
1893. 检查是否区域内所有整数都被覆盖
class Solution { public boolean isCovered(int[][] ranges, int left, int right) { for (int i = left; i <= right; i++) { boolean ok = false; for (int[] range : ranges) { int a = range[0], b = range[1];原创 2021-07-23 21:09:03 · 43 阅读 · 0 评论 -
剑指 Offer 39. 数组中出现次数超过一半的数字
class Solution { public int majorityElement(int[] nums) { int res = nums[0], count = 0; for (int i = 0; i < nums.length; i++) { if (res == nums[i]) { count++; } else { count--;原创 2021-04-03 15:20:20 · 51 阅读 · 0 评论 -
堆排序(heapsort)
package leetcode;class Solution { public static void main(String[] args) { int tree[] = {2, 5, 3, 1, 10, 4}; int n = 6;// bulid_heap(tree, n); heap_sort(tree, n); for (int i = 0; i < n; i++) { Sy原创 2021-04-02 20:47:39 · 62 阅读 · 0 评论 -
冒泡排序——三种方案
第一版本第二版本(判断是否有交换过)第三版本(记录最后的位置)原创 2021-04-01 20:44:05 · 47 阅读 · 0 评论 -
剑指 Offer 62. 圆圈中最后剩下的数字
一、方法一 (公式)class Solution { public int lastRemaining(int n, int m) { int ans = 0; // 最后一轮剩下2个人,所以从2开始反推 for (int i = 2; i <= n; i++) { ans = (ans + m) % i; } return ans; }}二、方法二(递归和一个不知道的什么方原创 2021-04-01 19:48:38 · 53 阅读 · 0 评论 -
剑指 Offer 57 - II. 和为s的连续正数序列
class Solution { public static int[][] findContinuousSequence(int target) { List<int[]> list = new ArrayList<>(); int sum = 0, limit = target - 1; for (int i = 1; i < limit; i++) { for (int j = i; ;原创 2021-04-01 19:09:48 · 55 阅读 · 0 评论 -
最长回文子串
public int getLongestPalindrome(String A, int n) { // 第 i 个字符到第 j 个字符是否是回文串 boolean[][] dp = new boolean[n][n]; int max = 0; // 字符串首尾字母长度差 (d = j-i) for (int d = 0; d < n; d++) { // 字符串起始位置 i for (int i = 0原创 2021-03-31 20:16:35 · 56 阅读 · 0 评论 -
322. 零钱兑换
class Solution { public int coinChange(int[] coins, int amount) { int[] dp = new int[amount + 1]; Arrays.fill(dp, Integer.MAX_VALUE); dp[0] = 0; for (int currentAmount = 1; currentAmount <= amount; currentAmount++) {原创 2021-03-31 16:07:35 · 62 阅读 · 0 评论 -
剑指 Offer 53 - I. 在排序数组中查找数字 I
class Solution { public int search(int[] nums, int target) { int left = 0, right = nums.length - 1, count = 0, mid = 0; while (left < right) { mid = (left + right) / 2 ; if (target > nums[mid]) {原创 2021-03-30 16:49:15 · 51 阅读 · 0 评论 -
剑指 Offer 05. 替换空格
class Solution { public String replaceSpace(String s) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < s.length(); i++) { if (String.valueOf(s.charAt(i)).equals(" ")) { sb.append("%20");原创 2021-03-29 16:37:54 · 50 阅读 · 0 评论 -
剑指 Offer 57. 和为s的两个数字
class Solution { public int[] twoSum(int[] nums, int target) { // Map<Integer, Integer> map = new HashMap<>(); // for (int i = 0; i < nums.length; i++) { // int res = target - nums[i]; // if (map.原创 2021-03-29 15:49:56 · 50 阅读 · 0 评论 -
58. 最后一个单词的长度
class Solution { public int lengthOfLastWord(String s) { int length = 0; for (int i = s.length() - 1; i >= 0; i--) { if (s.charAt(i) != ' ') { length++; } else if (length != 0) {原创 2021-03-29 15:22:33 · 53 阅读 · 0 评论 -
剑指 Offer 29. 顺时针打印矩阵
class Solution { public int[] spiralOrder(int[][] matrix) { if (matrix.length == 0) { return new int[]{}; } int[] arr = new int[matrix.length * matrix[0].length]; int top = 0; int bottom = matrix.leng原创 2021-03-29 14:47:08 · 54 阅读 · 0 评论 -
剑指 Offer 28. 对称的二叉树
class Solution { public boolean isSymmetric(TreeNode root) { if (root == null) return true; return helper(root.left, root.right); } public boolean helper(TreeNode root1, TreeNode root2) { if (root1 == null &原创 2021-03-27 16:38:45 · 49 阅读 · 0 评论 -
面试题30. 包含min函数的栈
import java.util.*;class MinStack { Stack<Integer> stack; int min; /** initialize your data structure here. */ public MinStack() { this.stack = new Stack<>(); } public void push(int x) { if (stack原创 2021-03-25 07:21:50 · 67 阅读 · 0 评论 -
257. 二叉树的所有路径
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { *原创 2021-03-24 19:19:53 · 45 阅读 · 0 评论 -
剑指 Offer 06. 从尾到头打印链表
采用翻转链表/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public int[] reversePrint(ListNode head) { // 翻转链表原创 2021-03-24 18:48:54 · 52 阅读 · 0 评论 -
剑指 Offer 32 - II. 从上到下打印二叉树 II
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ import java.util.*;class Solution { public List<List<Integer>>原创 2021-03-24 07:33:56 · 50 阅读 · 0 评论 -
剑指 Offer 32 - I. 从上到下打印二叉树
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public int[] levelOrder(TreeNode root) { if原创 2021-03-24 07:22:03 · 62 阅读 · 0 评论 -
剑指 Offer 15. 二进制中1的个数
public class Solution { // you need to treat n as an unsigned value // 把一个整数减去1,再和原整数做与运算,会把该整数最右边一个1变成0.那么一个整数的二进制有多少个1,就可以进行多少次这样的操作。 public int hammingWeight(int n) { int res = 0; while (n != 0) { n = n & (n - 1)原创 2021-03-22 22:29:21 · 57 阅读 · 0 评论 -
剑指 Offer 27. 二叉树的镜像
一、非递归/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode mirrorTree(TreeNode root) { if原创 2021-03-22 22:09:32 · 61 阅读 · 0 评论 -
剑指 Offer 27. 二叉树的镜像
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { public TreeNode mirrorTree(TreeNode root) { if (root原创 2021-03-22 21:45:27 · 51 阅读 · 0 评论 -
剑指 Offer 55 - I. 二叉树的深度
一、递归版本class Solution { public int maxDepth(TreeNode root) { if (root == null) { return 0; } int left = maxDepth(root.left); int right = maxDepth(root.right); return Math.max(left, right) + 1; }原创 2021-03-22 21:39:09 · 60 阅读 · 0 评论