
刷题
lly0_0
虚心向学
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Leetcode -- Integer to Roman
题目:Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.分析:罗马数字,转到阿拉伯数字是相当有规律的。比如9 -- IX, 90 -- XC, 99 -- XCIX思路:map形式,将所有特殊的都存储起来原创 2015-08-08 17:40:46 · 382 阅读 · 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.分析: 将后面的k个node旋转到链表的开头。若k大于链表的长度,则原创 2015-08-15 09:40:45 · 400 阅读 · 0 评论 -
Leetcode -- Reverse Linked List II
题目: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the f原创 2015-08-15 10:17:18 · 366 阅读 · 0 评论 -
Leetcode -- Sort List
题目: Sort a linked list in O(n log n) time using constant space complexity.分析: O(n log n)有归并排序,快速排序和堆排序。针对链表,采用归并排序是比较合理的。快排在最坏的情况,复杂度是O(n^2)。思路:难点在于寻找链表的中点。代码: ListNode* sortList(ListNode* head) {原创 2015-08-14 18:55:08 · 605 阅读 · 1 评论 -
Leetcode -- 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. Note: You can only move either down or right原创 2015-08-29 23:03:38 · 351 阅读 · 0 评论 -
Leetcode -- Reorder List
题目: 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-08-15 18:21:21 · 525 阅读 · 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.这个问题,跟LCS的解题思路上原创 2015-07-30 00:17:14 · 436 阅读 · 0 评论 -
Leetcode -- Remove Duplicates from Sorted List II
题目: 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->原创 2015-08-16 16:48:45 · 353 阅读 · 0 评论 -
Leetcode -- Insertion Sort List
题目: Sort a linked list using insertion sort.分析: 插入排序。代码1:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NU原创 2015-08-16 21:19:08 · 374 阅读 · 0 评论 -
Leetcode -- Reorder List
题目: 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-08-15 20:30:34 · 356 阅读 · 0 评论 -
Leetcode -- 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原创 2015-08-16 17:51:04 · 321 阅读 · 0 评论 -
Leetcode -- 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.分析: 将排好序的链表,转为平衡二叉搜索树。思路: 链表的中间那个node是树的根节点,前面是左子树,后面是右子树。以此,不断递归就可以得到平衡二叉树。时间复杂度是O(N原创 2015-08-17 15:38:23 · 406 阅读 · 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]原创 2015-09-01 21:46:24 · 412 阅读 · 0 评论 -
Leetcode -- Bitwise AND of Numbers Range
题目: Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. For example, given the range [5, 7], you should return 4. Credits: Special原创 2015-09-01 21:16:23 · 367 阅读 · 0 评论 -
Leetcode -- Ugly Number II
题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first原创 2015-08-19 11:31:21 · 399 阅读 · 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原创 2015-08-14 17:25:16 · 342 阅读 · 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.分析: 在一个链表中,将重复val的node去掉原创 2015-08-14 16:59:30 · 273 阅读 · 0 评论 -
Leetcode -- Roman to Integer
题目:Given a roman numeral, convert it to an integer.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‘原创 2015-08-08 11:57:32 · 359 阅读 · 0 评论 -
Leetcede -- 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 wi原创 2015-08-23 15:01:55 · 338 阅读 · 0 评论 -
Leetcode -- Valid Sudoku
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’ A partially filled sud原创 2015-08-23 18:01:14 · 342 阅读 · 0 评论 -
Leetcode -- Add Binary
题目: Given two binary strings, return their sum (also a binary string). For example, a = “11” b = “1” Return “100”.、分析: 就是这么简单,二进制相加代码:string addBinary(string a, string b) { string str = "原创 2015-08-23 19:54:16 · 396 阅读 · 0 评论 -
Leetcode -- Missing Number
题目: Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array. For example, Given nums = [0, 1, 3] return 2. Note: Your algorithm should ru原创 2015-08-24 18:53:24 · 373 阅读 · 0 评论 -
Leetcode -- 3Sum
题目: 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) mu原创 2015-08-25 11:46:18 · 427 阅读 · 0 评论 -
Leetcode -- 3Sum Closest
题目: 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 exa原创 2015-08-25 11:52:36 · 373 阅读 · 0 评论 -
Leetcode -- Two Sum
题目: 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, w原创 2015-08-25 11:38:25 · 389 阅读 · 0 评论 -
Leetcode -- 4Sum
题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in原创 2015-08-25 12:00:05 · 328 阅读 · 0 评论 -
Leetcode -- Container With Most Water
题目: 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). Find tw原创 2015-08-11 15:23:02 · 619 阅读 · 0 评论 -
Leetcode -- Longest Common Prefix
题目: Write a function to find the longest common prefix string amongst an array of strings.分析: 一堆string里面,找到大家共有的从头部开始的字串思路: 遍历,复杂度O(N)。每次记下当前最长的字串,然后跟新的一个string比较,获取当前的最长字串。代码:string longestCommonPr原创 2015-08-11 17:16:28 · 467 阅读 · 0 评论 -
Leetcode -- Word Search(Important!)
题目: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertical原创 2015-08-27 20:38:39 · 342 阅读 · 0 评论 -
Leetcode -- 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, t原创 2015-08-14 12:26:19 · 392 阅读 · 0 评论 -
Leetcode -- Remove Linked List Elements
题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5分析: 删除链表中顶点值为val的node思路: 遍历链表,遇到满足原创 2015-08-14 16:35:27 · 500 阅读 · 0 评论 -
Leetcode -- Count Primes
题目: Description: Count the number of prime numbers less than a non-negative number, n. Credits: Special thanks to @mithmatt for adding this problem and creating all test cases.分析【引自:Leetcode Hint】:原创 2015-08-20 13:27:08 · 410 阅读 · 0 评论