![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法
文章平均质量分 58
nianhua120
这个作者很懒,什么都没留下…
展开
-
LeetCode 121. Best Time to Buy and Sell Stock
LeetCode 121. Best Time to Buy and Sell StockMaSay 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 (ie,原创 2016-07-04 13:54:14 · 345 阅读 · 0 评论 -
LeetCode 111. 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.叶子节点是没有子节点的节点,刚开始没有理解这句话。所以对于测试用例[原创 2016-07-06 17:38:42 · 195 阅读 · 0 评论 -
LeetCode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]树的遍历,相当简单原创 2016-07-28 10:51:24 · 211 阅读 · 0 评论 -
LeetCode 234. Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?要在O(N)时间,O(1)空间完成,必须得反转链表,思路如下。1,通过快慢指针找到中心位置。2,以中心为线,将前面的链表反转3,通过原创 2016-07-28 11:19:55 · 226 阅读 · 0 评论 -
LeetCOde 64. Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at原创 2016-07-28 13:39:32 · 193 阅读 · 0 评论 -
LeetCode 40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in the combi原创 2016-07-28 14:39:56 · 204 阅读 · 0 评论 -
Leetcode112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and sum原创 2016-07-07 19:22:03 · 225 阅读 · 0 评论 -
leetcode 189. Rotate Array
Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].Note:Try to come up as many solutions as yo原创 2016-06-22 12:23:49 · 208 阅读 · 0 评论 -
LeetCode 86. Partition List
iven a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of原创 2016-07-07 20:39:24 · 191 阅读 · 0 评论 -
LeetCode 191,Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representation 000000原创 2016-07-06 16:41:29 · 188 阅读 · 0 评论 -
LeetCode 23. Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.一开始安装Merge 2 Sorted Lists 的思路,然后就超时了。不过把代码贴在这儿,便于后面比较,称为方法1./** * Definition for singly-l原创 2016-07-06 14:10:43 · 305 阅读 · 0 评论 -
Leetcode 51. N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle.原创 2016-07-05 21:28:04 · 290 阅读 · 0 评论 -
LeetCode 130 Surrounded Regions
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 that surrounded region.For example,原创 2016-07-26 15:50:22 · 263 阅读 · 0 评论 -
LeetCode70. Climbing Stairs
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?爬楼梯问题是Fibonacci问题,只要记录前两步的台阶原创 2016-07-04 21:46:23 · 213 阅读 · 0 评论 -
LeetCode 57. Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.E原创 2016-07-15 13:36:37 · 180 阅读 · 0 评论 -
LeetCode344. Reverse String
Write a function that takes a string as input and returns the string reversed.Example:Given s = "hello", return "olleh".这个题目考察O(1)空间交换数组的。public class Solution { public String reve原创 2016-07-26 17:23:56 · 180 阅读 · 0 评论 -
LeetCode 100. Same Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.这道题考察的就是树的原创 2016-07-05 09:20:12 · 195 阅读 · 0 评论 -
LeetCode 203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5Credits:Special thanks原创 2016-07-27 11:33:46 · 186 阅读 · 0 评论 -
LeetCode 104. Maximum Depth of Binary Tree
Given a binary 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 leaf node.这道题就是遍历树,记录最远的位置。/** * Defini原创 2016-07-05 17:27:49 · 198 阅读 · 0 评论 -
Leetcode 118. Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]这道题就是pascal三角形。我们可以看出从第三行开始,原创 2016-07-05 19:50:57 · 212 阅读 · 0 评论 -
LeetCode101 Symmetric Tree
iven a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3 4 4 3原创 2016-07-08 11:18:08 · 207 阅读 · 0 评论 -
LeetCode 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原创 2016-07-08 16:28:12 · 238 阅读 · 0 评论 -
LeetCode 242. Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.Note:You may ass原创 2016-08-04 14:39:16 · 217 阅读 · 0 评论 -
114. Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1原创 2016-08-05 16:55:11 · 227 阅读 · 0 评论 -
LeetCode 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 of every node never dif原创 2016-08-05 17:34:53 · 261 阅读 · 0 评论 -
LeetCode80. Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with the first fi原创 2016-08-08 14:06:47 · 234 阅读 · 0 评论 -
LeetCode82.Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->原创 2016-08-08 14:48:20 · 262 阅读 · 0 评论 -
LeetCode 138. Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.这个题目想了好久,觉得应该用HashMap 之类原创 2016-08-10 15:43:52 · 331 阅读 · 0 评论 -
棋盘覆盖
题目要求在棋盘覆盖问题中,要用下图—图(2)所示的4种不同形态的L型骨牌覆盖一个给定的特殊棋盘上除特殊方格以外的所有方格,且任何2个L型骨牌不得重叠覆盖运行结果 /* * Copyright (c) 2017, CipherGateway and/or its affiliates. All rights reserved. * */import javafx.application.Ap原创 2017-09-13 14:20:01 · 607 阅读 · 0 评论 -
LeetCode 226. Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1这道题和判断树的对称性是一样的递归翻转左右子树。/** * Definition for a binary tree node. *原创 2016-07-21 11:18:09 · 213 阅读 · 0 评论 -
LeetCode 129. Sum Root to Leaf Numbers
iven 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 number 123.Find the total原创 2016-07-21 11:13:39 · 226 阅读 · 0 评论 -
LeetCode105. Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.这种类型的题目经常考选择题。对于这道题,采用的是分治的思想,通过preorder的第一个元素为根元素,将ino原创 2016-08-04 13:11:15 · 200 阅读 · 0 评论 -
LeetCode 206. Reverse Linked List
Reverse a singly linked list.其实我们知道Stack有个特点,你将一组元素入Stack, 然后再出Stack,它就会反序。这儿我们就利用这个思想,我们并不用建立一个堆,只要利用递归函数访问具有Stack的特点这个性质就足够了。 我们每一次递归,就先求得后面链表Reverse之后的队尾元素,然后让这个元素指向当前元素,并把当前元素当作队尾元素返回。假如传入的参数的ne原创 2016-07-08 16:29:25 · 219 阅读 · 0 评论 -
LeetCode 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive solution is tr原创 2016-07-08 17:25:50 · 188 阅读 · 0 评论 -
美团2016 ,求最大值
题目描述有一个长为n的数组A,求满足0≤a≤b给定数组A及它的大小n,请返回最大差值。测试样例:[10,5],2返回:0时间复杂度O(N)可解。用一个变量缓存前面位置的最小值。import java.util.*;public class LongestDistance { public int getDis(int[] A,原创 2016-07-08 18:56:45 · 276 阅读 · 0 评论 -
TrieTree的实现
TrieTree是字典树,在文本处理中非常好用。先建立节点/** * Created by kyle on 2016/6/23. */public class TrieNode { private final static int NUMBER = 26; private char _value ; private boolean isWorld;原创 2016-06-23 16:43:48 · 270 阅读 · 0 评论 -
LeetCode 53. Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] ha原创 2016-07-20 12:21:20 · 213 阅读 · 0 评论 -
LeetCode 67. Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".一位一位的加就行了,考虑溢出。public class Solution { public String addBinary(String a, S原创 2016-07-20 14:25:45 · 178 阅读 · 0 评论 -
LeetCode 39. Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited numb原创 2016-07-20 16:36:14 · 216 阅读 · 0 评论 -
LeetCode 116. Populating Next Right Pointers in Each Node
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node.原创 2016-08-04 09:44:27 · 186 阅读 · 0 评论