
java
musenh
这个作者很懒,什么都没留下…
展开
-
offer26
思路:先序遍历A的每个节点N:isSubStructure(A,B) 判断以N为根节点的子树是否包含树B:recur(A,B) 对于函数isSubStructure(A,B)当A为空或者B为空时,返回false当B为A的子树时,瞒住以下条件中的一个以节点A为根节点的子树包含树B,对应recur(A,B) 以A节点的左孩子为根节点的子树包含树B:对应isSubStructure(A.left,B) 以A节点的右孩子为根节点的子树包含树B,对应isSubStructure(A.ri...原创 2020-09-04 17:39:11 · 130 阅读 · 0 评论 -
offer31
public class Main { public static void main(String[] args) { int[] pushed = {1,2,3,4,5}; int[] popped = {4,3,5,1,2}; Stack<Integer> st = new Stack(); int i = 0,j = 0,n = pushed.length; boolean f = true; while(j &l...原创 2020-09-04 17:13:52 · 143 阅读 · 0 评论 -
leetcode433
深度优先模板/* * dfs * 终止条件 * 访问当前节点 * 下探到下一层 * visted = set() * * def(node,visited): * if node in visited: * return * * visited.add(node) * * for next_node in node.children(): * if not next_node in visited: * dfs(next.node,visited) */.原创 2020-09-03 22:20:28 · 148 阅读 · 0 评论 -
leetcode200
/* * 广度优先: * queue =[] * queue.append(start) * visited.append(start) * * while queue: * node = queue.pop()遍历当前层的节点 * visited.add() * * process(node) * nodes = gennertae_visited_node(node) * queue.push(nodes)//每次在队列中添加下一层的节点 */采用扩散法和沉岛..原创 2020-09-03 22:16:36 · 143 阅读 · 0 评论 -
leetcode55
/* * 从最后开始,记录能到达最后一个点的下标,canreach初始化为length-1 * 因为最后一个点一定可以到达最后一个点,然后从后往前遍历 * 如果当前点加上其能跳跃的值大于等于最后一个点,则该点可以到达 * 最后一个点,此时将canreach重新赋值为当前点的下标 * 表明此时当前点可以到达最后一个点 */public class Main { public static void main(String[] args) { int[] nums = {2...原创 2020-09-03 22:09:35 · 112 阅读 · 0 评论 -
剑指offer31
public class Main { public static void main(String[] args) { int[] pushed = {1,2,3,4,5}; int[] popped = {4,3,5,1,2}; Stack<Integer> st = new Stack(); int i = 0,j = 0,n = pushed.length; boolean f = true; while(j &l...原创 2020-09-03 22:06:23 · 96 阅读 · 0 评论 -
剑指offer13
深度优先搜索深度优先其实是一种递归,广度优先一般是使用队列实现的深度优先递归模板:输入当前状态i,j1.判断i,j是否越界、i、j中的元素是否不满足题目要求,i、j是否访问过,是,则返回2.将当前i、j设置为访问过3.进行递归,下探到下一层,并记录结果(不需要重复查找,则可以在此返回,需要重复查找,例如寻找某个路径,那么两条不同的路径会重复经过某个点,而机器人运动范围则只是统计坐标范围,坐标不可以重复计算)4.恢复未访问状态(如果需要重复查找的话)5.返回结果pub原创 2020-09-03 22:05:29 · 83 阅读 · 0 评论 -
剑指offer48
滑动窗口import java.util.HashSet;/* * 滑动窗口问题: * 1.集合有自动去重功能,当当前元素与集合中的元素重复时 * 2.删除集合末尾的 元素(一般是循环删除,直到满足条件,比如小于滑动窗口要求的大小,或者不再有重复元素), * 其中k用来记录当前集合末尾元素的下标, * 通过k可以得到末尾元素,hs的删除操作是通过元素的值来进行的 * 3.当集合中没有与当前元素重复的元素了以后,将当前元素加入集合 * 4.计算最长不重复子串的长度 */publi原创 2020-08-27 16:00:01 · 118 阅读 · 0 评论 -
滑动窗口最大值
方法一://思路1:填满窗口,找出当前窗口最大值,将窗口移动一格class Solution { public int[] maxSlidingWindow(int[] nums, int k) { if(k == 0|| nums==null||nums.length == 0) return new int[]{}; Deque<Integer> q = new LinkedList(); int ma = nums[0],..原创 2020-08-27 15:51:38 · 134 阅读 · 0 评论 -
剑指offer56-1
位运算判断时,最好采用等于0的判断方式,否则会出错/* * 思路: * 先找出两个数字不同的位置,即异或为1的位置b * 将数字分为两组,一组b为0,一组b为1 * 分别将这两组异或,得到两个不同的数字 */public class Main { public static void main(String[] args) { int[] nums = {1,2,5,2}; int res = 0; for(int i = 0;i < nu原创 2020-08-27 15:36:32 · 123 阅读 · 0 评论 -
直方图的最小栈解法
class Solution { public int largestRectangleArea(int[] heights) { int squar = 0; Stack<Integer> st = new Stack();存储下标 int[] tmp = new int[heights.length+2]; System.arraycopy(heights, 0, tmp, 1, heights.length)...原创 2020-08-27 10:58:55 · 136 阅读 · 0 评论 -
剑指offer45
/*思路:使用快排,比较字符串拼接的数字大小给定两个数字m和n,寻找一种排序方法如果凭借成的数字mn<nm,则将m排序在n的前面反之,将n排在前面*/class Solution { public String minNumber(int[] nums) { String[] st = new String[nums.length]; for(int i = 0;i < nums.length;i++) { st...原创 2020-08-27 10:52:04 · 182 阅读 · 0 评论 -
leetcode46
动态规划/*递推公式:设当前位置为i1.当i和i-1可以组成字母,则判断i-2是否小于0如果小于,则证明只有两位数目前,且这两位数有分开翻译和合起来翻译两种方法,因此直接dp++如果大于,dp[i] = dp[i-2]+dp[i-1]2.当i和i-1位置的数字无法组成字母,则只有单独翻译一种方法因此,dp[i] = dp[i-1]*/class Solution { public int translateNum(int num) { String st =原创 2020-08-27 10:43:33 · 126 阅读 · 0 评论 -
leetcode98
使用中序遍历对二叉树进行遍历,并使用一个变量记录前一个节点上二叉树的值,为了判断是否为递增的class Solution { public long k = Long.MIN_VALUE; boolean f = true; public boolean isValidBST(TreeNode root) { if(root == null) return true; //到达叶子节点时,证明完成验证,是二叉搜索树 if(f..原创 2020-08-27 10:35:08 · 156 阅读 · 0 评论 -
复制链表(剪枝offer35)
、class Solution { public Node copyRandomList(Node head) { HashMap<Node,Node> hm = new HashMap(); Node cur = head; //将新复制的链表放在map的value,方便找到与原节点对应的节点 while(cur != null){ hm.put(cur,new Node(cur.val))原创 2020-08-27 10:26:28 · 93 阅读 · 0 评论 -
计算数组的子集
计算某个数组的子集(在递归中不用for循环和visit数组,因为每个结果长度不一致,如果每个结果长度一致,比如求全排列,则需要用for和visit,但后者也可以用记录层数的方式)public class Main { public static void main(String[] args) { int[] nums = {1,2,3}; List<List<Integer>> li = new ArrayList(); fin(nu原创 2020-08-27 10:24:07 · 276 阅读 · 0 评论 -
分治计算x的n次方
分治算法代码模板divide_pro{ 问题解决 if problem is none: print result return; 处理当前逻辑,如何分成子问题 data = prepare_data(problem) subproblems = split_problem(problem,data) 调用函数,下探一层解决子问题 subres1 = divide_pro(subproblems[0]..原创 2020-08-26 17:00:56 · 964 阅读 · 0 评论 -
leetcode433
深度优先遍历代码模板/* * dfs * 终止条件 * 访问当前节点 * 下探到下一层 * visted = set() * * def(node,visited): * if node in visited: * return * * visited.add(node) * * for next_node in node.children(): * if not next_node in visited: * dfs(next.node,visited) *原创 2020-08-26 16:56:42 · 152 阅读 · 0 评论 -
leetcode200
广度优先代码模板/* * 广度优先: * queue =[] * queue.append(start) * visited.append(start) * * while queue: * node = queue.pop()遍历当前层的节点 * visited.add() * * process(node) * nodes = gennertae_visited_node(node) * queue.push(nodes)//每次在队列中添加下一层的节点 */原创 2020-08-26 16:50:05 · 140 阅读 · 0 评论 -
leetcode55
反向贪心贪心算法解决问题,需要问题具有最优子结构,即局部最优解,能够得到全局最优解/* * 从最后开始,记录能到达最后一个点的下标,canreach初始化为length-1 * 因为最后一个点一定可以到达最后一个点,然后从后往前遍历 * 如果当前点加上其能跳跃的值大于等于最后一个点,则该点可以到达 * 最后一个点,此时将canreach重新赋值为当前点的下标 * 表明此时当前点可以到达最后一个点 */public class Main { public static vo原创 2020-08-26 16:40:51 · 143 阅读 · 0 评论 -
leetcode69
使用二分查找求小于某个数字的平方根的最大整数思路:使用二分查找减少查找时间,要每次记录mid的值,否则left和right可能会大于或者小于真正的结果值class Solution { public int mySqrt(int x) { if(x == 0||x == 1) return x; int left = 1,right = x,res = -1; while(left <= right) {原创 2020-08-26 16:35:31 · 176 阅读 · 0 评论 -
leetcode——169
给定某个长度为n的数组,个数超过n/2的数字为众数,求众数,先排序,然后返回下标n/2位置的数字,即为众数class Solution { public int majorityElement(int[] nums) { Arrays.sort(nums); &...原创 2019-08-08 17:49:04 · 97 阅读 · 0 评论 -
leetcode——100
递归判断是否是相同的树,如果两个节点同时为null,那么证明是相同的树,```java/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) {...原创 2019-09-30 09:09:57 · 66 阅读 · 0 评论 -
leecode——929
class Solution { public int numUniqueEmails(String[] emails) { Set<String> se = new HashSet<>(); for(String e : emails){ int i = e.indexOf('@'); ...原创 2019-09-20 10:35:37 · 131 阅读 · 0 评论 -
leetcode——788
在这里插入代码片原创 2019-09-20 10:01:54 · 151 阅读 · 0 评论 -
leetcode——804
将单词转化为摩斯电码class Solution { public int uniqueMorseRepresentations(String[] words) { HashMap<Character,String> m = new HashMap();//map将单词映射给莫斯码 int i,j; String[] p =...原创 2019-09-20 09:12:06 · 77 阅读 · 0 评论 -
leetcode——101
递归判断二叉树是否对称/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solutio...原创 2019-09-30 09:19:00 · 83 阅读 · 0 评论 -
leetcode——104
递归计算二叉树深度,递归左深度和右深度的值取最大,并加上初始层,即加一/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; ...原创 2019-09-30 09:26:55 · 85 阅读 · 0 评论 -
leetcode——637
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { pub...原创 2019-10-03 10:29:02 · 84 阅读 · 0 评论 -
leetcode——110
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { ...原创 2019-10-03 10:48:24 · 80 阅读 · 0 评论 -
leetcode——257
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { pub...原创 2019-10-03 11:57:07 · 93 阅读 · 0 评论 -
程序员代码面试指南——笔记1
在java中,想得到字符串str的第i个位置的字符,要使用:char p = str.charAt(i);把字符串转化为char类型的数组char[] arr = str.toCharArray();此时得到位置i的元素使用char p = arr[i];...原创 2019-10-03 12:19:25 · 175 阅读 · 0 评论 -
leetcode——541
分组反转字符串,将反转函数作为一个模块,另一个模块用来分组class Solution { public String reverseStr(String s, int k) { Solution ss = new Solution(); char[] pop = s.toCharArray(); int l = s.length();...原创 2019-09-17 11:41:20 · 129 阅读 · 0 评论 -
leetcode——459
将两个s合并成另外一个字符串,掐头去尾看是否还包含原来的sclass Solution { public boolean repeatedSubstringPattern(String s) { String str = s + s; retur...原创 2019-09-12 14:10:07 · 185 阅读 · 0 评论 -
leetcode——976
给定一个数组,从中挑选能够组成三角形的三边,并计算使得周长最大的三边的出的周长首先排序,然后三角形必须满足两边之和大于第三边,因为是排序后的数组,故直接计算这个条件即可class Solution { public int largestPerimeter(int[] A) { Arrays.sort(A); int i; for(i =...原创 2019-08-26 16:22:29 · 191 阅读 · 0 评论 -
leetcode——242
给定两个字符串,判断一个是不是另外一个的变位词,即字母类型、个数相同,的那位置不同的词,比如:cat和tac先排序,后判断两个字符串是否一样class Solution { public boolean isAnagram(String s, String t) { if...原创 2019-08-26 16:00:08 · 98 阅读 · 0 评论 -
leetcode——989
思路:将K直接加到A[i]上,并将最低位留在A[i]上,并且将原本的k+A[i]除以10,作为下一刻K值加到i–后的A[i]上,以此类推直到结束class Solution { public List<Integer> addToArrayForm(int[] A, int K) { int n = A.length; int cur = K...原创 2019-08-10 17:25:41 · 223 阅读 · 1 评论 -
leetcode——717
有两种特殊字符,一种可以用一比特0来表示,第二种可以用二比特10和11表示,给定一个比特串,判断最后一个字符是否为一比特字符思路:遍历比特串,如果位置上为1,则将index加二,如果位置上为0,则加一class Solution { public boolean isOneBitCharacter(int[] bits) { int i = 0; whi...原创 2019-08-10 16:48:22 · 175 阅读 · 0 评论 -
leetcode——122
使用连续的波峰减去波谷比如:[7, 1, 5, 3, 6, 4]画成折线图如下连续波峰波谷之间差的和即为最大的利润,代码如下,仍然要注意输入为空的情况class Solution { public int maxProfit(int[] prices) { if(...转载 2019-08-07 18:25:47 · 138 阅读 · 0 评论 -
leetcode——448
给定一个范围在1到n之间的数组,可能有重复元素,要求查找出不在数组中出现的1到n之间的数字,为节约空间,先用交换排序,后判断第i个元素是不是等于i+1,class Solution { public List<Integer> findDisappearedNumbers(int[] nums) { for(int i = 0; i<nums.lengt...原创 2019-08-09 19:11:37 · 168 阅读 · 0 评论