摘要:
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). F 阅读全文
随笔档案-2018年08月
[LeetCode] 153. Find Minimum in Rotated Sorted Array_Medium tag: Binary Search
2018-08-31 06:53 by Johnson_强生仔仔, 264 阅读, 收藏, 编辑
摘要:
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). F 阅读全文
[LeetCode] 69. Sqrt(x)_Easy tag: Binary Search
2018-08-31 06:35 by Johnson_强生仔仔, 198 阅读, 收藏, 编辑
摘要:
Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a non-negative integer. Since the return type is an in 阅读全文
[LeetCode] 240. Search a 2D Matrix II_Medium tag: Binary Search
2018-08-31 06:21 by Johnson_强生仔仔, 280 阅读, 收藏, 编辑
摘要:
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted i 阅读全文
[LeetCode] 74. Search a 2D Matrix_Medium tag: Binary Search
2018-08-30 11:08 by Johnson_强生仔仔, 233 阅读, 收藏, 编辑
摘要:
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted f 阅读全文
[LeetCode] 162. Find Peak Element(852. Peak Index in a Mountain Array)_Medium tag: Binary Search
2018-08-29 11:39 by Johnson_强生仔仔, 192 阅读, 收藏, 编辑
摘要:
A peak element is an element that is greater than its neighbors. Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return 阅读全文
[LeetCode] 278. First Bad Version_Easy tag: Binary Search
2018-08-29 11:19 by Johnson_强生仔仔, 151 阅读, 收藏, 编辑
摘要:
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality c 阅读全文
[LeetCode] 35. Search Insert Position_Easy tag: Binary Search
2018-08-29 10:34 by Johnson_强生仔仔, 213 阅读, 收藏, 编辑
摘要:
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 or 阅读全文
[LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search
2018-08-29 10:06 by Johnson_强生仔仔, 267 阅读, 收藏, 编辑
摘要:
Description Given a sorted array of n integers, find the starting and ending position of a given target value. If the target is not found in the array 阅读全文
[LeetCode] 374. Guess Number Higher or Lower_Easy tag: Binary Search
2018-08-29 07:22 by Johnson_强生仔仔, 214 阅读, 收藏, 编辑
摘要:
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wron 阅读全文
[LeetCode] 704. Binary Search_Easy tag: Binary Search
2018-08-29 06:52 by Johnson_强生仔仔, 220 阅读, 收藏, 编辑
摘要:
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, 阅读全文
[LeetCode] questions conclusion_ Binary Search
2018-08-29 06:49 by Johnson_强生仔仔, 343 阅读, 收藏, 编辑
摘要:
Binary Search T(n) = T(n/2) + O(1) => T(n) = O(lg n) proof: 如果能用iterable , 就用while loop, 可以防止用recursion的时候stack overflow( process in Linux is 8Mb), st 阅读全文
[LeetCode] 610. Triangle Judgement_Easy tag: SQL
2018-08-28 23:59 by Johnson_强生仔仔, 380 阅读, 收藏, 编辑
摘要:
A pupil Tim gets homework to identify whether three line segments could possibly form a triangle. However, this assignment is very heavy because there 阅读全文
[LeetCode] 613. Shortest Distance in a Line_Easy tag: SQL
2018-08-28 23:53 by Johnson_强生仔仔, 393 阅读, 收藏, 编辑
摘要:
Table point holds the x coordinate of some points on x-axis in a plane, which are all integers. Write a query to find the shortest distance between tw 阅读全文
[LeetCode] 422. Valid Word Square_Easy
2018-08-28 23:42 by Johnson_强生仔仔, 267 阅读, 收藏, 编辑
摘要:
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a valid word square if the kth row and column read th 阅读全文
[LeetCode] 492. Construct the Rectangle_Easy tag: Math
2018-08-28 23:20 by Johnson_强生仔仔, 171 阅读, 收藏, 编辑
摘要:
For a web developer, it is very important to know how to design a web page's size. So, given a specific rectangular web page’s area, your job by now i 阅读全文
[LeetCode] 619. Biggest Single Number_Easy tag: SQL
2018-08-28 23:13 by Johnson_强生仔仔, 298 阅读, 收藏, 编辑
摘要:
Table number contains many numbers in column num including duplicated ones.Can you write a SQL query to find the biggest number, which only appears on 阅读全文
[LeetCode] 584. Find Customer Referee_Easy tag: SQL
2018-08-28 23:04 by Johnson_强生仔仔, 376 阅读, 收藏, 编辑
摘要:
Given a table customer holding customers information and the referee. + + + + | id | name | referee_id| + + + + | 1 | Will | NULL | | 2 | Jane | NULL 阅读全文
[LeetCode] 603. Consecutive Available Seats_Easy tag: SQL
2018-08-28 21:57 by Johnson_强生仔仔, 638 阅读, 收藏, 编辑
摘要:
Several friends at a cinema ticket office would like to reserve consecutive available seats.Can you help to query all the consecutive available seats 阅读全文
[LeetCode] 607. Sales Person_Easy tag: SQL
2018-08-28 21:40 by Johnson_强生仔仔, 629 阅读, 收藏, 编辑
摘要:
Description Given three tables: salesperson, company, orders.Output all the names in the table salesperson, who didn’t have sales to company 'RED'. Ex 阅读全文
[LeetCode] 506. Relative Ranks_Easy tag: Sort
2018-08-28 12:23 by Johnson_强生仔仔, 149 阅读, 收藏, 编辑
摘要:
Given scores of N athletes, find their relative ranks and the people with the top three highest scores, who will be awarded medals: "Gold Medal", "Sil 阅读全文
[LeetCode] 747. Largest Number At Least Twice of Others_Easy
2018-08-28 12:08 by Johnson_强生仔仔, 162 阅读, 收藏, 编辑
摘要:
In a given integer array nums, there is always exactly one largest element. Find whether the largest element in the array is at least twice as much as 阅读全文
[LeetCode] 840. Magic Squares In Grid_Easy
2018-08-28 12:02 by Johnson_强生仔仔, 325 阅读, 收藏, 编辑
摘要:
A 3 x 3 magic square is a 3 x 3 grid filled with distinct numbers from 1 to 9 such that each row, column, and both diagonals all have the same sum. Gi 阅读全文
[LeetCode] 405. Convert a Number to Hexadecimal_Easy tag: Bit Manipulation
2018-08-28 08:27 by Johnson_强生仔仔, 164 阅读, 收藏, 编辑
摘要:
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used. Note: Example 1: Example 2: 阅读全文
[LeetCode] 276. Paint Fence_Easy tag: Dynamic Programming
2018-08-28 08:14 by Johnson_强生仔仔, 235 阅读, 收藏, 编辑
摘要:
There is a fence with n posts, each post can be painted with one of the k colors. You have to paint all the posts such that no more than two adjacent 阅读全文
[LeetCode] 762. Prime Number of Set Bits in Binary Representation_Easy
2018-08-28 00:55 by Johnson_强生仔仔, 198 阅读, 收藏, 编辑
摘要:
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime number of set bits in their binary representation 阅读全文
[LeetCode] 693. Binary Number with Alternating Bits_Easy
2018-08-28 00:42 by Johnson_强生仔仔, 240 阅读, 收藏, 编辑
摘要:
Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. Example 1: Input: 5 O 阅读全文
[LeetCode] 434. Number of Segments in a String_Easy
2018-08-28 00:38 by Johnson_强生仔仔, 218 阅读, 收藏, 编辑
摘要:
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters. Please note that the string 阅读全文
[LeetCode] 872. Leaf-Similar Trees_Easy tag: DFS
2018-08-28 00:01 by Johnson_强生仔仔, 217 阅读, 收藏, 编辑
摘要:
Consider all the leaves of a binary tree. From left to right order, the values of those leaves form a leaf value sequence. For example, in the given t 阅读全文
[LeetCode] 598. Range Addition II_Easy tag: Math
2018-08-27 23:48 by Johnson_强生仔仔, 187 阅读, 收藏, 编辑
摘要:
做个基本思路可以用 brute force, 但时间复杂度较高. 因为起始值都为0, 所以肯定是左上角的重合的最小的长方形就是结果, 所以我们求x, y 的最小值, 最后返回x*y. Code T: O(m*n) S: O(1) 阅读全文
[LeetCode] 661. Image Smoother_Easy
2018-08-27 12:07 by Johnson_强生仔仔, 157 阅读, 收藏, 编辑
摘要:
Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the ave 阅读全文
[LeetCode] 20. Valid Parentheses_Easy tag: Stack
2018-08-25 00:55 by Johnson_强生仔仔, 225 阅读, 收藏, 编辑
摘要:
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: No 阅读全文
[LeetCode] 367. Valid Perfect Square_Easy tag:Math
2018-08-21 13:32 by Johnson_强生仔仔, 210 阅读, 收藏, 编辑
摘要:
Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library functio 阅读全文
[LeetCode] 868. Binary Gap_Easy
2018-08-20 10:43 by Johnson_强生仔仔, 209 阅读, 收藏, 编辑
摘要:
Given a positive integer N, find and return the longest distance between two consecutive 1's in the binary representation of N. If there aren't two co 阅读全文
[LeetCode] 577. Employee Bonus_Easy tag: SQL
2018-08-20 10:34 by Johnson_强生仔仔, 272 阅读, 收藏, 编辑
摘要:
Select all employee's name and bonus whose bonus is < 1000. Table:Employee Table: Bonus Example ouput: # 不能直接用JOIN, 相当于inner join, 如果bonus没有的, 那么就没有了, 阅读全文
[LeetCode] 860. Lemonade Change_Easy tag: Greedy
2018-08-20 10:21 by Johnson_强生仔仔, 217 阅读, 收藏, 编辑
摘要:
At a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and order one at a time (in the order specified by bil 阅读全文
[LeetCode] 859. Buddy Strings_Easy
2018-08-20 09:59 by Johnson_强生仔仔, 249 阅读, 收藏, 编辑
摘要:
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters in A so that the result equals B. Example 1: Input: 阅读全文
[LeetCode] 876. Middle of the Linked List_Easy tag: Linked List ** slow, fast pointers
2018-08-20 09:37 by Johnson_强生仔仔, 237 阅读, 收藏, 编辑
摘要:
Given a non-empty, singly linked list with head node head, return a middle node of linked list. If there are two middle nodes, return the second middl 阅读全文
[LeetCode] 788. Rotated Digits_Easy tag: **Dynamic Programming
2018-08-20 09:29 by Johnson_强生仔仔, 235 阅读, 收藏, 编辑
摘要:
基本思路建一个helper function, 然后从1-N依次判断是否为good number, 注意判断条件为没有3,4,7 的数字,并且至少有一个2,5,6,9, 否则的话数字就一样了, 比如88, 18等. Improve: 利用DP去判断, 时间和空间都能降为O(lgn) Code T: 阅读全文
[LeetCode] 620. Not Boring Movies_Easy tag: SQL
2018-08-20 09:17 by Johnson_强生仔仔, 218 阅读, 收藏, 编辑
摘要:
X city opened a new cinema, many people would like to go to this cinema. The cinema also gives out a poster indicating the movies’ ratings and descrip 阅读全文
[LeetCode] 408. Valid Word Abbreviation_Easy
2018-08-20 09:11 by Johnson_强生仔仔, 229 阅读, 收藏, 编辑
摘要:
Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as "word" contains o 阅读全文
[LeetCode] 504. Base 7_Easy tag: Math
2018-08-20 08:47 by Johnson_强生仔仔, 180 阅读, 收藏, 编辑
摘要:
Given an integer, return its base 7 string representation. Example 1: Example 2: Note: The input will be in range of [-1e7, 1e7]. 不停除以7, 然后把余数放到ans里面, 阅读全文
[LeetCode] 830. Positions of Large Groups_Easy tag: Two Pointers
2018-08-20 03:56 by Johnson_强生仔仔, 187 阅读, 收藏, 编辑
摘要:
In a string S of lowercase letters, these letters form consecutive groups of the same character. For example, a string like S = "abbxxxxzyy" has the g 阅读全文
[LeetCode] 586. Customer Placing the Largest Number of Orders_Easy tag;SQL
2018-08-20 03:36 by Johnson_强生仔仔, 842 阅读, 收藏, 编辑
摘要:
Query the customer_number from the orders table for the customer who has placed the largest number of orders. It is guaranteed that exactly one custom 阅读全文
[LeetCode] 824. Goat Latin_Easy
2018-08-20 03:14 by Johnson_强生仔仔, 138 阅读, 收藏, 编辑
摘要:
A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only. We would like to convert the 阅读全文
[LeetCode] 596. Classes More Than 5 Students_Easy tag:SQL
2018-08-20 03:03 by Johnson_强生仔仔, 181 阅读, 收藏, 编辑
摘要:
There is a table courses with columns: student and class Please list out all classes which have more than or equal to 5 students. For example, the tab 阅读全文
[LeetCode] 744. Find Smallest Letter Greater Than Target_Easy tag: Binary Search
2018-08-20 02:46 by Johnson_强生仔仔, 175 阅读, 收藏, 编辑
摘要:
Given a list of sorted characters letters containing only lowercase letters, and given a target letter target, find the smallest element in the list t 阅读全文
[LeetCode] 414. Third Maximum Number_Easy
2018-08-20 02:33 by Johnson_强生仔仔, 185 阅读, 收藏, 编辑
摘要:
Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexi 阅读全文
[LeetCode] 696. Count Binary Substrings_Easy
2018-08-20 02:09 by Johnson_强生仔仔, 254 阅读, 收藏, 编辑
摘要:
利用group, 将每个连着的0或者1计数并且append进入group里面, 然后再将group里面的两两比较, 得到min, 并且加入到ans即可. T: O(n) S: O(n) 比较好理解 improve: 思路相同, 用pre和cur去将space节省到O(1) Code 1) T: O( 阅读全文
[LeetCode] 709. To Lower Case_Easy
2018-08-20 01:47 by Johnson_强生仔仔, 228 阅读, 收藏, 编辑
摘要:
Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. Example 1: Input: "Hello" Output: "hello" 阅读全文
[LeetCode] 581. Shortest Unsorted Continuous Subarray_Easy tag: Sort, Stack
2018-08-20 01:37 by Johnson_强生仔仔, 198 阅读, 收藏, 编辑
摘要:
Given an integer array, you need to find one continuous subarray that if you only sort this subarray in ascending order, then the whole array will be 阅读全文
[LeetCode] 849. Maximize Distance to Closest Person_Easy tag: BFS
2018-08-19 05:35 by Johnson_强生仔仔, 223 阅读, 收藏, 编辑
摘要:
In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is empty. There is at least one empty seat, and at least 阅读全文
[LeetCode] 455. Assign Cookies_Easy tag: Sort
2018-08-19 05:10 by Johnson_强生仔仔, 248 阅读, 收藏, 编辑
摘要:
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a g 阅读全文
[LeetCode] 884. Uncommon Words from Two Sentences_Easy tag: Hash Table
2018-08-19 04:54 by Johnson_强生仔仔, 348 阅读, 收藏, 编辑
摘要:
We are given two sentences A and B. (A sentence is a string of space separated words. Each word consists only of lowercase letters.) A word is uncommo 阅读全文
[LeetCode] 566. Reshape the Matrix_Easy
2018-08-19 04:50 by Johnson_强生仔仔, 230 阅读, 收藏, 编辑
摘要:
In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. 阅读全文
[LeetCode] 643. Maximum Average Subarray I_Easy tag: Dynamic Programming(Sliding windows)
2018-08-18 12:38 by Johnson_强生仔仔, 241 阅读, 收藏, 编辑
摘要:
Given an array consisting of n integers, find the contiguous subarray of given length k that has the maximum average value. And you need to output the 阅读全文
[LeetCode] 441. Arranging Coins_Easy tag: Math
2018-08-18 12:13 by Johnson_强生仔仔, 193 阅读, 收藏, 编辑
摘要:
You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number 阅读全文
[LeetCode] 867. Transpose Matrix_Easy
2018-08-18 11:55 by Johnson_强生仔仔, 163 阅读, 收藏, 编辑
摘要:
Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped over it's main diagonal, switching the row and column ind 阅读全文
[LeetCode] 58. Length of Last Word_Easy tag: String
2018-08-18 11:38 by Johnson_强生仔仔, 150 阅读, 收藏, 编辑
摘要:
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word 阅读全文
[LeetCode] 182. Duplicate Emails_Easy tag: SQL
2018-08-18 11:19 by Johnson_强生仔仔, 125 阅读, 收藏, 编辑
摘要:
Write a SQL query to find all duplicate emails in a table named Person. + + + | Id | Email | + + + | 1 | a@b.com | | 2 | c@d.com | | 3 | a@b.com | + + 阅读全文
[LeetCode] 476. Number Complement_Easy tag: Bit Manipulation
2018-08-18 11:14 by Johnson_强生仔仔, 174 阅读, 收藏, 编辑
摘要:
这个题目思路就是比如101 的结果是010, 可以从111^101 来得到, 那么我们就需要知道刚好比101多一位的1000, 所以利用 while i <= num : i <<= 1, 去得到1000, 然后-1, 便得到111, 再跟num ^, 也就是异或即可. Code 阅读全文
[LeetCode] 844. Backspace String Compare_Easy tag: Stack, Two pointers
2018-08-18 10:52 by Johnson_强生仔仔, 262 阅读, 收藏, 编辑
摘要:
Given two strings S and T, return if they are equal when both are typed into empty text editors. # means a backspace character. Example 1: Input: S = 阅读全文
[LeetCode] 728. Self Dividing Numbers_Easy tag: Math
2018-08-18 06:03 by Johnson_强生仔仔, 172 阅读, 收藏, 编辑
摘要:
A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is a self-dividing number because 128 % 1 == 0, 128 阅读全文
[LeetCode] 197. Rising Temperature_Easy tag: SQL
2018-08-18 05:54 by Johnson_强生仔仔, 221 阅读, 收藏, 编辑
摘要:
Given a Weather table, write a SQL query to find all dates' Ids with higher temperature compared to its previous (yesterday's) dates. For example, ret 阅读全文
[LeetCode] 496. Next Greater Element I_Easy tag: Stack
2018-08-18 05:44 by Johnson_强生仔仔, 186 阅读, 收藏, 编辑
摘要:
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1' 阅读全文
[LeetCode] 225. Implement Stack using Queues_Easy tag: Design
2018-08-18 05:30 by Johnson_强生仔仔, 306 阅读, 收藏, 编辑
摘要:
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. pop() -- Removes the element on top of the stack. to 阅读全文
[LeetCode] 532. K-diff Pairs in an Array_Easy tag: Hash Table
2018-08-17 11:44 by Johnson_强生仔仔, 226 阅读, 收藏, 编辑
摘要:
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an inte 阅读全文
[LeetCode] 605. Can Place Flowers_Easy
2018-08-17 11:27 by Johnson_强生仔仔, 231 阅读, 收藏, 编辑
摘要:
Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they 阅读全文
[LeetCode] 183. Customers Who Never Order_Easy tag: SQL
2018-08-17 11:09 by Johnson_强生仔仔, 185 阅读, 收藏, 编辑
摘要:
Suppose that a website contains two tables, the Customers table and the Orders table. Write a SQL query to find all customers who never order anything 阅读全文
[LeetCode] 821. Shortest Distance to a Character_Easy tag: BFS
2018-08-17 10:58 by Johnson_强生仔仔, 241 阅读, 收藏, 编辑
摘要:
Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string. Example 1: Note 阅读全文
[LeetCode] 796. Rotate String_Easy **KMP
2018-08-17 10:40 by Johnson_强生仔仔, 189 阅读, 收藏, 编辑
摘要:
We are given two strings, A and B. A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, 阅读全文
[LeetCode] 168. Excel Sheet Column Title_Easy tag: Math
2018-08-17 10:02 by Johnson_强生仔仔, 277 阅读, 收藏, 编辑
摘要:
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: Example 1: Example 2: Example 3: 这个题目实际上就想用2 阅读全文
[LeetCode] 836. Rectangle Overlap_Easy
2018-08-17 06:01 by Johnson_强生仔仔, 212 阅读, 收藏, 编辑
摘要:
A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bottom-left corner, and (x2, y2) are the coordinates 阅读全文
[LeetCode] 345. Reverse Vowels of a String_Easy tag:Two Pointers
2018-08-17 03:37 by Johnson_强生仔仔, 189 阅读, 收藏, 编辑
摘要:
Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Example 2: Input: "leetcode" Output: "leotcede" Exam 阅读全文
[LeetCode] 196. Delete Duplicate Emails_Easy tag: SQL
2018-08-17 00:46 by Johnson_强生仔仔, 188 阅读, 收藏, 编辑
摘要:
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 | Ema 阅读全文
[LeetCode] 832. Flipping an Image_Easy
2018-08-17 00:35 by Johnson_强生仔仔, 230 阅读, 收藏, 编辑
摘要:
Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image. To flip an image horizontally means t 阅读全文
[LeetCode] 190. Reverse Bits_Easy tag: Bit Manipulation
2018-08-16 23:13 by Johnson_强生仔仔, 177 阅读, 收藏, 编辑
摘要:
Reverse bits of a given 32 bits unsigned integer. Example: Follow up:If this function is called many times, how would you optimize it? 这个题目就是利用Bit Man 阅读全文
[LeetCode] 243. Shortest Word Distance_Easy
2018-08-16 12:08 by Johnson_强生仔仔, 195 阅读, 收藏, 编辑
摘要:
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. Example:Assume that words = ["p 阅读全文
[LeetCode] 256. Paint House_Easy tag: Dynamic Programming
2018-08-16 11:17 by Johnson_强生仔仔, 211 阅读, 收藏, 编辑
摘要:
There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certai 阅读全文
[LeetCode] 172. Factorial Trailing Zeroes_Easy tag: Math
2018-08-16 10:46 by Johnson_强生仔仔, 199 阅读, 收藏, 编辑
摘要:
Given an integer n, return the number of trailing zeroes in n!. Example 1: Example 2: Note: Your solution should be in logarithmic time complexity. 思路 阅读全文
[LeetCode] 181. Employees Earning More Than Their Managers_Easy tag: SQL
2018-08-16 10:24 by Johnson_强生仔仔, 163 阅读, 收藏, 编辑
摘要:
The Employee table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id. Given the Em 阅读全文
[LeetCode] 252. Meeting Rooms_Easy tag: Sort
2018-08-16 10:11 by Johnson_强生仔仔, 207 阅读, 收藏, 编辑
摘要:
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all mee 阅读全文
[LeetCode] 232. Implement Queue using Stacks_Easy tag: Design
2018-08-16 09:49 by Johnson_强生仔仔, 241 阅读, 收藏, 编辑
摘要:
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front 阅读全文
[LeetCode] 724. Find Pivot Index_Easy tag: Dynamic Programming
2018-08-16 00:49 by Johnson_强生仔仔, 201 阅读, 收藏, 编辑
摘要:
Given an array of integers nums, write a method that returns the "pivot" index of this array. We define the pivot index as the index where the sum of 阅读全文
[LeetCode] 364. Nested List Weight Sum II_Medium tag:DFS
2018-08-16 00:21 by Johnson_强生仔仔, 270 阅读, 收藏, 编辑
摘要:
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list -- w 阅读全文
[LeetCode] 339. Nested List Weight Sum_Easy tag:DFS
2018-08-15 23:50 by Johnson_强生仔仔, 282 阅读, 收藏, 编辑
摘要:
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list -- w 阅读全文
[LeetCode] 628. Maximum Product of Three Numbers_Easy
2018-08-15 12:22 by Johnson_强生仔仔, 185 阅读, 收藏, 编辑
摘要:
Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Inp 阅读全文
[LeetCode] 438. Find All Anagrams in a String_Easy
2018-08-15 11:14 by Johnson_强生仔仔, 144 阅读, 收藏, 编辑
摘要:
438. Find All Anagrams in a String 438. Find All Anagrams in a String 438. Find All Anagrams in a String 438. Find All Anagrams in a String Descriptio 阅读全文
[LeetCode] 804. Unique Morse Code Words_Easy tag: Hash Table
2018-08-15 05:10 by Johnson_强生仔仔, 157 阅读, 收藏, 编辑
摘要:
International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a"maps to ".-", "b" maps 阅读全文
[LeetCode] 717. 1-bit and 2-bit Characters_Easy
2018-08-15 04:56 by Johnson_强生仔仔, 187 阅读, 收藏, 编辑
摘要:
We have two special characters. The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11). N 阅读全文
[LeetCode] 383. Ransom Note_Easy tag: Hash Table
2018-08-15 00:45 by Johnson_强生仔仔, 160 阅读, 收藏, 编辑
摘要:
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the rans 阅读全文
[LeetCode] 203. Remove Linked List Elements_Easy tag: Linked LIst
2018-08-15 00:38 by Johnson_强生仔仔, 154 阅读, 收藏, 编辑
摘要:
Remove all elements from a linked list of integers that have value val. Example: Input: 1->2->6->3->4->5->6, val = 6 Output: 1->2->3->4->5 Remove all 阅读全文
[LeetCode] 680. Valid Palindrome II_Easy tag: Two Pointers
2018-08-15 00:24 by Johnson_强生仔仔, 213 阅读, 收藏, 编辑
摘要:
Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome. Example 1: Example 2: Note: 题目思路就是正常的用tw 阅读全文
[LeetCode] 69. Sqrt(x)_Easy tag: Binary Search
2018-08-14 23:53 by Johnson_强生仔仔, 169 阅读, 收藏, 编辑
摘要:
Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a non-negative integer. Since the return type is an in 阅读全文
[LeetCode] 627. Swap Salary_Easy tag: SQL
2018-08-14 23:33 by Johnson_强生仔仔, 210 阅读, 收藏, 编辑
摘要:
Given a table salary, such as the one below, that has m=male and f=female values. Swap all f and m values (i.e., change all f values to m and vice ver 阅读全文
[LeetCode] 697. Degree of an Array_Easy tag: Hash Table
2018-08-13 05:46 by Johnson_强生仔仔, 142 阅读, 收藏, 编辑
摘要:
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the maximum frequency of any one of its elements. Your t 阅读全文
[LeetCode] 443. String Compression_Easy tag:String
2018-08-13 05:22 by Johnson_强生仔仔, 151 阅读, 收藏, 编辑
摘要:
Given an array of characters, compress it in-place. The length after compression must always be smaller than or equal to the original array. Every ele 阅读全文
[LeetCode] 415. Add Strings_Easy tag: String
2018-08-13 04:21 by Johnson_强生仔仔, 256 阅读, 收藏, 编辑
摘要:
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. Note: 这个题目跟[LeetCode] 67. Add Binary_Easy tag: S 阅读全文
[LeetCode] 67. Add Binary_Easy tag: String
2018-08-13 04:05 by Johnson_强生仔仔, 199 阅读, 收藏, 编辑
摘要:
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 阅读全文
[LeetCode] 557. Reverse Words in a String III_Easy tag: String
2018-08-13 03:39 by Johnson_强生仔仔, 216 阅读, 收藏, 编辑
摘要:
Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. E 阅读全文
[LeetCode] 176. Second Highest Salary_Easy tag: SQL
2018-08-12 05:28 by Johnson_强生仔仔, 180 阅读, 收藏, 编辑
摘要:
Write a SQL query to get the second highest salary from the Employee table. + + + | Id | Salary | + + + | 1 | 100 | | 2 | 200 | | 3 | 300 | + + + For 阅读全文
[LeetCode] 88. Merge Sorted Array_Easy tag: Two Pointers
2018-08-12 05:21 by Johnson_强生仔仔, 201 阅读, 收藏, 编辑
摘要:
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: The number of elements initialized in nums1 and num 阅读全文
[LeetCode] 258. Add Digits_Easy tag: Math
2018-08-11 11:59 by Johnson_强生仔仔, 160 阅读, 收藏, 编辑
摘要:
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. Example: Follow up:Could you do it without any lo 阅读全文
[LeetCode] 193. Valid Phone Numbers_Easy tag: Bash
2018-08-11 05:32 by Johnson_强生仔仔, 226 阅读, 收藏, 编辑
摘要:
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers. You may 阅读全文
[LeetCode] 38. Count and Say_Easy
2018-08-11 05:13 by Johnson_强生仔仔, 178 阅读, 收藏, 编辑
摘要:
The count-and-say sequence is the sequence of integers with the first five terms as following: 1 is read off as "one 1" or 11.11 is read off as "two 1 阅读全文
[LeetCode] 561. Array Partition I_Easy tag: Sort
2018-08-11 04:39 by Johnson_强生仔仔, 213 阅读, 收藏, 编辑
摘要:
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of 阅读全文
[LeetCode] 595. Big Countries_Easy tag: SQL
2018-08-11 04:28 by Johnson_强生仔仔, 226 阅读, 收藏, 编辑
摘要:
There is a table World A country is big if it has an area of bigger than 3 million square km or a population of more than 25 million. Write a SQL solu 阅读全文
[LeetCode] 195. Tenth Line_Easy tag: Bash
2018-08-11 04:21 by Johnson_强生仔仔, 215 阅读, 收藏, 编辑
摘要:
Given a text file file.txt, print just the 10th line of the file. Example: Assume that file.txt has the following content: Line 1 Line 2 Line 3 Line 4 阅读全文
[LeetCode] 237. Delete Node in a Linked List_Easy tag: Linked List
2018-08-11 00:44 by Johnson_强生仔仔, 220 阅读, 收藏, 编辑
摘要:
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], wh 阅读全文
[LeetCode] 458. Poor Pigs_Easy tag: Math
2018-08-10 23:59 by Johnson_强生仔仔, 147 阅读, 收藏, 编辑
摘要:
There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. They all look the same. If a pig drinks that poison 阅读全文
[LeetCode] 292. Nim Game_Easy tag: Math
2018-08-10 23:19 by Johnson_强生仔仔, 216 阅读, 收藏, 编辑
摘要:
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 ston 阅读全文
[LeetCode] 1. Two Sum_Easy tag: Hash Table
2018-08-10 12:16 by Johnson_强生仔仔, 263 阅读, 收藏, 编辑
摘要:
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have ex 阅读全文
[LeetCode] 559. Maximum Depth of N-ary Tree_Easy tag: DFS
2018-08-09 09:00 by Johnson_强生仔仔, 269 阅读, 收藏, 编辑
摘要:
Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest le 阅读全文
[LeetCode] 310. Minimum Height Trees_Medium tag: BFS
2018-08-09 04:31 by Johnson_强生仔仔, 183 阅读, 收藏, 编辑
摘要:
For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible roote 阅读全文
[LeetCode] 787. Cheapest Flights Within K Stops_Medium tag: Dynamic Programming, BFS, Heap
2018-08-09 00:00 by Johnson_强生仔仔, 265 阅读, 收藏, 编辑
摘要:
There are n cities connected by m flights. Each fight starts from city u and arrives at v with a price w. Now given all the cities and fights, togethe 阅读全文
[LeetCode] 490. The Maze_Medium tag: BFS/DFS
2018-08-08 04:34 by Johnson_强生仔仔, 356 阅读, 收藏, 编辑
摘要:
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right, but it won't stop roll 阅读全文
[LeetCode] 129. Sum Root to Leaf Numbers_Medium tag: DFS
2018-08-08 00:40 by Johnson_强生仔仔, 284 阅读, 收藏, 编辑
摘要:
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 whic 阅读全文
[LeetCode] 78. Subsets tag: backtracking
2018-08-08 00:26 by Johnson_强生仔仔, 270 阅读, 收藏, 编辑
摘要:
Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solution set must not contain duplicate subsets. Exampl 阅读全文
[LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS
2018-08-08 00:09 by Johnson_强生仔仔, 248 阅读, 收藏, 编辑
摘要:
Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in 阅读全文
[LeetCode] 99. Recover Binary Search Tree_Hard_Inorder traversal
2018-08-07 23:37 by Johnson_强生仔仔, 200 阅读, 收藏, 编辑
摘要:
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Example 1: Input: [1,3,null,null,2 阅读全文
[LeetCode] 100. Same Tree_Easy tag: DFS
2018-08-07 04:31 by Johnson_强生仔仔, 187 阅读, 收藏, 编辑
摘要:
Given two binary trees, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally ident 阅读全文
[LeetCode] All questions numbers conclusion 所有题目题号
2018-08-07 00:50 by Johnson_强生仔仔, 520 阅读, 收藏, 编辑
摘要:
Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, DFS LeetCode questions conclustion_Path in Tree [L 阅读全文
[LeetCode] 110. Balanced Binary Tree_Easy tag: DFS
2018-08-07 00:49 by Johnson_强生仔仔, 227 阅读, 收藏, 编辑
摘要:
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: a binary tree in which the dep 阅读全文
[LeetCode] questions conclusion_ Dynamic Programming
2018-08-06 23:49 by Johnson_强生仔仔, 337 阅读, 收藏, 编辑
摘要:
Questions: [LeetCode] 198. House Robber _Easy tag: Dynamic Programming [LeetCode] 221. Maximal Square _ Medium Tag: Dynamic Programming [LeetCode] 62. 阅读全文
[LeetCode] 111. Minimum Depth of Binary Tree_Easy tag:DFS
2018-08-06 23:19 by Johnson_强生仔仔, 207 阅读, 收藏, 编辑
摘要:
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest l 阅读全文
[LeetCode] 122. Best Time to Buy and Sell Stock II_Easy tag: Dynamic Programming
2018-08-03 23:51 by Johnson_强生仔仔, 264 阅读, 收藏, 编辑
摘要:
Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may comple 阅读全文
[LeetCode] 121. Best Time to Buy and Sell Stock_Easy tag: Dynamic Programming
2018-08-03 23:27 by Johnson_强生仔仔, 220 阅读, 收藏, 编辑
摘要:
Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction 阅读全文
[LeetCode] 674. Longest Continuous Increasing Subsequence_Easy Dynamic Programming
2018-08-02 09:18 by Johnson_强生仔仔, 237 阅读, 收藏, 编辑
摘要:
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray). Example 1: Example 2: Note: Length of th 阅读全文
[LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming
2018-08-02 05:46 by Johnson_强生仔仔, 227 阅读, 收藏, 编辑
摘要:
Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. Example 1: I 阅读全文
[LeetCode] 53. Maximum Subarray_Easy tag: Dynamic Programming
2018-08-02 04:39 by Johnson_强生仔仔, 325 阅读, 收藏, 编辑
摘要:
Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. Example: 3.2) 阅读全文
[LeetCode] 312. Burst Balloons_hard tag: 区间Dynamic Programming
2018-08-01 05:07 by Johnson_强生仔仔, 298 阅读, 收藏, 编辑
摘要:
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by array nums. You are asked to burst all the balloon 阅读全文
[LeetCode] 877. Stone Game == [LintCode] 396. Coins in a Line 3_hard tag: 区间Dynamic Programming, 博弈
2018-08-01 01:24 by Johnson_强生仔仔, 859 阅读, 收藏, 编辑
摘要:
Alex and Lee play a game with piles of stones. There are an even number of piles arranged in a row, and each pile has a positive integer number of sto 阅读全文