
LeetCode
文章平均质量分 71
CiaoLiang
努力学习30种搬砖技巧。
展开
-
[LeetCode刷题记录]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".思路:每次遇到空格把每个单词反转,然后把整个字符串反转。边界条件注意:开始的多个空格跳过,结尾的多个空格删掉。单词中间间隔的多个空格原创 2015-04-08 12:58:02 · 547 阅读 · 0 评论 -
[LeetCode]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-04-18 18:26:35 · 374 阅读 · 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 '.'.LeetCode Source分析:利原创 2015-04-18 13:07:15 · 325 阅读 · 0 评论 -
[LeetCode]Word Search
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 vertically原创 2015-04-18 19:43:19 · 321 阅读 · 0 评论 -
[LeetCode刷题记录]Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume原创 2015-04-09 14:18:18 · 609 阅读 · 0 评论 -
[LeetCode]Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".思路:把两个二进制串相加,返回加的结果。从每个字符串最后一位开始加,同时要考虑到进位位。ret+=((a[i]-'0')^(b[i-m+n]-'原创 2015-04-18 11:12:25 · 366 阅读 · 0 评论 -
[LeetCode]Happy Number
Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares原创 2015-04-22 13:22:05 · 390 阅读 · 0 评论 -
[LeetCode]Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list./** * Definition for singly-l原创 2015-04-22 18:28:34 · 445 阅读 · 0 评论 -
[LeetCode]Candy
There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least on原创 2015-04-20 21:37:14 · 372 阅读 · 0 评论 -
[LeetCode]Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited numb原创 2015-04-19 09:45:43 · 331 阅读 · 0 评论 -
[LeetCode]Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.Note: The result may be ve原创 2015-04-19 10:35:47 · 414 阅读 · 0 评论 -
[LeetCode]Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: ["2", "1",原创 2015-04-19 12:33:19 · 317 阅读 · 0 评论 -
[LeetCode刷题记录]190-191 Number of 1 Bits & Reverse Bits
[191 Numbers of 1 Bits][EASY]Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11'原创 2015-03-12 12:58:46 · 461 阅读 · 0 评论 -
[LeetCode刷题记录]Single Number II
Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without u原创 2015-04-08 13:06:44 · 439 阅读 · 0 评论 -
[LeetCode]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 --> 5LeetCode Source分析:保存待删原创 2015-04-23 21:37:55 · 365 阅读 · 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 t原创 2015-04-13 10:12:06 · 335 阅读 · 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-04-08 13:17:35 · 399 阅读 · 0 评论 -
[LeetCode刷题记录]Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array retur原创 2015-04-08 13:26:41 · 309 阅读 · 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 of原创 2015-04-08 13:21:50 · 349 阅读 · 0 评论 -
[LeetCode]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-04-24 11:02:16 · 392 阅读 · 0 评论 -
[LeetCode]Merge Intervals
Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].思路:首先对元素进行排序,按照start升序排序。然后依次合并。但是代码运行时间略长600m原创 2015-04-24 09:46:51 · 387 阅读 · 0 评论 -
[LeetCode]Divide Two Integers
Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.LeetCode Source分析:这题思路很奇特。最很容易想到的一个方法是一直做减法,然后计数,但是提交之后显示超时。在网上找到一种解法,原创 2015-04-24 10:44:20 · 445 阅读 · 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.分析:利用链表生成平衡二叉查找数。题目中要求生成平衡树,所以我们每次要取链表中间位置的值作为二叉树的一个根。然后依次从左边链表序列和右边链表序列取中间值来递归生成平衡树。特别原创 2015-04-17 00:21:32 · 495 阅读 · 0 评论 -
[LeetCode]Bitwise AND of Numbers Range
Given a range [m, n] where 0 For example, given the range [5, 7], you should return 4.思路:对[m,n]范围内所有的数取与,实际上最后结果就是m和n从最高前面有多少位同为1,然后把后面位全置零即可。比如m=4;n=7100&101&110=100 class Soluti原创 2015-04-17 23:55:24 · 429 阅读 · 0 评论 -
[LeetCode]Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1原创 2015-04-17 20:54:26 · 356 阅读 · 0 评论 -
[LeetCode]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.LeetCode Source/** * Definiti原创 2015-04-26 14:25:08 · 400 阅读 · 0 评论 -
[LeetCode]Find Peak Element
A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple peaks, i原创 2015-04-25 17:31:23 · 385 阅读 · 0 评论 -
[LeetCode]Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.思路:很简单的题目,判断好边界条件就可以了。LeetCode Sourceclass Solution {public: string longestCommonPrefix(vector& strs) {原创 2015-04-25 19:07:27 · 388 阅读 · 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-05-09 13:33:08 · 275 阅读 · 0 评论 -
[LeetCode]House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house原创 2015-04-13 19:44:36 · 396 阅读 · 0 评论 -
[LeetCode]Sort Colors
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原创 2015-05-09 13:29:07 · 418 阅读 · 0 评论 -
[LeetCode]Compare Version Numbers
Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 version2 return -1, otherwise return 0.You may assume that the version strings are non-empty and co原创 2015-05-09 13:21:47 · 403 阅读 · 0 评论 -
[LeetCode]Path Sum II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / \原创 2015-05-09 13:14:26 · 404 阅读 · 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-04-12 15:09:37 · 501 阅读 · 0 评论 -
[LeetCode]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?非递归:找好链表关系不难解决:/** * Definition for原创 2015-05-05 16:41:57 · 381 阅读 · 0 评论 -
[LeetCode]Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with anot原创 2015-05-02 10:56:36 · 354 阅读 · 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-05-26 11:12:37 · 410 阅读 · 0 评论 -
[LeetCode]Permutations
Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].思路:暴力求解,遍原创 2015-05-27 13:04:16 · 409 阅读 · 0 评论 -
[Python]快速排序
最近摸了下Pyhton,发现真心好用。写下快排来练手。import randomdef partition(a,lo,hi): i = lo j = hi+1 v = a[lo] while True: while a[i]<v: i = i+1 if i == hi: break j = j-1 ##特别注意 whil原创 2015-05-10 14:32:23 · 1357 阅读 · 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-05-28 11:31:55 · 489 阅读 · 0 评论