算法与数据结构
fsdgfsf
这个作者很懒,什么都没留下…
展开
-
LeetCode1071. 字符串的最大公因子
思路:使用辗转相除法求出两个字符串的最大公约数class Solution { public String gcdOfStrings(String str1, String str2) { //利用欧几里得辗转相除法求最大公约数 if(!(str1+str2).equals(str2+str1)){ return ""; ...原创 2020-03-12 15:02:06 · 93 阅读 · 0 评论 -
Leetcode1013. 将数组分成和相等的三个部分
主要是充分利用总数和的1/3这个条件class Solution { public boolean canThreePartsEqualSum(int[] A) { int p1 = 0; int p2 = 0; int sum = 0; for(int i = 0;i < A.length;i++){ ...原创 2020-03-11 12:14:41 · 257 阅读 · 1 评论 -
Leetcode322.零钱兑换
思路:一开始想起来的就是给现有的零钱进行排序,然后选取最大面值的零钱,进行回溯,找到就立即退出,这样的做法没有考虑到1,7,10 amount = 14这种情况,10,1,1,1,1与7,7肯定是最少两个硬币。如果说遍历所有的情况,不提前退出,会出现超时的情况。那么回溯的时候不用每次都只减一个coins[i],而是每次减个amount/coins[i]coins[i],这样的话也不行,...原创 2020-03-08 18:29:44 · 200 阅读 · 0 评论 -
LeetCode面试题11. 旋转数组的最小数字
时间复杂度为O(N)的很好达到,这里主要是列出二分查找的方法,时间复杂度是O(logN)numbers[mid] == numbers[r]时,r = r - 1是关键。class Solution { public int minArray(int[] numbers) { if(numbers.length < 1){ return ...原创 2020-03-07 20:31:04 · 116 阅读 · 0 评论 -
LeetCode面试题10- I. 斐波那契数列
要注意a%k + b%k = (a+b)%k这道题要在里面就对k取余,否则会溢出class Solution { public int fib(int n) { if(n <= 0){ return 0; } if(n == 1){ return 1; } ...原创 2020-03-07 18:39:23 · 223 阅读 · 0 评论 -
LeetCode面试题59.队列的最大值
存储最大值的用双向队列,从后面压入,从前面弹出class MaxQueue { Queue<Integer> queue; LinkedList<Integer> maxqueue; public MaxQueue() { queue = new LinkedList<>(); maxqueu...原创 2020-03-07 14:38:54 · 83 阅读 · 0 评论 -
LeetCode785.判断二分图
思路:是标色题,二种颜色class Solution { public boolean isBipartite(int[][] graph) { //标色题,DFS来标 int[] color = new int[graph.length]; for(int i = 0;i < graph.length;i++){ ...原创 2020-03-06 19:27:43 · 220 阅读 · 0 评论 -
LeetCode74. 搜索二维矩阵
思路:讲二维数组想象成一维数组进行二分查找主要是索引,行索引是i/n,列索引是i%nclass Solution { public boolean searchMatrix(int[][] matrix, int target) { if(matrix == null || matrix.length < 1 || matrix[0].length < 1...原创 2020-03-06 18:35:15 · 80 阅读 · 0 评论 -
Leetcode面试题57 - II. 和为s的连续正数序列
思路:我只想到了暴力的方法,但是其实双指针才是正解class Solution { public int[][] findContinuousSequence(int target) { //双指针法 int p1 = 1,p2 = 2; ArrayList<int[]> list = new ArrayList<>...原创 2020-03-06 17:49:36 · 110 阅读 · 0 评论 -
LeetCode565. 数组嵌套
思路:在遍历一组集合的过程中,这个集合就是一个循环的关系,所以遍历过一个元素就不需要再遍历了。时间复杂度是O(N),空间复杂度是O(1)的做法class Solution { public int arrayNesting(int[] nums) { int max = 0; for(int i = 0;i < nums.leng...原创 2020-03-02 12:08:27 · 152 阅读 · 0 评论 -
LeetCode769. 最多能完成排序的块
思路:就是找规律的题,找不到规律就麻爪class Solution { public int maxChunksToSorted(int[] arr) { int max = 0,count = 0; for(int i = 0;i < arr.length;i++){ max = Math.max(arr[i],max);...原创 2020-03-02 12:02:39 · 155 阅读 · 0 评论 -
LeetCode667. 优美的排列 II
思路:是一道找规律的题代码:class Solution { public int[] constructArray(int n, int k) { int[] res = new int[n]; for(int i = 1;i < n - k;i++){ res[i-1] = i; } ...原创 2020-03-01 16:00:09 · 182 阅读 · 0 评论 -
LeetCode5345. 通过投票对团队排名
思路:统计每个字母从1-players排名出现的次数,然后自定义排序函数进行排序class Solution { public String rankTeams(String[] votes) { int voters = votes.length; int players = votes[0].length(); int[][] vot...原创 2020-03-01 14:53:12 · 285 阅读 · 0 评论 -
LeetCode5346.二叉树中的列表
思路:很明显是使用DFS,但是当我使用DFS的时候,竟然有两个用例没有通过看代码没看出来问题,后来发现当head.val != root.val的时候,跳过了树的当前层,但是链表并没有回到起点,相当于是找树的子序列了,这样遍历下去便不连续了,所以这是错误的/** * Definition for singly-linked list. * public class ListNode {...原创 2020-03-01 13:11:35 · 95 阅读 · 0 评论 -
LeetCode287. 寻找重复数
思路:要求比较严苛首先先介绍二分法,一种时间复杂度是O(nlogn),空间复杂度是O(1)的做法。class Solution { public int findDuplicate(int[] nums) { int left = 1,right = nums.length - 1; while(left < right){ ...原创 2020-03-01 10:30:40 · 136 阅读 · 1 评论 -
LeetCode645. 错误的集合
思路:时间复杂度O(N),空间复杂度O(N)使用额外的数组空间来存储出现的次数class Solution { public int[] findErrorNums(int[] nums) { int[] tmp = new int[nums.length+1]; int repeated = 0; int n = nums.lengt...原创 2020-02-29 18:16:48 · 105 阅读 · 0 评论 -
LeetCode.378. 有序矩阵中第K小的元素
思路:1.使用最小堆我的做法就是使用最小堆来存放那些可能的最小值,一个值为当前最小值,那么下一个最小值会是它的右边和下边的那个值,因此要把这两个都放进最小堆里面去,为了防止存放重复的数字还搞了个布尔数组。class Solution { public int kthSmallest(int[][] matrix, int k) { if(matrix == null...原创 2020-02-29 17:36:11 · 130 阅读 · 0 评论 -
LeetCode240.搜索二维矩阵 II
时间复杂度是O(M+N)class Solution { public boolean searchMatrix(int[][] matrix, int target) { if(matrix.length < 1) return false; int m = matrix.length; int n ...原创 2020-02-29 14:15:52 · 71 阅读 · 0 评论 -
LeetCode485. 最大连续1的个数
思路就是判断是否是连续的1class Solution { public int findMaxConsecutiveOnes(int[] nums) { if(nums == null || nums.length < 1){ return 0; } int len = 0; int max...原创 2020-02-29 12:35:01 · 144 阅读 · 0 评论 -
LeetCode283.移动零
思路1:双指针,进行交换操作class Solution { public void moveZeroes(int[] nums) { int p0 = 0; int p1 = 0; while(p1 < nums.length && p0 < nums.length){ swap(n...原创 2020-02-29 12:02:31 · 77 阅读 · 0 评论 -
LeetCode792. 匹配子序列的单词数
思路:1.爆破法,对每个单词进行比对,但是超时2,考虑只遍历S一次class Solution { public int numMatchingSubseq(String S, String[] words) { HashMap<Character,ArrayList<String>> map = new HashMap<>()...原创 2020-02-27 22:26:19 · 207 阅读 · 0 评论 -
LeetCode611. 有效三角形的个数
思路:竟然最初的想法是用回溯,真的是没想到会超时。连爆破都没超时class Solution { public int triangleNumber(int[] nums) { Arrays.sort(nums); int count = 0; for(int i = 0;i < nums.length-2;i++){ ...原创 2020-02-27 21:50:46 · 84 阅读 · 0 评论 -
LeetCode476.数字的补数
思路:1.把数字的二进制求出来,进行翻转(1变0,0变1)然后求出来当前二进制的十进制class Solution { public int findComplement(int num) { int sum = 0; int k = 0,t = 0; while(num > 0){ k = num % 2...原创 2020-02-27 21:35:37 · 90 阅读 · 0 评论 -
LeetCode696. 计数二进制子串
思路:聪明的人真的很聪明,看别人的做法写出来的,数字的翻转游戏class Solution { int count = 0; public int countBinarySubstrings(String s) { int count = 0; int pre = 0,cur = 1; for(int i = 1;i < ...原创 2020-02-27 21:30:24 · 84 阅读 · 0 评论 -
LeetCode647.回文子串
我的思路是找出所有的子串然后,去判断是否是回文的这样的时间复杂度是O(N^3)class Solution { int count = 0; public int countSubstrings(String s) { //挨个判断是否是回文子串 for(int i = 0;i < s.length();i++){ ...原创 2020-02-25 22:41:52 · 75 阅读 · 0 评论 -
LeetCode205.同构字符串 LeetCode290.单词规律
使用数组来记录上一次出现的索引值class Solution { public boolean isIsomorphic(String s, String t) { int[] index1 = new int[256]; int[] index2 = new int[256]; for(int i = 0;i < s.length...原创 2020-02-25 22:36:05 · 115 阅读 · 0 评论 -
LeetCode.哈希表系列题
1.求数组中两数之和为目的值Two Sums针对给定的数组序列是无序的class Solution { public int[] twoSum(int[] nums, int target) { //对于无序数组的两数之和 HashMap<Integer,Integer> hm = new HashMap<>(); ...原创 2020-02-24 17:13:33 · 103 阅读 · 0 评论 -
Leetcode栈和队列
栈实现队列很简单,在一个栈中压入,另一个栈负责弹出push():只需要将元素压入栈1,时间复杂度是O(n)pop():当栈2为空的时候将栈1中的元素全部弹出压入栈2.否则直接从栈2弹出即可empty():两栈全部为空class MyQueue { private Stack<Integer> stack1; private Stack<Integer&g...原创 2020-02-20 12:01:58 · 263 阅读 · 0 评论 -
LeetCode208.677实现前缀树
LeetCode208.实现前缀树每个前缀树节点需要一个boolean值来记录当前节点是不是终止点。还需要一个size为26的节点数组来记录这个字母下面的节点。class Trie { private TrieNode root; class TrieNode{ boolean flag; TrieNode[] nodes = new Trie...原创 2020-02-20 11:50:03 · 99 阅读 · 0 评论 -
LeetCode109.653.501一些BST的题
LeetCode109.有序链表转换成二叉搜索树这道题是 LeetCode108.从有序数组中构造二叉查找树 的进阶版/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * ...原创 2020-02-20 11:31:10 · 121 阅读 · 0 评论 -
LeetCode前序、中序、后序的非递归遍历,以及BST的题目
前序、中序、后序的递归解法非常好做,但是迭代的写法比较生疏,甚至可以写上一个多小时,令人害怕BST的题目做得不是很好,感觉主要问题是脑子想不到递归该如何去解,思维混乱序列化和反序列化这类的题目也比较生疏,虽然LeetCode上把它标为hard,但是感觉还是很常见的题目之一,一定要会做非递归前序遍历前序遍历其实是根左右非递归前序遍历的思路:如果先输出遍历元素,再向栈中压入右节点,左节点,这...原创 2020-02-17 16:13:34 · 124 阅读 · 0 评论 -
LeetCode513.找数左下角的值
思路:1.使用BFS,我的代码,很普通的常规写法/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } *...原创 2020-02-15 17:03:34 · 103 阅读 · 0 评论 -
LeetCode337.打家劫舍III
这道题我的思路是错的,没有写出来,看了别人的思路用递归来写代码,但是效率不高/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val...原创 2020-02-15 15:49:20 · 163 阅读 · 0 评论 -
LeetCode687.最长同值路径
思路:我自己写的,时间复杂度是O(n^2)/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */c...原创 2020-02-15 14:41:53 · 129 阅读 · 0 评论 -
LeetCode111.二叉树的最小深度
思路:1.DFS/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution...原创 2020-02-14 16:00:37 · 181 阅读 · 0 评论 -
LeetCode437.路径总和III
我的做法O(N^2)/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solutio...原创 2020-02-14 15:00:02 · 84 阅读 · 0 评论 -
LeetCode112.路径总和
我自己的写法/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution {...原创 2020-02-14 14:12:39 · 70 阅读 · 0 评论 -
LeetCode226.翻转二叉树
思路:我只会用迭代的方法/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solu...原创 2020-02-13 15:18:01 · 79 阅读 · 0 评论 -
LeetCode328.奇偶链表
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode oddEvenList...原创 2020-02-13 08:42:45 · 67 阅读 · 0 评论 -
LeetCode234.回文链表
思路:找到中点,逆转后半部分链表,然后与前半部分进行比对找到中点可以使用快慢指针的方式,快指针走两步,慢指针走一步/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x;...原创 2020-02-12 21:20:01 · 68 阅读 · 0 评论