- 博客(34)
- 资源 (1)
- 收藏
- 关注
原创 [Lintcode]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".两个指针,每次遇到空格进行判定是否插入新单词并初始化index。Leading space会由于start=-1而避免。多个空格的情况同
2016-02-25 22:54:05 431
原创 [Lintcode]Reverse Linked List
ExampleFor linked list 1->2->3, the reversed linked list is 3->2->1ChallengeReverse it in-place and in one-pass值可以改动的话,用双指针,链表保持不变,更新值即可。值不可以改变的话,需要改变链表指针public class Solutio
2016-02-25 21:33:49 239
原创 [Lintcode]Reverse Integer
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer).ExampleGiven x = 123, return 321Given x = -123, return -321先转化为正数再使用取余和除法计算。需要注意的是处理
2016-02-25 18:05:38 312
原创 [Lintcode]Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.ExampleGiven linked list: 1->2->3->4->5->null, and n = 2.After removing the second node from the end, the li
2016-02-25 17:37:04 257
原创 [Lintcode]Remove Element
Given an array and a value, remove all occurrences of that value in place and return the new length.The order of elements can be changed, and the elements after the new length don't matter.
2016-02-24 23:43:02 224
原创 [Lintcode]Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.ExampleGiven 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.public class Solution {
2016-02-24 23:34:16 195
原创 [Lintcode] Remove Duplicates from Sorted Array II
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,
2016-02-24 22:54:34 187
原创 [Lintcode]Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with
2016-02-24 22:04:54 211
原创 [Lintcode]Recover Rotated Sorted Array Show result
Given a rotated sorted array, recover it to sorted array in-place.Example[4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5]ChallengeIn-place, O(1) extra space and O(n) time.找到数组的旋转点,旋转
2016-02-24 21:31:36 290
原创 [Lintcode]Product of Array Exclude Itself
Given an integers array A.Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WITHOUT divide operation.ExampleFor A = [1, 2, 3], return [6, 3, 2].分成两个数组分别储存(1,
2016-02-24 17:58:45 261
原创 [Lintcode]Plus One
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.Have you met this qu
2016-02-24 17:17:46 341
原创 [Lintcode]Permutation Index
Question :Given a permutation which contains no repeated number, find its index in all the permutations of these numbers, which are ordered in lexicographical order. The index begins at 1.
2016-02-24 16:56:57 424
原创 [Lintcode]Partition List
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
2016-02-23 23:44:44 249
原创 [Lintcode]Partition Array by Odd and Even
Partition an integers array into odd number first and even number second.ExampleGiven [1, 2, 3, 4], return [1, 3, 2, 4]ChallengeDo it in-place.双指针,一个指针从左至右寻找偶数,另一个从右至
2016-02-23 22:35:27 289
原创 *[Lintcode]O(1) Check Power of 2
Using O(1) time to check whether an integer n is a power of2.ExampleFor n=4, return true;For n=5, return false;ChallengeO(1) time若为2的幂次方,则只存在一位为1. 可以1)统计1的个数 2)与n-1做与
2016-02-23 06:00:32 533
原创 [Lintcode]Number of Islands
Given a boolean 2D matrix, find the number of islands.ExampleGiven graph:[ [1, 1, 0, 0, 0], [0, 1, 0, 0, 1], [0, 0, 0, 1, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 1]]return 3.
2016-02-23 05:21:58 310
原创 [Lintcode]Nth to Last Node in List
Find the nth to last element of a singly linked list. The minimum number of nodes in list is n.ExampleGiven a List 3->2->1->5->null and n = 2, return node whose value is 1.
2016-02-23 05:09:26 216
原创 *[Lintcode]Minimum Subarray
Given an array of integers, find the subarray with smallest sum.Return the sum of the subarray.ExampleFor [1, -1, -2, 1], return -3题目和最大子串类似,解法也类似public class Sol
2016-02-23 05:01:45 351
原创 [Lintcode]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.可以用递归,二维DP或者一维DP解决。三种算法原理大体相同,在优化上一维DP使用滚动数组所
2016-02-22 23:58:31 329
原创 [Lintcode]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.ExampleGiven a binary tr
2016-02-22 23:34:31 348
原创 [Lintcode]Merge Two Sorted Lists
Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list should be made by splicing together the nodes of the two lists and sorted in ascending order.p
2016-02-22 00:22:58 259
原创 *[Lintcode]Merge Sorted Array II
Merge two given sorted integer array A and B into a new sorted integer array.与上一道类似class Solution { /** * @param A and B: sorted integer array A and B. * @return: A new sor
2016-02-21 23:45:25 272
原创 [Lintcode]Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array.ExampleA = [1, 2, 3, empty, empty], B = [4, 5]After merge, A will be filled as [1, 2, 3, 4, 5]如果从数组A的ind
2016-02-21 22:13:13 203
原创 [Lintcode]Median
Given a unsorted array with integers, find the median of it. A median is the middle number of the array after it is sorted. If there are even numbers in the array, return the N/2-th number aft
2016-02-21 18:49:53 223
原创 [Lintcode]Maximum Subarray
Given an array of integers, find a contiguous subarray which has the largest sumExampleGiven the array [−2,2,−3,4,−1,2,1,−5,3], the contiguous subarray [4,−1,2,1]has the largest sum =
2016-02-19 18:38:19 367
原创 [Lintcode]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.ExampleGiven a binary tr
2016-02-19 17:52:01 335
原创 [Lintcode] Matrix Zigzag Traversal
Given a matrix of m x nelements (m rows, ncolumns), return all elements of the matrix in ZigZag-order.ExampleGiven a matrix:[ [1, 2, 3, 4], [5, 6, 7, 8], [9,10, 11, 12]]r
2016-02-18 23:25:43 248
原创 [Lintcode]Majority Number
Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it.要求:O(n) time and O(1) extra space投票算法, 一般需要再遍历一遍,为防止不存在majority num
2016-02-18 22:22:51 229
原创 [Lintcode]Longest Words
Given a dictionary, find all of the longest words in the dictionary.(do it in one pass)简单题,直接写代码class Solution { /** * @param dictionary: an array of strings * @return: an arra
2016-02-17 05:37:28 364
原创 [Lintcode]Longest Increasing Continuous Subsequence
Give an integer array,find the longest increasing continuous subsequence in this array.An increasing continuous subsequence:Can be from right to left or from left to right.Indices of the
2016-02-17 05:27:03 288
原创 [LintCode]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 exist, return 0.遍历每个字符,每次
2016-02-17 04:56:40 264
原创 [LintCode]Invert Binary Tree
Invert a binary tree.DFS递归,依次交换左右节点public class Solution { /** * @param root: a TreeNode, the root of the binary tree * @return: nothing */ public void invertBinaryTree(Tree
2016-02-16 18:48:03 230
原创 [LintCode]Insertion Sort List
Sort a linked list using insertion sort.ExampleGiven 1->3->2->0->null, return 0->1->2->3->null.1,使用dummy节点,便于处理插入位置为首位的问题。2,1个head指针,指向待处理list首位。3,1个临时变量,保存下一个head节点位置。此题一个指针+一个临时变
2016-02-16 18:33:55 271
原创 [LintCode]Insert Interval
[题目] : Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tree.ExampleGiven binary search tree as follow
2016-02-16 00:08:47 316
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人