LeetCode专栏
针对LeetCode上的题目给出详细思路解释以及AC代码示例
轻春
Talk is cheap, show me the code.
展开
-
N皇后问题
N皇后问题-I问题描述 n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击。 给定一个整数n,返回所有不同的n皇后问题的解决方案。 每个解决方案包含一个明确的n皇后放置布局,其中“Q”和“.”分别表示一个女王和一个空位置。样例: 对于4皇后问题存在两种解决的方案:[ [".Q..", // Solution 1 ".....原创 2018-05-04 00:49:40 · 282 阅读 · 0 评论 -
二叉树之理解记忆并背诵,了解一下?
内容主要包含: 1. 二叉树中DFS三种搜索的模板程序 (递归+非递归) 2. 二叉树BFS非递归版本 3. 二叉树常见到必背的考题 不管你是刷题学习,还是准备面试,二叉树下面的这几个程序都是 理解记忆并背诵!理解记忆并背诵!理解记忆并背诵!重要的事情说三遍真的太常用了,小哥哥小姐姐一定要掌握哦~BFS递归版本void bfs(TreeNode...原创 2018-04-09 22:36:53 · 707 阅读 · 0 评论 -
LeetCode 108. Convert Sorted Array to Binary Search Tree
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.二、输入输出三、解题思路Binary Search Tree 二叉搜索树定义为:是指一颗空树原创 2017-07-11 17:44:33 · 556 阅读 · 0 评论 -
LeetCode 111. Minimum Depth of Binary Tree
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原创 2017-07-11 17:44:56 · 491 阅读 · 0 评论 -
LeetCode 104. Maximum Depth of Binary Tree
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原创 2017-07-10 13:57:38 · 574 阅读 · 0 评论 -
LeetCode 107. Binary Tree Level Order Traversal II
107. Binary Tree Level Order Traversal II一、问题描述 Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root).二、输入输原创 2017-07-10 13:57:54 · 603 阅读 · 0 评论 -
LeetCode 101. Symmetric Tree
101. Symmetric Tree一、问题描述 Given 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 / \原创 2017-07-10 13:17:19 · 538 阅读 · 0 评论 -
LeetCode 10. Same Tree
10. 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 sa原创 2017-07-10 13:17:35 · 462 阅读 · 0 评论 -
LeetCode 69. Sqrt(x)
69. Sqrt(x)一、问题描述 Implement int sqrt(int x). Compute and return the square root of x.二、输入输出三、解题思路二分法直接写二分法超时了,不知道为什么 int Sqrt(int x, int low, int high) { int mid; while原创 2017-07-06 20:35:28 · 689 阅读 · 0 评论 -
LeetCode 67. Add Binary
67. Add Binary一、问题描述 Given two binary strings, return their sum (also a binary string).二、输入输出For example, a = "11" b = "1" Return "100".三、解题思路从后向前遍历这道题看着不难,有几个点需要注意。首先最好不要转换成int或者二进制的数来进行计算,再转换回原创 2017-07-06 19:54:02 · 714 阅读 · 0 评论 -
LeetCode 58. Length of Last Word
58. Length of Last Word一、问题描述 Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exis原创 2017-07-05 19:13:47 · 698 阅读 · 0 评论 -
LeetCode 28. Implement strStr()
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.二、输入输出三、解题思路转换成:判断两个字符串prefix是否相同单独处理原创 2017-07-05 17:37:52 · 655 阅读 · 0 评论 -
LeetCode 20. Valid Parentheses
20. Valid Parentheses一、问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "原创 2017-07-05 16:03:14 · 545 阅读 · 0 评论 -
LeetCode 14. Longest Common Prefix
14. Longest Common Prefix一、问题描述 Write a function to find the longest common prefix string amongst an array of strings.二、输入输出三、解题思路二路归并解题思路:如果给你两个字符串,让你找出最大的common prefix 你一定有办法(对吧?)简单说就是取第一个字符串的子串,原创 2017-07-05 16:03:33 · 550 阅读 · 0 评论 -
LeetCode 9. Palindrome Number
9. Palindrome Number一、问题描述 Determine whether an integer is a palindrome. Do this without extra space. **Some hints:**Could negative integers be palindromes? (ie, -1)If you are thinking of conver原创 2017-07-04 16:34:51 · 410 阅读 · 0 评论 -
LeetCode 7. Reverse Integer
7. Reverse Integer一、问题描述 Reverse digits of an integer. **Have you thought about this?**Here are some good questions to ask before coding. Bonus points for you if you have already thought through原创 2017-07-04 15:52:10 · 438 阅读 · 0 评论 -
LeetCode 4. Median of Two Arrays
4. Median of Two Arrays一、问题描述 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log原创 2017-07-04 00:42:59 · 614 阅读 · 1 评论 -
LeetCode 3. Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters一、问题描述 Given a string, find the length of the longest substring without repeating characters.二、输入输出Examples:Given "abcabcbb", the answer is "abc", wh原创 2017-07-03 17:30:22 · 467 阅读 · 0 评论 -
LeetCode 617. Merge Two Binary Trees
617. Merge Two Binary Trees一、问题描述 Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You ne原创 2017-06-26 15:21:49 · 605 阅读 · 0 评论 -
LeetCode 500. Keyboard Row
500. Keyboard Row一、问题描述 Given 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 image below. 二、输入输出Example 1:Input:原创 2017-06-26 14:59:34 · 527 阅读 · 0 评论 -
LeetCode 2. Add Two Numbers
2. Add Two Numbers一、问题描述 You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add th原创 2017-06-23 16:42:38 · 651 阅读 · 0 评论 -
LeetCode 61. Rotate List
61. 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.三、解题思路这道题的原创 2017-06-22 17:33:42 · 704 阅读 · 0 评论 -
LeetCode 330. Patching Array
330. Patching Array一、问题描述 Given a sorted positive integer array nums and an integer n, add/patch elements to the array such that any number in range [1, n]inclusive can be formed by the sum of some e原创 2017-06-21 23:56:21 · 660 阅读 · 0 评论 -
LeetCode 502. IPO
502. IPO一、问题描述 Suppose LeetCode will start its IPO soon. In order to sell a good price of its shares to Venture Capital, LeetCode would like to work on some projects to increase its capital before th原创 2017-06-21 21:46:35 · 733 阅读 · 0 评论 -
LeetCode 435. Non-overlapping Intervals
435. Non-overlapping Intervals一、问题描述 Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Note: You原创 2017-06-19 20:38:15 · 598 阅读 · 0 评论 -
LeetCode 406. Queue Reconstruction by Height
406. Queue Reconstruction by Height一、问题描述 Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and原创 2017-06-19 20:38:30 · 502 阅读 · 0 评论 -
LeetCode 621. Task Scheduler
621. Task Scheduler一、问题描述 Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without origin原创 2017-06-19 16:34:44 · 3771 阅读 · 0 评论 -
LeetCode 402. Remove K Digits
402. Remove K Digits一、问题描述 Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible. Note: The length of原创 2017-06-19 14:35:10 · 648 阅读 · 0 评论 -
LeetCode 376. Wiggle Subsequence
376. Wiggle Subsequence一、问题描述 A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (i原创 2017-06-16 12:44:09 · 490 阅读 · 0 评论 -
LeetCode 452. Minimum Number of Arrows to Burst Balloons
452. Minimum Number of Arrows to Burst Balloons一、问题描述 There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of th原创 2017-06-15 20:10:50 · 381 阅读 · 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?一,算法分析大体思想是利用快慢两个指针找到中间节点,然后将后面半个链表进行逆转;依次与前半部分比较,若相等则说明是回文串;二,C语言实现/*原创 2016-03-09 01:03:46 · 411 阅读 · 0 评论 -
LeetCode 21. 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.一,算法分析类似两个数组的合并,注意OJ给的链表默认是不带头结点的;另外两个数相等的时候可以归到大于或小于原创 2016-03-09 01:40:20 · 541 阅读 · 0 评论 -
LeetCode 206. Reverse Linked List
Reverse a singly linked list.click to show more hints.Hint:A linked list can be reversed either iteratively or recursively. Could you implement both?一,算法分析头插法重建单链表,注意默认链表是没有头结点的,所以建表的时候不原创 2016-03-09 01:57:28 · 378 阅读 · 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 --> 5一、算法分析本来很简单的一道题,但是Le原创 2016-03-10 01:06:16 · 327 阅读 · 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原创 2016-03-10 01:18:38 · 371 阅读 · 0 评论 -
LeetCode 83. 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.一、算法分析处理不带头结点的单链表,涉及到原创 2016-03-10 10:28:35 · 332 阅读 · 0 评论 -
LeetCode 328. Odd Even Linked List
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it in原创 2016-03-10 20:08:05 · 399 阅读 · 0 评论 -
LeetCode 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 ↘原创 2016-03-10 20:32:02 · 371 阅读 · 0 评论 -
LeetCode 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up:Can you solve it without using extra space?一、原创 2016-03-18 13:00:14 · 422 阅读 · 0 评论 -
LeetCode 66.Plus One
66.Plus One一、问题描述 Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. You may assume the integer do not contain any leading zero, except the number原创 2017-05-22 15:03:52 · 262 阅读 · 0 评论