![](https://img-blog.csdnimg.cn/20201014180756923.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode小记
文章平均质量分 65
SP独坐
需要慢慢学习的一名程序员
展开
-
453. Minimum Moves to Equal Array Elements
Given a non-empty integer array of size n, find the minimum number of moves required to make all array elements equal, where a move is incrementing n - 1 elements by 1.Example:Input:[1,2,3]Output:...原创 2018-03-06 13:11:25 · 137 阅读 · 0 评论 -
441. Arranging Coins
You have a total of n coins that you want to form in a staircase shape, where everyk-th row must have exactly k coins.Given n, find the total number of full staircase rows that can be formed.n i原创 2017-11-09 23:34:39 · 137 阅读 · 0 评论 -
303. Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indicesi and j (i ≤ j), inclusive.Example:Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sumRange(2, 5) -> -1sumRang原创 2017-10-24 13:03:30 · 158 阅读 · 0 评论 -
110. 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 ofevery node never differ原创 2017-10-15 16:27:43 · 169 阅读 · 0 评论 -
108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.源码:/** * Definition for a binary tree node. * public class TreeNode { * int val; *原创 2017-10-15 15:48:03 · 133 阅读 · 0 评论 -
160. 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 ↘原创 2017-10-15 10:50:10 · 191 阅读 · 0 评论 -
75. 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 integers 0,原创 2017-10-24 00:33:22 · 128 阅读 · 0 评论 -
39. Combination Sum
Given a set of candidate numbers (C)(without duplicates) and a target number (T), find all unique combinations inC where the candidate numbers sums to T.The same repeated number may be chosen原创 2017-10-14 20:20:06 · 260 阅读 · 0 评论 -
129. Sum Root to Leaf Numbers
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 which represents the number123.Find the total sum原创 2017-11-09 00:33:01 · 118 阅读 · 0 评论 -
122. Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one原创 2017-10-13 23:44:43 · 139 阅读 · 0 评论 -
136. Single Number
Given an 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原创 2017-09-27 00:00:24 · 190 阅读 · 0 评论 -
89. Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequence of gr原创 2017-10-31 00:58:40 · 133 阅读 · 0 评论 -
507. Perfect Number
We define the Perfect Number is a positive integer that is equal to the sum of all itspositive divisors except itself. Now, given an integer n, write a function that returns true when it is a perf原创 2017-11-08 01:52:46 · 141 阅读 · 0 评论 -
225. Implement Stack using Queues
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.top() -- Get the top element.empty() -- Return whet原创 2017-11-01 23:26:07 · 356 阅读 · 0 评论 -
60. Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132""213""231""312""原创 2017-11-02 01:30:14 · 147 阅读 · 0 评论 -
367. Valid Perfect Square
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 function such as sqrt.Example 1:Input: 16Return原创 2017-10-25 11:31:12 · 181 阅读 · 0 评论 -
96. Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / ...原创 2018-02-25 20:42:27 · 138 阅读 · 0 评论 -
438. Find All Anagrams in a String
Given a string s and a non-empty string p, find all the start indices of p's anagrams in s.Strings consists of lowercase English letters only and the length of both stringss and p will not be原创 2018-01-10 01:18:25 · 154 阅读 · 0 评论 -
401. Binary Watch
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom represent theminutes (0-59).Each LED represents a zero or one, with the least significant bit on原创 2017-12-29 19:25:01 · 124 阅读 · 0 评论 -
594. Longest Harmonious Subsequence
We define a harmonious array is an array where the difference between its maximum value and its minimum value isexactly 1.Now, given an integer array, you need to find the length of its longest ha原创 2018-01-04 22:25:07 · 133 阅读 · 0 评论 -
222. Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes.Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled,原创 2018-01-04 15:56:12 · 130 阅读 · 0 评论 -
627. Swap Salary
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 versa) with a single update query and no intermediate原创 2018-01-21 14:16:43 · 140 阅读 · 0 评论 -
383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; ot原创 2018-01-03 17:09:09 · 143 阅读 · 0 评论 -
200. Number of Islands
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原创 2018-01-02 19:16:54 · 122 阅读 · 0 评论 -
405. Convert a Number to Hexadecimal
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer,two’s complement method is used.Note:All letters in hexadecimal (a-f) must be in lowercase.The hexadec原创 2017-11-06 00:31:04 · 120 阅读 · 0 评论 -
98. Validate Binary Search Tree
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原创 2017-11-03 01:15:28 · 127 阅读 · 0 评论 -
33. Search in Rotated Sorted Array
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).You are given a target value to search. If found in t原创 2017-09-22 01:16:24 · 121 阅读 · 0 评论 -
231. Power of Two
Given an integer, write a function to determine if it is a power of two.较好的思路:讲这个数与其前一个数作与运算,一旦为0满足要求,否则不满足要求官方解答:Power of 2 means only one bit of n is '1', so use the trick n&(n-1)==0 to原创 2017-10-13 22:51:13 · 613 阅读 · 0 评论 -
73. 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.click to show follow up.Follow up:Did you use extra space?A straight forward solution using O(mn) s原创 2017-10-13 01:42:33 · 139 阅读 · 0 评论 -
258. 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 has only one原创 2017-10-29 14:10:53 · 182 阅读 · 0 评论 -
563. Binary Tree Tilt
Given a binary tree, return the tilt of the whole tree.The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtree原创 2017-11-07 00:34:49 · 157 阅读 · 0 评论 -
62. Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bo原创 2017-10-17 20:48:21 · 132 阅读 · 0 评论 -
155. Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get原创 2017-09-29 21:36:03 · 148 阅读 · 0 评论 -
22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[ "((()))", "(()())", "(())()", "()(())",原创 2017-09-21 00:42:19 · 115 阅读 · 0 评论 -
14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.public class Solution { public String longestCommonPrefix(String[] strs) { if(strs.l原创 2017-09-11 23:54:27 · 121 阅读 · 0 评论 -
11. 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 linei is at (i, ai) and (i, 0). Find原创 2017-09-11 23:50:24 · 115 阅读 · 0 评论 -
238. Product of Array Except Self
Given an array of n integers where n > 1, nums, return an arrayoutput such that output[i] is equal to the product of all the elements ofnums except nums[i].Solve it without division and in O(n).原创 2017-09-11 23:31:03 · 141 阅读 · 0 评论 -
19. Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the l原创 2017-09-19 23:25:12 · 260 阅读 · 0 评论 -
31. Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible o原创 2017-09-26 01:08:00 · 217 阅读 · 0 评论 -
28. Implement strStr()
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.官方代码如下:public int strStr(String haystack, String needle) { for (in原创 2017-09-16 00:42:49 · 127 阅读 · 0 评论