leetcode
文章平均质量分 55
Leon__CAI
这个作者很懒,什么都没留下…
展开
-
双调数组查找(Bitonic search)
数组查找是一个非常常见的问题,u原创 2014-11-04 16:04:50 · 2820 阅读 · 9 评论 -
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, whe原创 2015-01-07 16:23:47 · 869 阅读 · 1 评论 -
LeetCode---Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link原创 2015-01-07 19:49:40 · 639 阅读 · 0 评论 -
LeetCode---Longest Substring Without Repeating Characters
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-07 21:03:20 · 620 阅读 · 0 评论 -
LeetCiode--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, "()" and "()[]{}" are all va原创 2015-01-08 23:45:40 · 551 阅读 · 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). Fin原创 2015-01-08 21:14:12 · 568 阅读 · 0 评论 -
LeetCode--Regular Expression Matching
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input st原创 2015-01-08 17:42:56 · 654 阅读 · 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:Element原创 2015-01-08 23:07:35 · 698 阅读 · 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, the原创 2015-01-08 23:38:38 · 473 阅读 · 0 评论 -
LeetCode--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.Solutions:/** * Definition for singly-linked list.原创 2015-01-08 23:56:11 · 480 阅读 · 0 评论 -
LeetCode--Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity./** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *ne原创 2015-01-09 00:11:33 · 672 阅读 · 0 评论 -
LeetCode--String to Integer (atoi)
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input ca原创 2015-01-07 23:49:24 · 526 阅读 · 0 评论 -
LeetCode---ZigZag Conversion
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I原创 2015-01-07 22:54:41 · 527 阅读 · 0 评论 -
LeetCode--Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.class Solution {public: bool isPalindrome(int x) { if(x < 0) return false; int org =原创 2015-01-08 15:05:04 · 572 阅读 · 0 评论 -
LeetCode--Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit st原创 2015-01-08 20:25:10 · 561 阅读 · 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 exact原创 2015-01-08 22:57:54 · 652 阅读 · 0 评论 -
LeetCode---Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.Solution:class Solution {public: string longestCommonPrefix(vector &strs) { vector s = strs原创 2015-01-08 23:20:18 · 564 阅读 · 0 评论 -
LeetCode--Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321class Solution {public: int reverse(int x) { if(x>2147483647 || x <= -2147483648 |原创 2015-01-07 23:23:51 · 535 阅读 · 0 评论 -
LeetCode---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原创 2015-01-11 14:23:38 · 545 阅读 · 0 评论 -
LeetCode--Search for a Range
Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target is not found原创 2015-01-11 16:12:08 · 559 阅读 · 0 评论 -
LeetCode--Search Insert Position
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-11 16:19:31 · 551 阅读 · 0 评论 -
LeetCode---Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.c原创 2015-01-11 14:30:38 · 591 阅读 · 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-01-10 12:20:13 · 658 阅读 · 0 评论 -
LeetCode--Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is原创 2015-01-11 22:16:47 · 446 阅读 · 0 评论 -
LeetCode--Implement strStr()
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Update (2014-11-02):The signature of the function had been updat原创 2015-01-11 22:58:24 · 474 阅读 · 0 评论 -
LeetCode--Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as原创 2015-01-12 00:20:21 · 571 阅读 · 0 评论 -
LeetCode--Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the原创 2015-01-12 14:52:53 · 575 阅读 · 0 评论 -
LeetCode--Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".class Solution {public: string addBinary(string a, string b) {原创 2015-01-12 16:32:05 · 543 阅读 · 0 评论 -
LeetCode--Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.二分查找法:class Solution {public: int sqrt(int x) { int high = INT_MAX; int low = 0; while(low原创 2015-01-12 16:43:38 · 515 阅读 · 0 评论 -
LeetCode--Subsets
Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For exa原创 2015-01-13 09:43:22 · 559 阅读 · 0 评论 -
LeetCode--Anagrams
Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.先将原字符串数组中的字符串单独排序,如:bac-->abc,然后对整个的数组排序,此时数组中相邻的字符串如果相等,则原数组中的两字符串必满足条件,将其加入到结原创 2015-01-12 11:03:12 · 553 阅读 · 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] ha原创 2015-01-12 11:34:31 · 619 阅读 · 0 评论 -
LeetCode---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.Note: A word is原创 2015-01-12 12:57:54 · 548 阅读 · 0 评论 -
LeetCode--Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]原创 2015-01-12 13:38:55 · 507 阅读 · 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./** * Definition for singly-lin原创 2015-01-12 14:21:42 · 508 阅读 · 0 评论 -
LeetCode--Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?递推公式T(n) = T(n-1) + T(n-2);原创 2015-01-12 16:55:55 · 562 阅读 · 0 评论 -
LeetCode--Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].Note: Recursive solutio原创 2015-01-13 19:13:44 · 493 阅读 · 0 评论 -
LeetCode---Pow(x, n)
Implement pow(x, n).class Solution {public: double pow(double x, int n) { if(x == 1) return 1; if(x == -1) { if(n%2 == 0)原创 2015-01-12 11:23:51 · 583 阅读 · 0 评论 -
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 at原创 2015-01-12 16:06:42 · 559 阅读 · 0 评论 -
LeetCode--Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space?A straight forward solution using O(m原创 2015-01-13 09:17:04 · 633 阅读 · 0 评论