![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
文章平均质量分 52
tianlongloved
Coding
展开
-
LeetCode:Single Number I && II
Given an array of integers, every element appears twice except for one. Find that single one.题目很直接,直接建哈希表,遍历,在表中就删除元素,不在表中就加入表,最后表中只剩下一个元素,返回其值:public class Solution { public int singleNum原创 2014-03-27 13:00:54 · 474 阅读 · 0 评论 -
LeetCode:Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. Y原创 2014-03-28 17:34:14 · 284 阅读 · 0 评论 -
LeetCode:Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.原创 2014-04-16 16:43:12 · 338 阅读 · 0 评论 -
LeetCode:Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.有序数列化为二叉搜索树,经典算法,找到中间节点作为root,依次递归下去,直到构建成为二叉搜索树。/** * Definition for binary tree * public class原创 2014-04-03 10:02:52 · 447 阅读 · 0 评论 -
LeetCode:Path Sum I &&II
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原创 2014-04-03 16:13:53 · 419 阅读 · 0 评论 -
LeetCode:Subsets I
Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For examp原创 2014-03-27 11:50:33 · 419 阅读 · 0 评论 -
LeetCode:Best Time to Buy and Sell Stock I & II & III
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 (ie, buy one and sell one share of the stock),原创 2014-03-27 14:24:32 · 339 阅读 · 0 评论 -
LeetCode:Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: ["2", "1",原创 2014-03-27 19:05:09 · 405 阅读 · 0 评论 -
LeetCode:Search Insert Position
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 order.You may assume no duplicates in the arra原创 2014-03-27 21:20:46 · 272 阅读 · 0 评论 -
LeetCode: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?典型的斐波拉契数列,简单的动态规划问题,开始的时候想到f原创 2014-03-27 21:41:59 · 262 阅读 · 0 评论 -
LeetCode: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原创 2014-04-09 11:46:26 · 311 阅读 · 0 评论 -
LeetCode:Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1]原创 2014-04-09 10:08:50 · 381 阅读 · 0 评论 -
LeetCode:Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving ofs1 and s2. For example,Given:s1 = "aabcc",s2 = "dbbca", When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", retu原创 2014-06-30 15:45:17 · 317 阅读 · 0 评论 -
LeetCode: 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:"((()))", "(()())", "(())()", "()(())", "()()原创 2014-06-18 22:24:18 · 343 阅读 · 0 评论 -
LeetCode:Jump Game I && II
Jump Game Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that positi原创 2014-06-19 23:40:09 · 315 阅读 · 0 评论 -
LeetCode:Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from原创 2014-03-28 20:39:41 · 458 阅读 · 0 评论 -
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 never diffe原创 2014-03-28 17:53:11 · 312 阅读 · 0 评论 -
LeetCode: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]]生成杨辉三角,一行中除了两边的数字为1以外,该数等于原创 2014-03-28 20:18:40 · 374 阅读 · 0 评论 -
LeetCode: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原创 2014-03-27 14:31:23 · 222 阅读 · 0 评论 -
LeetCode:Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321记录正负号,将数组转为无符号并转为字符串,之后翻转字符,最后再把正负号加上。public class Solution { public int reverse(int x) {原创 2014-03-27 14:38:30 · 278 阅读 · 0 评论 -
LeetCode:Populating Next Right Pointers in Each Node I & II
Populating Next Right Pointers in Each NodeGiven a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Popul原创 2014-03-27 21:09:49 · 355 阅读 · 0 评论 -
LeetCode: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原创 2014-03-27 22:29:06 · 292 阅读 · 0 评论 -
LeetCode:Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.删除有序链表中重复的数值,一次遍历,设置指针指原创 2014-03-27 21:32:39 · 317 阅读 · 0 评论 -
LeetCode:Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.首先计算链表长度,将链表首尾相接,之后得出链表从head开始起的正原创 2014-03-28 16:51:29 · 317 阅读 · 0 评论 -
LeetCode: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 binary tree * public class TreeNode { * int val; * T原创 2014-03-28 16:57:25 · 297 阅读 · 0 评论 -
LeetCode:Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.原创 2014-04-29 13:56:15 · 375 阅读 · 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, 2, 3原创 2014-04-29 14:17:16 · 468 阅读 · 0 评论 -
LeetCode: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.求二叉树的深度,递归得到结果。/** * Definiti原创 2014-03-27 14:34:50 · 223 阅读 · 0 评论 -
LeetCode:Simplify Path
Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"题目不难,利用正则表达式以"/"为分隔符进行分割,将"",".",".."的其他所有字符串压栈,碰到一次".."将栈原创 2014-03-27 18:55:24 · 281 阅读 · 0 评论 -
LeetCode:Linked List Cycle I & II
Linked List CycleGiven a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?判断链表是否有环,经典的做法就是设快慢指针各一个,当快指针和慢指针重合时说明链接有环,否则快指针将为null。原创 2014-03-27 19:30:02 · 435 阅读 · 0 评论 -
LeetCode:Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.合并有序链表,为避免对head进行讨论,建立一个空head,返回的时候返回head.next,建立空head之原创 2014-03-28 17:12:27 · 304 阅读 · 0 评论 -
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 3But the f原创 2014-03-28 17:23:14 · 403 阅读 · 0 评论 -
LeetCode:Regular Expression Matching
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input st原创 2014-06-22 19:43:13 · 394 阅读 · 0 评论