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, 1, and 2 to represent the color red, white, and bl…
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-Solution/ ———————————————————————————————————————— ———————————————————————————————————————— LeetCode OJ 题解 LeetCode OJ is a platform for preparing tech…
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 = "aabcc", s2 = "dbbca", When s3 = "aadbbcbcac", return t…
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reverse the string word by word. For example, Given s = "the sky is blue", return "blue is sky the". LeetCode OJ supports Python now! The s…
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another comput…
leetcode 148. Sort List 提交网址: https://leetcode.com/problems/sort-list/ Total Accepted: 68702 Total Submissions: 278100 Difficulty: Medium ACrate: 24.7% Sort a linked list in O(n log n) time using constant space complexity. 由于需要使用常量空间,即S(n)=O(1),故需要…
题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 0 0 1 0 Return 4. 题目链接:https://leetcode.com/problems/maximal-square…
LeetCode--Insertion Sort List Question Sort a linked list using insertion sort. Solution 我的解法,假设第一个节点都比其他节点小,这样感觉好移动指针一些,所以添加了一个额外的最小的节点. Code class Solution { public: ListNode *insertionSortList(ListNode *head) { if (head == NULL) return head; ListN…
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 给定一个整数,将他转换到罗马字符.同样这里的罗马字符不会超过1000(M),PS:关于罗马字符的介绍看我的这篇文章 :LeetCode OJ:Roman to Integer(转换罗马字符到整数) 这里的大体思路是这样的,例如对于罗马字符952来说,起答题的可以分成三个组成部分, 就…
Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another comput…
Sort a linked list using insertion sort. Subscribe to see which companies asked this question 解答 对于链表的插入排序,用tmp_tail遍历链表,每次的待插入数是tmp_tail->next的元素,待插入数必须从头开始比较,当然从头开始比较时要注意处理待排序数小于或等于链表首元素的情况,因为插入在链表的首元素之前与一般情况的插入不同,而如果待插入数插入在它之前的数列中,则用于遍历链表的指针tmp_ta…
Problem: Sort a linked list using insertion sort. The node of the linked list is defined as: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ The insertion sor…
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, 1, and 2 to represent the color red, white, and bl…
Sort a linked list in O(n log n) time using constant space complexity. 排序问题是我们遇到的一个老问题,从大一开始我们就学习了各种排序算法,大部分是基于数组的,比如冒泡排序,插入排序,快速排序等.这其中冒泡排序,插入排序的算法复杂度都是O(n2),插入排序的时间复杂度为O(nlogn).对于这个题目,显然我们不能使用冒泡排序和插入排序,因为这样时间复杂度不满足要求,那么链表适合用快速排序吗?答案是肯定的,链表的结构还是非常方便…
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, 1, and 2 to represent the color red, white, and bl…
Sort a linked list in O(n log n) time using constant space complexity. 题目要求在常数控件内以O(nlogn)的事件复杂度来排序链表. 常数空间内我没有实现,O(nlogn)的话使用归并排序就可以了吗, 下面是代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x…
原题如下面,这意味着无序排列(由0,1,2组成).一号通.组织成若干阵列0-几个1-几个2这样的序列. 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…
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... Example:(1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6]. (2) Given nums = [1, 3, 2, 2, 3, 1], one possible answer is [2, 3…
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4]. 这道题让我们求摆动排序,跟Wiggle Sort II相比起来,这道题的条件宽松很多,只因为多了一个等号…
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, 1, and 2 to represent the color red, white, and bl…
Problem Link: https://oj.leetcode.com/problems/validate-binary-search-tree/ We inorder-traverse the tree, and for each node we check if current_node.val > prev_node.val. The code is as follows. # Definition for a binary tree node # class TreeNode: #…
Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder traversal of a binary search tree should be a sorted array. Therefore, we can compare each node with its previous node in the inorder to find the two…