LeetCode从零单刷
文章平均质量分 72
以初学者角度,逐题解析每道LeetCode编程题,提供思路与源码。源码实现:C++
IronYoung
字节跳动 AI Lab | 图形图像 | 机器学习 | weibo:@iamironyoung
展开
-
【LeetCode从零单刷】Bulb Switcher
题目:There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turni原创 2015-12-19 16:50:27 · 9098 阅读 · 0 评论 -
【LeetCode从零单刷】Convert Sorted List to Binary Search Tree
题目:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.解答:将一段有序序列转换为平衡二叉树,重要是递归。即中点作为根节点,小于部分作为左子树递归,大于部分作为右子树递归。这里的单链表给 “找中点” 造成了难题,原创 2015-11-28 21:42:54 · 949 阅读 · 0 评论 -
【LeetCode从零单刷】Reverse Bits
题目:Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as001原创 2015-11-28 21:19:28 · 1574 阅读 · 0 评论 -
【LeetCode从零单刷】Merge Sorted Array
题目:Given two sorted integer arrays nums1 and nums2, merge nums2 intonums1 as one sorted array.Note:You may assume that nums1 has enough space (size that is greater or equal tom + n) to hol原创 2015-11-28 21:07:10 · 1308 阅读 · 1 评论 -
【LeetCode从零单刷】Kth Largest Element in an Array
题目:Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,Given [3,2,1,5,6,4] and k = 2, retur原创 2015-11-28 20:55:39 · 1425 阅读 · 1 评论 -
【LeetCode从零单刷】Minimum Depth of Binary Tree
题目: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 leaf node.解答:非常无聊的一题。需要注意的是:叶子节点是自身不为 N原创 2015-11-28 20:24:35 · 1379 阅读 · 1 评论 -
【LeetCode从零单刷】Perfect Squares
题目:Given a positive integer n, find the least number of perfect square numbers (for example,1, 4, 9, 16, ...) which sum to n.For example, given n = 12, return 3 because12 = 4 + 4 + 4; given原创 2015-11-28 19:38:52 · 2127 阅读 · 2 评论 -
【LeetCode从零单刷】Different Ways to Add Parentheses
题目:Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are+, - and *.Example原创 2015-11-15 22:20:07 · 906 阅读 · 0 评论 -
【LeetCode从零单刷】Intersection of Two Linked Lists
题目:Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists: A: a1 → a2 ↘原创 2015-11-13 17:54:49 · 1421 阅读 · 0 评论 -
【LeetCode从零单刷】Factorial Trailing Zeroes
题目:Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.解答:n 的阶乘末尾有多少个 0?其实也就是询问结果是10的多少倍数。10 = 2 * 5,因此对每个乘数进行因式分解就好。原创 2015-11-05 19:07:33 · 891 阅读 · 0 评论 -
【LeetCode从零单刷】Longest Increasing Subsequence
题目:Given an unsorted array of integers, find the length of longest increasing subsequence.For example, Given [10, 9, 2, 5, 3, 7, 101, 18],The longest increasing subsequence is [2, 3, 7, 101],原创 2015-11-05 16:45:31 · 3178 阅读 · 0 评论 -
【LeetCode从零单刷】Longest Consecutive Sequence
题目:Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1,原创 2015-11-04 10:07:34 · 1479 阅读 · 0 评论 -
【LeetCode从零单刷】Search in Rotated Sorted Array I & II
I 题目:Suppose a sorted array 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).You are given a target value to search. If found in the array re原创 2015-11-02 23:18:12 · 1514 阅读 · 0 评论 -
【LeetCode从零单刷】Game of Life
题目:According to the Wikipedia's article: "The Game of Life, also known simply asLife, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."Given a board w原创 2015-11-02 21:53:00 · 1476 阅读 · 0 评论 -
【LeetCode从零单刷】Remove Duplicates from Sorted Array I & II
I 题目:Given a sorted array, remove the duplicates in place such that each element appear onlyonce and return the new length.Do not allocate extra space for another array, you must do this in plac原创 2015-11-02 21:29:36 · 1409 阅读 · 0 评论 -
【LeetCode从零单刷】House Robber
题目:You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacen原创 2015-11-02 21:03:07 · 1593 阅读 · 0 评论 -
【LeetCode从零单刷】Binary Tree Level Order Traversal I & II
题目:Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9原创 2015-11-01 10:53:39 · 797 阅读 · 0 评论 -
【LeetCode从零单刷】H-index I & II
题目:Given an array of citations (each citation is a non-negative integer) of a researcher, write a function to compute the researcher's h-index.According to the definition of h-index on Wikip原创 2015-11-01 10:25:25 · 926 阅读 · 0 评论 -
【LeetCode从零单刷】Set Matrix Zeroes
题目:Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.Follow up:Did you use extra space?A straight forward solution using O(mn) space is probabl原创 2015-11-01 09:38:57 · 1565 阅读 · 0 评论 -
【LeetCode从零单刷】Combinations & Combination Sum 系列
题目:Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3],原创 2015-10-31 22:35:41 · 2658 阅读 · 0 评论 -
【LeetCode从零单刷】Search a 2D Matrix I & II
题目: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 from left to right.The first intege原创 2015-10-30 11:10:10 · 1260 阅读 · 2 评论 -
【LeetCode从零单刷】Kth Smallest Element in a BST
题目:Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow up:Wh原创 2015-10-30 10:36:37 · 1512 阅读 · 2 评论 -
【LeetCode从零单刷】Symmetric Tree
题目:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3原创 2015-10-29 22:02:12 · 1254 阅读 · 2 评论 -
【LeetCode从零单刷】Balanced Binary Tree
题目: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 depth of the two subtrees of every node ne原创 2015-10-14 21:18:57 · 1459 阅读 · 0 评论 -
【LeetCode从零单刷】Container With Most Water
题目:Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and原创 2015-10-13 23:35:59 · 732 阅读 · 0 评论 -
【LeetCode从零单刷】Find Peak Element
题目:A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple peaks原创 2015-10-14 20:53:05 · 1451 阅读 · 0 评论 -
【LeetCode从零单刷】Rotate Image
题目:You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?解答:一开始想的很复杂,希望找到一个通式应对所有角度的旋转。所有的细节写在《【图像处原创 2015-10-13 22:13:56 · 1589 阅读 · 0 评论 -
【LeetCode从零单刷】Spiral Matrix II
题目:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9原创 2015-10-13 21:49:18 · 1346 阅读 · 0 评论 -
【LeetCode从零单刷】Nim Game
【LeetCode从零单刷】Nim Game原创 2015-10-13 16:42:16 · 9346 阅读 · 0 评论 -
【LeetCode从零单刷】Permutations
LeetCode 从零单刷原创 2015-10-13 09:39:04 · 719 阅读 · 0 评论 -
【LeetCode从零单刷】Swap Nodes in Pairs
LeetCode从零单刷原创 2015-10-11 10:33:38 · 781 阅读 · 0 评论 -
【LeetCode从零单刷】Ugly Number I, II & Super Ugly Number
LeetCode从零单刷原创 2015-10-11 10:17:06 · 4746 阅读 · 0 评论 -
【LeetCode从零单刷】Find the Duplicate Number
题目:Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate n原创 2015-10-10 09:51:21 · 3333 阅读 · 0 评论 -
【LeetCode从零单刷】Majority Element
题目:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.原创 2015-10-10 09:10:59 · 1177 阅读 · 0 评论 -
【LeetCode从零单刷】Move Zeroes
题目:Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after ca原创 2015-10-10 08:50:57 · 1338 阅读 · 0 评论 -
【LeetCode从零单刷】Sort Colors
题目:Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the intege原创 2015-09-21 19:38:17 · 758 阅读 · 0 评论 -
【LeetCode从零单刷】Generate Parentheses
菜鸡从零单刷 LeetCode 系列!原创 2015-09-18 21:05:55 · 1030 阅读 · 0 评论 -
【LeetCode从零单刷】Reverse Linked List
题目:Reverse a singly linked list.解答:反转单链表。很常见的一道题目。思路暂时想到两种,一种是浪费一些空间,重新构造一个反转的链表,代码是这种;第二种是每次单独保存原链表节点,使得其 next 指针指向原先的上一个节点。主要我想说的是代码上的问题:关于 struct, class 类型的指针。使用指针之前,切记要分配内存空间!!原创 2015-09-16 21:37:21 · 682 阅读 · 0 评论 -
【LeetCode从零单刷】Binary Tree Postorder Traversal
从今天起,模仿《从零单排》系列,菜鸡单刷LeetCode!原创 2015-09-16 14:07:49 · 694 阅读 · 0 评论 -
【LeetCode从零单刷】Add Digits
题目:Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 h原创 2015-09-15 09:18:57 · 711 阅读 · 0 评论