算法
springTen
这个作者很懒,什么都没留下…
展开
-
剑指offer33 判断指定序列是否是二叉搜索树的后序遍历 Java
public class VerifySequenOfBST33_ { public static void main(String[] args) { int[] arr = {5, 7, 6, 9, 11, 10, 8}; System.out.println(check(arr, 0, arr.length - 1)); } pr...原创 2019-08-14 08:58:06 · 83 阅读 · 0 评论 -
剑指offer29 顺时针打印矩阵序列 Java
public class PrintMatrixSequence29 { public static void main(String[] args) { int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8},{9, 10, 11, 12}, {13, 14, 15, 16}}; int rows = matrix.le...原创 2019-08-15 09:33:53 · 75 阅读 · 0 评论 -
剑指offer 06 倒序打印链表 Java
public class PrintListFromTailToHead06 { static class Node { int val; Node next; public Node(int val){ this.val = val; } } public static void m...原创 2019-08-15 09:34:08 · 75 阅读 · 0 评论 -
剑指offer32 从上到下打印二叉树 Java
public class PrintBinaryTreeTopToBottom32 { static class Node { int val; Node left; Node right; public Node(int val){ this.val = val; } ...原创 2019-08-15 09:34:14 · 69 阅读 · 0 评论 -
剑指offer 56 出现一次的数 Java
数组中只有一个数只出现1次,其他的出现3次,找出这个数public class OnceInOtherThree56 { public static void main(String[] args) { int[] arr = {1, 2, 3, 4, 1, 1, 2, 4, 4, 2}; findThat(arr); } priv...原创 2019-08-15 09:34:20 · 86 阅读 · 0 评论 -
剑指offer21 奇偶序列 Java
public class OddEvenSequence21 { public static void main(String[] args) { int[] arr = {1, 2, 5, 6, 4, 3, 8, 0}; adjust(arr); for (int i = 0; i < arr.length; i++) { ...原创 2019-08-15 09:34:25 · 132 阅读 · 0 评论 -
剑指offer39 出现次数超过一半的数 Java
public class NumOverHalf39 { public static void main(String[] args) { int[] arr = {1, 2, 3, 2, 2, 2, 5, 4, 2}; System.out.println(overhaf(arr)); } private static int over...原创 2019-08-15 09:34:31 · 73 阅读 · 0 评论 -
剑指offer15 二进制中1的个数 Java
public class NumOfOne15 { public static void main(String[] args) { int n = 15; System.out.println(countOne2(n)); } private static int countOne(int n) { int count ...原创 2019-08-15 09:34:37 · 68 阅读 · 0 评论 -
剑指offer21 链表中的倒数第K个节点 Java
public class NumKOfReverse21 { static class Node { int val; Node next; public Node(int val){ this.val = val; } @Override public String ...原创 2019-08-17 15:11:53 · 77 阅读 · 0 评论 -
剑指offer08 二叉搜索树中序遍历某节点的下一个节点 Java
private static Node nextNode(Node node) { //只有一个节点 if (node.left == null && node.right == null) return null; //若该节点有右孩子 if (node.right != null) { ...原创 2019-08-17 15:11:59 · 252 阅读 · 0 评论 -
剑指offer 07 重建二叉树 Java
public class RebuildBinaryTree07_ { static class Node { int val; Node left; Node right; public Node(int val){ this.val = val; } @Overr...原创 2019-08-15 09:33:46 · 102 阅读 · 0 评论 -
剑指offer24 反转链表 Java
public class ReverseLinkList24 { static class Node { int val; Node next; public Node(int val){ this.val = val; } @Override public Strin...原创 2019-08-15 09:33:39 · 80 阅读 · 0 评论 -
剑指offer58 反转字符串 Java
public class ReverseString58_ { public static void main(String[] args) { String str = "i am a student!"; System.out.println(reverseString(str)); } private static St...原创 2019-08-15 09:33:33 · 98 阅读 · 0 评论 -
剑指offer09 两个栈实现一个队列 Java
public class TwoStackForOneQueue09 { static class Queueue{ static Stack<Integer> stack1 = new Stack<>(); static Stack<Integer> stack2 = new Stack<>(); ...原创 2019-08-14 08:58:13 · 69 阅读 · 0 评论 -
剑指offer09 两个队列实现一个栈 Java
public class TwoQueueForOneStack09 { static class Stackck { static Queue<Integer> queue1 = new LinkedList<>(); static Queue<Integer> queue2 = new LinkedList<&...原创 2019-08-14 08:58:39 · 58 阅读 · 0 评论 -
剑指offer56 数组里只出现一次的两个数字 Java
public class TwoNumShowOnceInArr56 { public static void main(String[] args) { int[] arr = {2, 4, 3, 6, 3, 2, 5, 5}; findit(arr); } private static void findit(int[] arr) {...原创 2019-08-14 08:58:46 · 85 阅读 · 0 评论 -
剑指offer52 两个链表的第一个公共节点 Java
public class TwoLinkPublicNode52 { static class Node { int val; Node next; public Node(int val){ this.val = val; } @Override public Str...原创 2019-08-14 08:58:53 · 117 阅读 · 0 评论 -
剑指offer57 和为某数的连续子数组 Java
public class SumOfSerialNumber57_ { public static void main(String[] args) { find(15); } private static void find(int sum) { if (sum <= 2) return; i...原创 2019-08-14 08:59:01 · 211 阅读 · 0 评论 -
剑指offer57 和为某数的两个数字 Java
public class SumEqualsNum57 { public static void main(String[] args) { int[] arr = {1, 2, 3, 5, 6, 7, 8, 9, 10}; findTwo(arr, 14); } private static void findTwo(int[] arr...原创 2019-08-14 08:59:07 · 88 阅读 · 0 评论 -
剑指offer26 树的子结构 Java
public class SubTreeConstruct26_ { static class Node { int val; Node left; Node right; public Node(int val){ this.val = val; } @Overrid...原创 2019-08-14 08:59:13 · 96 阅读 · 0 评论 -
剑指offer31 验证一个出栈顺序是否正确 Java
public class StackIsValide31 { public static void main(String[] args) { int[] in = {1, 2, 3, 4, 5}; int[] out = {4, 5, 3, 1, 1}; System.out.println(isValide(in, out)); ...原创 2019-08-14 08:59:23 · 176 阅读 · 0 评论 -
剑指offer30 包含min函数的栈 Java
public class StackIncludeMinFunc30_ { static class Stackck { static Stack<Integer> stack = new Stack<>(); static Stack<Integer> minStack = new Stack<>()...原创 2019-08-14 08:59:36 · 67 阅读 · 0 评论 -
剑指offer40 最小的K个数 Java
public class MinestK40_ { public static void main(String[] args) { int[] arr = {4, 5, 1, 6, 2, 7, 3, 8}; int k = 4; minestK(arr, k - 1); for (int i = 0; i < 4; ...原创 2019-08-17 15:12:05 · 90 阅读 · 0 评论 -
剑指offer25 合并两个有序链表 Java
public class MergeSortedLinkList25 { static class Node { int val; Node next; public Node(int val){ this.val = val; } @Override public S...原创 2019-08-17 15:12:12 · 80 阅读 · 0 评论 -
剑指offer48 最长不重复子串 Java
public class LongestSubStringWithoutDuplication48__ { public static void main(String[] args) { String str = "arabcacfr"; System.out.println(subString(str)); int i = 0; ...原创 2019-08-17 15:12:17 · 129 阅读 · 0 评论 -
剑指offer 28 判断二叉树是否对称 Java
如题:public class BinaryTreeIsSymmertrical28 { static class Node { int val; Node left; Node right; public Node(int val){ this.val = val; } ...原创 2019-08-12 18:57:31 · 69 阅读 · 0 评论 -
Leecode 213 Rob213 打家劫舍升级版 Java
打家劫舍2* 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。* 给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。public class Rob21...原创 2019-08-12 18:46:42 · 117 阅读 · 0 评论 -
Leetcode198 Rob198 打家劫舍 Java
打家劫舍* 你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。* 给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。public class Rob198 { public static void ...原创 2019-08-12 18:45:10 · 62 阅读 · 0 评论 -
Leetcode 64 MinPathSum64 最小路径和 Java
64. 最小路径和* 给定一个包含非负整数的 mxn网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。* 说明:每次只能向下或者向右移动一步。* 示例:* 输入:* [* [1,3,1],* [1,5,1],* [4,2,1]* ]* 输出: 7* 解释: 因为路径 1→3→1→1→1 的总和最小。public class MinP...原创 2019-08-12 18:41:47 · 137 阅读 · 0 评论 -
Leetcode53 MaxSubArray53 最大子序和 Java
53. 最大子序和* 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。public class MaxSubArray53 { public static void main(String[] args) { int[] arr = {5, -6, 8, 2, 1, -6, -1, 9, 8, 5,}; ...原创 2019-08-12 18:37:08 · 101 阅读 · 0 评论 -
Leetcode 121 MaxProfit121 买卖股票的最大收益 Java
121. 买卖股票的最佳时机* 给定一个数组,它的第i 个元素是一支给定股票第 i 天的价格。* 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。* 注意你不能在买入股票前卖出股票public class MaxProfit121 { public static void main(String[] args) { ...原创 2019-08-12 18:32:33 · 132 阅读 · 0 评论 -
Leetcode 1025 DivisorGame1025 Java
爱丽丝和鲍勃一起玩游戏,他们轮流行动。爱丽丝先手开局。* 最初,黑板上有一个数字N。在每个玩家的回合,玩家需要执行以下操作:* 选出任一x,满足0 < x < N 且N % x == 0。* 用 N - x替换黑板上的数字 N 。* 如果玩家无法执行这些操作,就会输掉游戏。* 只有在爱丽丝在游戏中取得胜利时才返回True,否则返回 false。假设两个玩家都...原创 2019-08-12 18:30:23 · 115 阅读 · 0 评论 -
Leetcode 70 ClimbStairs 70 爬楼梯 Java
爬楼梯假设你正在爬楼梯。需要 n 阶你才能到达楼顶。每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?注意:给定 n 是一个正整数。public class ClimbStairs70 { public static void main(String[] args) { System.out.println(climbStairs(55))...原创 2019-08-12 18:27:43 · 66 阅读 · 0 评论 -
Leetcode 62 不同路径 UniquePaths Java
输入: m = 3, n = 2输出: 3解释:从左上角开始,总共有 3 条路径可以到达右下角。1. 向右 -> 向右 -> 向下2. 向右 -> 向下 -> 向右3. 向下 -> 向右 -> 向右public class UniquePaths62 { public static void main(String[] args) {...原创 2019-08-12 18:23:34 · 83 阅读 · 0 评论 -
剑指offer54 二叉搜索树的第K大节点 Java
public class BinaryTreeK54 { static class Node { int val; Node left; Node right; public Node(int val) { this.val = val; } @Override ...原创 2019-08-12 19:00:13 · 87 阅读 · 0 评论 -
剑指offer27 二叉树的镜像 Java
public class BinaryTreeMirror27_ { static class Node { int val; Node left; Node right; public Node(int val){ this.val = val; } @Overrid...原创 2019-08-19 09:00:28 · 77 阅读 · 0 评论 -
剑指offer 34 二叉树中和为某一值的路径 Java
public class BinaryTreePath34_ { static class Node { int val; Node left; Node right; public Node(int val) { this.val = val; } @Overri...原创 2019-08-19 09:00:40 · 85 阅读 · 0 评论 -
剑指offer58 左旋转字符串 Java
public class LeftReverseString58 { public static void main(String[] args) { String str = "abcdefg"; System.out.println(leftReverse(str, 3)); } private static String leftR...原创 2019-08-17 15:12:22 · 82 阅读 · 0 评论 -
剑指offer55 是否是平衡二叉树 Java
public class IsBalancedTree55 { static class Node { int val; Node left; Node right; public Node(int val) { this.val = val; } @Overrid...原创 2019-08-17 15:12:28 · 114 阅读 · 0 评论 -
剑指offer49 丑数
丑数,2,3,5的倍数,1是第一个丑数public class GetUglyNumber49 { public static void main(String[] args) { int n = 55; System.out.println(ugly(n)); } private static int ugly(int n) {...原创 2019-08-17 15:12:34 · 109 阅读 · 0 评论