随笔分类 -  leetcode

摘要:用一个栈去存储,从头开始遍历,若遇到比自己大的就放到栈中。 package leetcode; import java.util.Stack; /** * @author doyinana * @create 2020-07-22 9:57 */ public class L300 { public 阅读全文
posted @ 2020-07-22 10:58 doyi 阅读(151) 评论(0) 推荐(0)
摘要:一开始的想法是:用一个map来存储长度较长的数组中的所有数字,再与较短的数组中的数字比较,若出现在较长数组中,则map中的数量减一,最后用原始map与比较结束后的map比较,得到重复的数字有哪些。 答案的解法: 用一个数组来存储比较的结果: class Solution { public int[] 阅读全文
posted @ 2020-07-22 09:57 doyi 阅读(168) 评论(0) 推荐(0)
摘要:这到题一开始自己想到的是用两个栈来转换,再用一个lis来存储。。。经过验证这也太麻烦了 看了大佬的解答,可以直接用stringbuilder来存储 package leetcode;/** * @author doyinana * @create 2020-07-21 18:05 */ public 阅读全文
posted @ 2020-07-22 09:30 doyi 阅读(94) 评论(0) 推荐(0)
摘要:靠 class Solution { public int maxProfit(int[] prices) { int profit=0; for(int i=1;i<prices.length;i++){ int tmp=prices[i]-prices[i-1]; if(tmp>0) profi 阅读全文
posted @ 2020-06-18 18:13 doyi 阅读(118) 评论(0) 推荐(0)
摘要:看了官网给的答案自己写了一遍,一开始还有点没绕清楚,我的妈。。。。 class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> tri=new ArrayList<>(); if(nu 阅读全文
posted @ 2020-06-18 17:59 doyi 阅读(147) 评论(0) 推荐(0)
摘要:看了官方解答的答案,不用从后面的节点倒回来更新值,直接用一个total值来存储整个的值 如果total》=0,说明能形成环路,否则错误。 注意其中的证明。 class Solution { public int canCompleteCircuit(int[] gas, int[] cost) { 阅读全文
posted @ 2020-06-17 13:46 doyi 阅读(142) 评论(0) 推荐(0)
摘要:开始想到了用前缀树,但是没写出来 class Trie { class TireNode { private boolean isEnd; TireNode[] next; public TireNode() { isEnd = false; next = new TireNode[26]; } } 阅读全文
posted @ 2020-06-14 22:51 doyi 阅读(136) 评论(0) 推荐(0)
摘要:yi开始自己想的 class Solution { public int[] productExceptSelf(int[] nums) { int[] res=new int[nums.length]; int sum=1; for(int num:nums){ sum =sum*num; } f 阅读全文
posted @ 2020-06-04 23:53 doyi 阅读(102) 评论(0) 推荐(0)
摘要:牛了 短路的思维 class Solution { int res = 0; public int sumNums(int n) { boolean x = n > 1 && sumNums(n - 1) > 0; res += n; return res; } } 作者:jyd 链接:https: 阅读全文
posted @ 2020-06-03 01:08 doyi 阅读(70) 评论(0) 推荐(0)
摘要:这个题真的是lc送给我们的儿童节礼物,简单的甜哈 class Solution { public List<Boolean> kidsWithCandies(int[] candies, int extraCandies) { int max=candies[0]; for(int i=1;i<ca 阅读全文
posted @ 2020-06-01 22:55 doyi 阅读(89) 评论(0) 推荐(0)
摘要:简简单单 class Solution { public boolean isSymmetric(TreeNode root) { return isMirror(root, root); } public boolean isMirror(TreeNode t1, TreeNode t2) { i 阅读全文
posted @ 2020-05-31 23:03 doyi 阅读(99) 评论(0) 推荐(0)
摘要:方法一:官方答案 class Solution { int ptr; public String decodeString(String s) { LinkedList<String> stk = new LinkedList<String>(); ptr = 0; while (ptr < s.l 阅读全文
posted @ 2020-05-28 22:20 doyi 阅读(138) 评论(0) 推荐(0)
摘要:方法一: class Solution { public int subarraysDivByK(int[] A, int K) { Map<Integer, Integer> record = new HashMap<>(); record.put(0, 1); int sum = 0, ans 阅读全文
posted @ 2020-05-27 23:02 doyi 阅读(156) 评论(0) 推荐(0)
摘要:方法一: public class Solution { public int findDuplicate(int[] nums) { int len = nums.length; int left = 1; int right = len - 1; while (left < right) { / 阅读全文
posted @ 2020-05-26 23:04 doyi 阅读(153) 评论(0) 推荐(0)
摘要:牛 class Node { public int key, val; public Node next, prev; public Node(int k, int v) { this.key = k; this.val = v; } } class DoubleList { private Nod 阅读全文
posted @ 2020-05-25 23:26 doyi 阅读(140) 评论(0) 推荐(0)
摘要:方法一: class Solution { public double findMedianSortedArrays(int[] A, int[] B) { int m = A.length; int n = B.length; int len = m + n; int left = -1, rig 阅读全文
posted @ 2020-05-25 00:18 doyi 阅读(644) 评论(0) 推荐(0)
摘要:方法一:递归 public TreeNode buildTree(int[] preorder, int[] inorder) { return buildTreeHelper(preorder, 0, preorder.length, inorder, 0, inorder.length); } 阅读全文
posted @ 2020-05-22 23:41 doyi 阅读(197) 评论(0) 推荐(0)
摘要:官方给出答案 class Solution { public int findTheLongestSubstring(String s) { int n = s.length(); int[] pos = new int[1 << 5]; Arrays.fill(pos, -1); int ans 阅读全文
posted @ 2020-05-20 21:45 doyi 阅读(165) 评论(0) 推荐(0)
摘要:回文是死穴,多练习 利用双指针来判断 class Solution { public boolean validPalindrome(String s) { for (int i=0,j=s.length()-1;i<j;i++,j--){ if(s.charAt(i)!=s.charAt(j)){ 阅读全文
posted @ 2020-05-20 21:35 doyi 阅读(143) 评论(0) 推荐(0)
摘要:。。。。自己想到的真的太菜了,想用四个变量来存储,考虑到乘以负数最大值会变小的问题,但是没有想到要用最大值或者最小值与nums[i]来比较 package leetcode; /** * @author doyinana * @create 2020-05-20 20:27 */ public cl 阅读全文
posted @ 2020-05-20 21:14 doyi 阅读(101) 评论(0) 推荐(0)