![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode medium
文章平均质量分 74
豆腐脑是咸的
这个作者很懒,什么都没留下…
展开
-
Binary Tree Preorder Traversal (Java)
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 soluti原创 2014-12-31 15:12:02 · 353 阅读 · 0 评论 -
Swap Nodes in Pairs (Java)
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原创 2015-01-01 16:19:27 · 412 阅读 · 0 评论 -
Insertion Sort List (Java)
Sort a linked list using insertion sort.写的有点乱,有时间再写一遍Source/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { *原创 2015-01-02 20:11:29 · 305 阅读 · 0 评论 -
Sort Colors (Java)
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原创 2015-01-02 21:39:31 · 304 阅读 · 0 评论 -
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.和Convert Sorted Array to Binary Search Tree的区别是array可以用二分法,而list没法直接算mid。这时可用双指针,一个走快一个走慢原创 2015-01-21 15:55:53 · 379 阅读 · 0 评论 -
Unique Binary Search Trees II (Java)
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n.For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3原创 2015-01-21 14:37:05 · 377 阅读 · 0 评论 -
Remove Duplicates from Sorted Array II (Java)
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [1,1,2,原创 2015-01-03 16:22:09 · 387 阅读 · 0 评论 -
Longest Substring Without Repeating Characters (Java)
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. Fo原创 2015-01-03 20:23:40 · 270 阅读 · 0 评论 -
Container With Most Water (Java)
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 (i, 0). Fin原创 2015-01-03 21:21:57 · 270 阅读 · 0 评论 -
Rotate List (Java)
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.注意:这题是rotate 就像是把链表首尾相连,然后向右翻转,所以原创 2015-01-01 15:47:41 · 406 阅读 · 0 评论 -
Sort List (Java)
Sort a linked list in O(n log n) time using constant space complexity.学习了网上的帖子,用归并排序,注意空间复杂度超了,记得重做Source/** * Definition for singly-linked list. * class ListNode { * int val; * Li原创 2015-01-01 14:30:09 · 292 阅读 · 0 评论 -
Largest Number (Java)
Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.Note: The result may be ve原创 2015-01-20 11:02:10 · 674 阅读 · 0 评论 -
Longest Palindromic Substring (Java)
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.这道题可以套用Palindrome原创 2015-01-20 21:32:46 · 341 阅读 · 0 评论 -
Integer to Roman (Java)
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.罗马数字 I(1)、V(5)、X(10)、L(50)、C(100)、D(500)、M(1000)罗马数字是左减右加只需记住罗马数字转为整型数字的减操作注意事项:原创 2015-01-20 22:01:07 · 242 阅读 · 0 评论 -
Flatten Binary Tree to Linked List (Java)
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原创 2014-12-31 10:02:09 · 363 阅读 · 0 评论 -
Reorder List (Java)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder it to原创 2015-01-01 16:55:08 · 262 阅读 · 0 评论 -
Remove Duplicates from Sorted List II (Java)
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->1-原创 2015-01-01 19:50:39 · 314 阅读 · 0 评论 -
Partition List (Java)
Given 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原创 2015-01-01 20:32:18 · 452 阅读 · 0 评论 -
Linked List Cycle II (Java)
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Follow up:Can you solve it without using extra space?先用一个指针走一步,另一个指针走两步的方法判断是否存在回路,如果存在,则将其中一个指原创 2015-01-01 21:20:35 · 315 阅读 · 0 评论 -
Clone Graph (Java)
Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.OJ's undirected graph serialization:Nodes are labeled uniquely.We use # as a separator for ea原创 2015-01-20 09:33:39 · 374 阅读 · 0 评论 -
Two Sum (Java)
Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, whe原创 2015-01-03 22:30:06 · 315 阅读 · 0 评论 -
Linked List Cycle (Java)
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?设两个指针,一个走两步,一个走一步,如果有环,快的一定会和慢的汇合Source/** * Definition for singly-linked原创 2015-01-01 20:46:45 · 292 阅读 · 0 评论 -
3Sum Closest (Java)
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exact原创 2015-01-04 14:39:38 · 408 阅读 · 0 评论 -
Single Number II (Java)
Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without usi原创 2015-01-05 10:25:28 · 405 阅读 · 0 评论 -
Anagrams (Java)
Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.给定一字符串数组,返回其中是由相同字符组成只是顺序不同的字符串子数组,这些字符只出现了一次的字符串不返回。比如 abb,bab,bba,abc 返回[原创 2015-01-04 21:44:09 · 293 阅读 · 0 评论 -
Word Ladder (Java)
Given two words (start and end), and a dictionary, find the length of shortest transformation sequence from start to end, such that:Only one letter can be changed at a timeEach intermediate word m原创 2015-01-05 15:02:50 · 461 阅读 · 0 评论 -
Sqrt(x) (Java)
Implement int sqrt(int x).Compute and return the square root of x.用二分法来查找,x的根肯定是大于0小于x的,mid作为x的根,用mid * mid与x比大小,然后接着查找,如果有则返回,不能开尽的用较小的整数代替(根向下取整)。Sourcepublic class Solution { pu原创 2015-01-05 16:16:02 · 442 阅读 · 0 评论 -
Search Insert Position (Java)
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 array.原创 2015-01-05 16:34:30 · 328 阅读 · 0 评论 -
Search a 2D Matrix (Java)
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 integer of each原创 2015-01-05 17:10:10 · 253 阅读 · 0 评论 -
Pow(x, n) (Java)
Implement pow(x, n).注意n为负数的情况Sourcepublic class Solution { public double pow(double x, int n) { if(n == 0) return 1; if(n < 0){ n = -n; x = 1 / x;原创 2015-01-05 19:40:28 · 345 阅读 · 0 评论 -
Find Peak Element (Java)
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, i原创 2015-01-05 21:42:19 · 370 阅读 · 0 评论 -
Fraction to Recurring Decimal (Java)
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.If the fractional part is repeating, enclose the repeating part in parentheses.原创 2015-01-23 10:20:01 · 452 阅读 · 0 评论 -
Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the原创 2015-01-23 18:08:01 · 273 阅读 · 0 评论 -
Simplify Path (Java)
Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"click to show corner cases.Corner Cases:Did原创 2015-01-23 09:15:32 · 351 阅读 · 0 评论 -
3Sum (Java)
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c原创 2015-01-04 13:33:18 · 406 阅读 · 0 评论 -
Reverse Words in a String (Java)
Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".click to show clarification.Clarification:What constitutes原创 2015-01-22 09:15:02 · 424 阅读 · 0 评论 -
Binary Search Tree Iterator (Java)
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next()原创 2015-01-22 16:38:20 · 411 阅读 · 0 评论 -
Maximum Product Subarray (Java)
Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest原创 2015-01-21 13:16:27 · 376 阅读 · 0 评论 -
Divide Two Integers (Java)
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.这道题题意是算余数不能用乘、除和取模运算。循环减会超时。可以采用位运算,下次写注意下。这道题主要在对于边界的处理上,-2147483648,原创 2015-01-22 10:44:04 · 388 阅读 · 0 评论 -
Word Break (Java)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.For example, givens = "leetcode",dict = ["leet"原创 2015-01-21 17:21:59 · 344 阅读 · 0 评论