
LeetCode
墨染百城
目前专注于数据分析、大数据计算方面的学习
展开
-
【LeetCode】LeetCode中那些应该背下来的经典代码
收集在刷LeetCode过程中遇到的经典代码,持续更新。136. Single Number中使用异或求解问题描述问题链接:https://leetcode.com/problems/single-number/#/descriptionGiven an array of integers, every element appears twice except for one. Find that原创 2017-03-17 10:52:10 · 2400 阅读 · 1 评论 -
【LeetCode】从LeetCode中学到的常用Trick
昨天开始刷LeetCode,在这个过程中学到了不少Trick,为了方便复习,特地整理出来。List转String[]示例代码:List<String> wordList = new ArrayList<String>();String[] results = new String[wordList.size()];wordList.toArray(results);获得char在String中原创 2017-03-15 11:25:14 · 278 阅读 · 0 评论 -
【LeetCode】496. Next Greater Element I
问题描述问题链接:https://leetcode.com/problems/next-greater-element-i/#/descriptionYou are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next gr原创 2017-03-15 23:19:47 · 1810 阅读 · 0 评论 -
【LeetCode】344. Reverse String
问题描述问题链接:https://leetcode.com/problems/reverse-string/#/descriptionWrite a function that takes a string as input and returns the string reversed.Example:Given s = "hello", return "olleh".我的代码探索过程这么简单的原创 2017-03-15 22:04:38 · 293 阅读 · 0 评论 -
【LeetCode】412. Fizz Buzz
问题描述问题链接:https://leetcode.com/problems/fizz-buzz/#/descriptionWrite a program that outputs the string representation of numbers from 1 to n.But for multiples of three it should output “Fizz” instead of原创 2017-03-15 21:42:11 · 285 阅读 · 0 评论 -
【LeetCode】121. Best Time to Buy and Sell Stock
问题描述问题链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock/#/descriptionSay 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原创 2017-03-25 13:16:37 · 399 阅读 · 0 评论 -
【LeetCode】常用算法之动态规划
一直很想学习动态规划算法,虽然也在课上听老师讲过背包问题啊,旅行商问题啊,但是都是懵懵懂懂的匆匆而过,一直也没有学会。这次刷LeetCode也给了我一个机会来弥补之前的遗憾。参考资料http://blog.youkuaiyun.com/baidu_28312631/article/details/47418773http://www.hawstein.com/posts/dp-novice-to-advance原创 2017-03-25 11:04:36 · 639 阅读 · 0 评论 -
【LeetCode】338. Counting Bits
问题描述问题链接:https://leetcode.com/problems/counting-bits/#/descriptionGiven a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary repre原创 2017-03-15 21:27:47 · 1305 阅读 · 0 评论 -
【LeetCode】419. Battleships in a Board
问题描述问题链接:https://leetcode.com/problems/battleships-in-a-board/#/descriptionGiven an 2D board, count how many battleships are in it. The battleships are represented with ‘X’s, empty slots are represente原创 2017-03-15 20:16:20 · 392 阅读 · 0 评论 -
【LeetCode】463. Island Perimeter
问题描述问题链接:https://leetcode.com/problems/island-perimeter/#/descriptionYou are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connect原创 2017-03-16 15:10:35 · 391 阅读 · 0 评论 -
【LeetCode】485. Max Consecutive Ones
问题描述问题链接:https://leetcode.com/problems/max-consecutive-ones/#/descriptionGiven a binary array, find the maximum number of consecutive 1s in this array.Example 1:Input: [1,1,0,1,1,1]Output: 3Explanati原创 2017-03-16 17:25:46 · 274 阅读 · 0 评论 -
【LeetCode】53. Maximum Subarray
问题描述问题链接:https://leetcode.com/problems/maximum-subarray/#/descriptionFind the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array原创 2017-03-27 11:25:34 · 612 阅读 · 0 评论 -
【LeetCode】520. Detect Capital
问题描述问题链接:https://leetcode.com/problems/single-number/#/descriptionGiven a word, you need to judge whether the usage of capitals in it is right or not.We define the usage of capitals in a word to be rig原创 2017-03-17 11:08:33 · 1433 阅读 · 0 评论 -
【LeetCode】136. Single Number
问题描述问题链接:https://leetcode.com/problems/single-number/#/descriptionGiven an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear r原创 2017-03-17 10:50:32 · 799 阅读 · 0 评论 -
【LeetCode】413. Arithmetic Slices
问题描述问题链接:https://leetcode.com/problems/arithmetic-slices/#/descriptionA sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecuti原创 2017-03-17 10:14:58 · 355 阅读 · 0 评论 -
【LeetCode】258. Add Digits
问题描述问题链接:https://leetcode.com/problems/add-digits/#/descriptionGiven a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the proces原创 2017-04-03 12:54:48 · 618 阅读 · 0 评论 -
【LeetCode】406. Queue Reconstruction by Height
问题描述问题链接:https://leetcode.com/problems/queue-reconstruction-by-height/#/descriptionSuppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), w原创 2017-03-16 20:46:00 · 387 阅读 · 0 评论 -
【LeetCode】513. Find Bottom Left Tree Value
问题描述问题链接:https://leetcode.com/problems/find-bottom-left-tree-value/#/descriptionGiven a binary tree, find the leftmost value in the last row of the tree.Example 1:Input: 2 / \ 1 3Output:1Ex原创 2017-03-16 17:27:58 · 1313 阅读 · 0 评论 -
【LeetCode】292. Nim Game
问题描述问题链接:https://leetcode.com/problems/nim-game/#/descriptionYou 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原创 2017-03-16 17:26:57 · 368 阅读 · 0 评论 -
【LeetCode】500. Keyboard Row
问题描述问题链接:https://leetcode.com/problems/keyboard-row/#/descriptionGiven a List of words, return the words that can be typed using letters of alphabet on only one row’s of American keyboard like the imag原创 2017-03-15 11:24:15 · 785 阅读 · 0 评论 -
【LeetCode】476. Number Complement
问题描述问题链接:https://leetcode.com/problems/number-complement/#/descriptionGiven a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.No原创 2017-03-14 15:59:47 · 553 阅读 · 0 评论 -
【LeetCode】461. Hamming Distance
问题描述问题链接:https://leetcode.com/problems/hamming-distance/#/descriptionThe Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given two integer原创 2017-03-14 11:29:24 · 927 阅读 · 0 评论 -
【LeetCode】226. Invert Binary Tree
问题描述问题链接:https://leetcode.com/problems/invert-binary-tree/#/descriptionInvert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1Trivia:原创 2017-03-28 21:22:22 · 329 阅读 · 0 评论 -
【LeetCode】371. Sum of Two Integers
问题描述问题链接:https://leetcode.com/problems/sum-of-two-integers/#/descriptionCalculate the sum of two integers a and b, but you are not allowed to use the operator + and -。Example:Given a = 1 and b = 2, ret原创 2017-03-28 20:55:46 · 435 阅读 · 0 评论 -
【LeetCode】538. Convert BST to Greater Tree
问题描述问题链接:https://leetcode.com/problems/convert-bst-to-greater-tree/#/descriptionGiven a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the原创 2017-03-21 13:02:11 · 3776 阅读 · 1 评论 -
【LeetCode】442. Find All Duplicates in an Array
问题描述问题链接:https://leetcode.com/problems/binary-watch/#/descriptionGiven an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements th原创 2017-03-21 11:33:36 · 448 阅读 · 0 评论 -
【LeetCode】357. Count Numbers with Unique Digits
问题描述问题链接:https://leetcode.com/problems/count-numbers-with-unique-digits/#/descriptionGiven a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.Example:Given n = 2, retu原创 2017-03-21 09:30:24 · 311 阅读 · 0 评论 -
【LeetCode】401. Binary Watch
问题描述问题链接:https://leetcode.com/problems/binary-watch/#/descriptionA binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent the minutes (0-59).Each LE原创 2017-03-20 14:51:05 · 330 阅读 · 0 评论 -
【LeetCode】526. Beautiful Arrangement
问题描述问题链接:https://leetcode.com/problems/beautiful-arrangement/#/descriptionSuppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constructed by these N numbers s原创 2017-03-20 11:21:58 · 1059 阅读 · 0 评论 -
【LeetCode】448. Find All Numbers Disappeared in an Array
问题描述问题链接:https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/#/descriptionGiven an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others app原创 2017-03-19 10:15:07 · 920 阅读 · 0 评论 -
【LeetCode】529. Minesweeper
问题描述问题链接:https://leetcode.com/problems/minesweeper/#/descriptionLet’s play the minesweeper game (Wikipedia, online game)!You are given a 2D char matrix representing the game board. ‘M’ represents an un原创 2017-03-21 23:56:18 · 722 阅读 · 0 评论 -
【LeetCode】508. Most Frequent Subtree Sum
问题描述问题链接:https://leetcode.com/problems/most-frequent-subtree-sum/#/descriptionGiven the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined as the原创 2017-03-22 09:06:07 · 393 阅读 · 0 评论 -
【LeetCode】535. Encode and Decode TinyURL
这几天陆续听说同学们都在刷LeetCode,是面试必须的。听完我也坐不住了,决定开始刷起来。先在知乎看了一遍,大部分人都是按照AC rate从高到低刷,那么我也就这么办吧。同学还建议直接在网页上写代码,这样可以适应在线笔试时的手写要求。稍后把解决方案放到GitHub上去。问题描述问题连接:https://leetcode.com/problems/encode-and-decode-tinyurl/原创 2017-03-14 10:45:11 · 4753 阅读 · 0 评论 -
【LeetCode】462. Minimum Moves to Equal Array Elements II
问题描述问题链接:https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/#/descriptionGiven a non-empty integer array, find the minimum number of moves required to make all array elements equal,原创 2017-03-23 13:32:27 · 466 阅读 · 0 评论 -
【LeetCode】常用算法之Quick Select
之前刚刚实现了快速排序算法。现在还有一个要求就是找到一个序列中第K大的数。我们当然可以用先排序再取值的方法来做,这样的时间复杂度为O(NlogN)。或者使用heap_sort,或者优先队列,则复杂度是O(NlogK)。那么有没有一种更加高效的方式呢?答案是肯定的。可以使用快速排序的一个变种quick_select,则平均复杂度为O(N),最坏复杂度为O(N^2)。算法思想通过一趟快排过后,序列将被分原创 2017-03-23 12:33:17 · 13396 阅读 · 1 评论 -
【LeetCode】常用算法之回溯法
参考资料http://www.cnblogs.com/steven_oyj/archive/2010/05/22/1741376.html概念回溯算法实际上一个类似枚举的搜索尝试过程,主要是在搜索尝试过程中寻找问题的解,当发现已不满足求解条件时,就“回溯”返回,尝试别的路径。回溯法是一种选优搜索法,按选优条件向前搜索,以达到目标。但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步重新选择原创 2017-03-20 11:22:58 · 452 阅读 · 0 评论 -
【LeetCode】常用算法之快速排序
今天刷题的时候要用到快排,然后发现居然写不出来了,好沮丧啊。看来基础知识还需要好好加强,先把快排补起来。简介快速排序由C. A. R. Hoare在1962年提出。它的基本思想是:通过一趟排序将要排序的数据分割成独立的两部分,其中一部分的所有数据都比另外一部分的所有数据都要小,然后再按此方法对这两部分数据分别进行快速排序,整个排序过程可以递归进行,以此达到整个数据变成有序序列。算法步骤设要排序的数组原创 2017-03-23 11:51:18 · 3101 阅读 · 0 评论 -
【LeetCode】389. Find the Difference
问题描述问题链接:https://leetcode.com/problems/find-the-difference/#/descriptionGiven two strings s and t which consist of only lowercase letters.String t is generated by random shuffling string s and then add原创 2017-03-22 11:01:38 · 343 阅读 · 0 评论 -
【LeetCode】495. Teemo Attacking
问题描述问题链接:https://leetcode.com/problems/teemo-attacking/#/descriptionIn LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo’s原创 2017-03-22 10:41:43 · 446 阅读 · 0 评论 -
【LeetCode】104. Maximum Depth of Binary Tree
问题描述问题链接:https://leetcode.com/problems/maximum-depth-of-binary-tree/#/descriptionGiven a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the roo原创 2017-03-22 09:42:24 · 590 阅读 · 0 评论