- 博客(42)
- 资源 (1)
- 收藏
- 关注
原创 LeetCode 28. Implement strStr() Rabin-Karp算法
Description:Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Example 1:Input: haystack = “hello”, needle = “ll”Output: 2Clarification:What should we return when needle is an e
2020-05-19 10:20:18 632
原创 LeetCode 40. Combination Sum II
Description:Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.Each number in candidates may only be used once in the combination.Note:
2020-05-31 21:45:29 153
原创 LeetCode 39. Combination Sum
Description:Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.The same repeated number may be chosen from candidates unli
2020-05-31 01:54:11 176
原创 LeetCode 83. Remove Duplicates from Sorted List
Description:Given a sorted linked list, delete all duplicates such that each element appear only once.Example 1:Input: 1->1->2Output: 1->2Solution:leetcode 上有个奇怪的现象,这个cpp代码跑了12ms,一模一样改写的java代码却只跑了0ms ??????public ListNode deleteDuplicates(L
2020-05-30 20:38:34 193
原创 LeetCode 196. Delete Duplicate Emails
Description:Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique emails based on its smallest Id.±—±-----------------+| Id | Email |±—±-----------------+| 1 | [email protected] || 2 | bob@ex
2020-05-30 05:49:45 142
原创 LeetCode 70. Climbing Stairs
Description:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?Note: Given n will be a positive integer.Example 1:Input: 2Output: 2Expla
2020-05-30 05:37:18 154
原创 LeetCode 176. Second Highest Salary
Description:Write a SQL query to get the second highest salary from the Employee table.±—±-------+| Id | Salary |±—±-------+| 1 | 100 || 2 | 200 || 3 | 300 |±—±-------+For example, given the above Employee table, the query should retur
2020-05-27 22:47:25 151
原创 图的DFS与BFS
图的DFS与BFS用好,能解决leetcode 25%的题目DFS是一次走到底,(如果不用递归)所以要用stack保留之前信息(可理解成树的先序遍历),BFS每次只走一步,新添加的待遍历点放入queue,等之后再遍历。其实我有一篇关于DFS和BFS很好的教材,不过是全英版的。链接戳这里~根据数据结构(C语言版)清华大学出版社:DFS:Boolean visited[MAX]; //记录某个节点是否被访问过Status(*VisitFunc)(int v);
2020-05-26 22:38:04 247
原创 LeetCode 542. 01 Matrix
Description:Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.The distance between two adjacent cells is 1.Example 1:Input:[[0,0,0],[0,1,0],[0,0,0]]Output:[[0,0,0],[0,1,0],[0,0,0]]Solution:public int[][] upda
2020-05-26 13:38:07 292
原创 LeetCode 547. Friend Circles
Description:There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then A is an indirect friend of C. And we define
2020-05-26 13:07:51 129
原创 LeetCode 200. Number of Islands
Description:Given a 2d grid map of '1’s (land) and '0’s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surroun
2020-05-26 12:00:25 138
原创 LeetCode 230. Kth Smallest Element in a BST
Description:Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Solution:常规的二叉树遍历即可public int kthSmallest(TreeNode root, int k) { Stack<TreeNode> s = new Stack<>(); while(
2020-05-26 11:29:48 142
原创 LeetCode 98. Validate Binary Search Tree
Description:Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node’s key.The right subtree of a node contains only nodes wi
2020-05-26 11:21:32 119
原创 LeetCode 1116. Print Zero Even Odd
Description:Suppose you are given the following code:class ZeroEvenOdd {public ZeroEvenOdd(int n) { … } // constructorpublic void zero(printNumber) { … } // only output 0’spublic void even(printNumber) { … } // only output even numberspublic v
2020-05-25 16:27:25 271
原创 JAVA Synchronized
参考 / References :https://blog.csdn.net/mulanlong/article/details/84566016最近刷leetcode时刷到了concurrent题目,所以深入学习下synchronized1.概念synchronized可以修饰代码块,方法,静态类和类2.修饰代码块:public class SyncThread implements Runnable { private static int count; public Sy
2020-05-25 16:25:40 162
原创 LeetCode 25. Reverse Nodes in k-Group
Description:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out n
2020-05-24 22:18:24 115
原创 LeetCode 92. Reverse Linked List II
Description:Reverse a linked list from position m to n. Do it in one-pass.Note: 1 ≤ m ≤ n ≤ length of list.Example:Input: 1->2->3->4->5->NULL, m = 2, n = 4Output: 1->4->3->2->5->NULLSolution:这题就是部分翻转,此题还有进阶版:以 k 为间隔进行 k
2020-05-24 19:39:24 123
原创 LeetCode 206. Reverse Linked List
Description:Reverse a singly linked list.Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULLSolution:即使是这么简单的题目,没有草稿纸没用 debug 我确实还是会犯错误。一开始下面那个 insert 函数我没有第一行,这样会导致第一个插入末尾的节点后面指向 null,所以在 reverseList 函数中第二个fo
2020-05-24 18:50:58 106
原创 LeetCode 2. Add Two Numbers
Description:You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.You may assume the two
2020-05-22 15:52:30 132
原创 LeetCode 67. Add Binary
Description:Given two binary strings, return their sum (also a binary string).The input strings are both non-empty and contains only characters 1 or 0.Example 1:Input: a = “11”, b = “1”Output: “100”Solution:class Solution { public String addBina
2020-05-22 15:41:29 110
原创 LeetCode 66. Plus One
Description:Given a non-empty array of digits representing a non-negative integer, plus one to the integer.The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.You ma
2020-05-22 14:20:28 116
原创 LeetCode 53. Maximum Subarray
Description:Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.Example:Input: [-2,1,-3,4,-1,2,1,-5,4],Output: 6Explanation: [4,-1,2,1] has the largest sum = 6.Soluti
2020-05-21 05:43:54 137
原创 LeetCode 58. Length of Last Word
Description:Given a string s consists of upper/lower-case alphabets and empty space characters ’ ', return the length of last word (last word means the last appearing word if we loop from left to right) in the string.If the last word does not exist, retu
2020-05-21 04:37:00 109
原创 LeetCode 1115. Print FooBar Alternately
Description:Suppose you are given the following code:class FooBar {public void foo() {for (int i = 0; i < n; i++) {print(“foo”);}}public void bar() {for (int i = 0; i < n; i++) {print(“bar”);}}}The same instance of FooBar will be passed
2020-05-20 21:12:41 167
原创 LeetCode 35. Search Insert Position
Description:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.Input: [1,3,5,6], 7Output: 4Input: [1,3,5,6
2020-05-19 10:34:46 107
原创 LeetCode 922. Sort Array By Parity II
Description:Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even.Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even.You may return any answer array that
2020-05-18 23:39:37 157
原创 LeetCode 27. Remove Element
Description:Given an array nums and a value val, remove all instances of that value in-place and return the new length.Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.The order
2020-05-18 23:09:48 167
原创 LeetCode 26. Remove Duplicates from Sorted Array
Description:Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra
2020-05-18 23:01:25 108
原创 LeetCode 47. Permutations II
Description:Given a collection of numbers that might contain duplicates, return all possible unique permutations.Example:Input: [1,1,2]Output:[[1,1,2],[1,2,1],[2,1,1]]Solution:和leetcode 46思路一样,最后用hashset去个重。(虽然去重可以直接在list遍历中去了,但是摸个鱼,直接最后改,坏处是时间开
2020-05-17 09:32:58 137
原创 LeetCode 46. Permutations
Description:Given a collection of distinct integers, return all possible permutations.Example:Input: [1,2,3]Output:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]Solution:思路:考虑到单链表的数据结构,排列出所有组合,即意味着:新数字对每个节点做插入尝试,最后返回结果集合。比如输入1,2;那么先插入1;然后在
2020-05-17 09:25:52 118
原创 LeetCode 1114. Print in Order
Description:Suppose we have a class:public class Foo {public void first() { print(“first”); }public void second() { print(“second”); }public void third() { print(“third”); }}The same instance of Foo will be passed to three different threads. Thread
2020-05-16 11:05:11 404
原创 LeetCode 705. Design HashSet
Description:Design a HashSet without using any built-in hash table libraries.To be specific, your design should include these functions:add(value): Insert a value into the HashSet.contains(value) : Return whether the value exists in the HashSet or not.
2020-05-16 10:32:31 215
原创 LeetCode 706. Design HashMap
Description:Design a HashMap without using any built-in hash table libraries.To be specific, your design should include these functions:put(key, value) : Insert a (key, value) pair into the HashMap. If the value already exists in the HashMap, update the
2020-05-16 09:03:12 179
原创 LeetCode 226. Invert Binary Tree
Description:Invert a binary tree.Solution:敲黑板,重点:homebrew创始人(90%谷歌员工用它写代码),去谷歌面试,结果不能在白板上写出翻转二叉树,然后喜提RejGoogle: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.先是递归算法:public
2020-05-15 18:59:04 147
原创 LeetCode 237. Delete Node in a Linked List
Description:Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Given linked list – head = [4,5,1,9], which looks like following:Example 1:Input: head = [4,5,1,9], node = 5Output: [4,1,9]Explana
2020-05-15 18:34:48 138
原创 LeetCode 169. Majority Element
Description:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element always exist in the array.Example 1:Input: [3,
2020-05-15 18:08:09 108
原创 LeetCode 136. Single Number
Description:Given a non-empty array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?Example 1:Input: [2,2,1]O
2020-05-15 17:45:54 137
原创 LeetCode 140. Word Break II
**Description: **Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences.Note:The same word in the
2020-05-15 16:09:22 139
原创 LeetCode 139. Word Break
Description:Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.Note:The same word in the dictionary may be reused mul
2020-05-15 12:23:27 140
原创 LeetCode 309. Best Time to Buy and Sell Stock with Cooldown
dp[i] refers to the max profit at prices[i]temp refers to the min buy price before the point of i (I mean we can see all the transactions before this point as one transaction)Due to cooldown, temp = Math.min(temp, prices[j] - dp[j-2]), however, the seco.
2020-05-15 10:07:16 206 1
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人