
算法
文章平均质量分 69
qibobo
这个作者很懒,什么都没留下…
展开
-
算法:二叉树的前序遍历
Binary Tree Preorder Traversal原创 2014-11-20 15:33:56 · 554 阅读 · 0 评论 -
[leetcode]14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.比较简单,直接上代码。java代码public class Solution { public String longestCommonPrefix(String[] strs) { if(s原创 2016-10-15 21:49:09 · 250 阅读 · 0 评论 -
[leetcode]15. 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: The solution set must not contain原创 2016-10-17 22:19:46 · 252 阅读 · 0 评论 -
[leetcode]16. 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原创 2016-10-17 22:34:23 · 256 阅读 · 0 评论 -
[leetcode]17. 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:Di原创 2016-10-18 23:20:01 · 268 阅读 · 0 评论 -
[leetcode]18. 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: The solution原创 2016-10-18 23:25:16 · 288 阅读 · 0 评论 -
[leetcode]19. 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原创 2016-10-19 00:03:11 · 261 阅读 · 0 评论 -
[leetcode]20. 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原创 2016-10-19 20:22:25 · 382 阅读 · 0 评论 -
[leetcode]21. 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.直接上代码了。java/** * Definition for singly-linked list原创 2016-10-19 20:39:45 · 251 阅读 · 0 评论 -
[leetcode] 1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution.Example:Given nums =原创 2016-10-07 17:30:22 · 355 阅读 · 0 评论 -
[leetcode] 2. 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原创 2016-10-07 21:34:40 · 266 阅读 · 0 评论 -
[leetcode]22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:[ "((()))", "(()())", "(())()", "()(())原创 2016-11-01 18:30:32 · 280 阅读 · 0 评论 -
[leetcode]23. Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.Leetcode 21题是合并两个链表。合并K个链表的方法就是2的进一步。有两种方法:1 归并法。即每次链表数组中相邻的两个链表合并,结果为一个新的链表数组,重复之前的流程知道数组中只有一个元原创 2016-11-01 18:38:53 · 201 阅读 · 0 评论 -
[leetcode]24. 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原创 2016-11-01 18:43:27 · 338 阅读 · 0 评论 -
[leetcode]25. 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原创 2016-11-01 18:48:09 · 313 阅读 · 0 评论 -
[leetcode]13. Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.这道题和12题正好相反,但是要比12题难一点。最重要的还是要熟悉对应表。1~9: {"I", "II", "III", "IV", "V", "VI", "VI原创 2016-10-15 18:21:39 · 251 阅读 · 0 评论 -
[leetcode]12. Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.这道题基本没什么算法上的难点,就是需要知道罗马字母和整数的对应关系就行了。1~9: {"I", "II", "III", "IV", "V", "VI", "V原创 2016-10-15 18:13:54 · 281 阅读 · 0 评论 -
算法:有序数组转为平衡的二叉搜索树
Convert Sorted Array to Binary Search TreeGiven an array where elements are sorted in ascending order, convert it to a height balanced BST.二叉搜索树的特点就是原创 2014-11-20 11:34:57 · 717 阅读 · 0 评论 -
算法:帕斯卡三角
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]原创 2014-11-24 23:07:43 · 798 阅读 · 0 评论 -
算法 Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or equal to m +n) to hold additional elements from B原创 2014-11-24 23:11:26 · 532 阅读 · 0 评论 -
算法 Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each原创 2014-11-24 23:12:43 · 534 阅读 · 0 评论 -
算法 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. You m原创 2014-11-24 23:14:24 · 579 阅读 · 0 评论 -
算法 Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?代码:原创 2014-11-24 23:09:50 · 485 阅读 · 0 评论 -
算法 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 valid b原创 2014-11-24 23:16:17 · 545 阅读 · 0 评论 -
算法 二叉树的各种遍历
二叉树的遍历方式基本就是前序遍历,中序遍历,后序遍历和层次遍历。从代码的角度来说,前三种最简单的就是用递归了,代码会非常简洁。但是递归有一个缺陷,就是当二叉树的节点非常多的时候,层次深的递归会不停的进行程序的压栈和出栈操作,效率比较低。这里就不写递归算法了,只写四种遍历的非递归算法。先定义二叉树的节点如下:/** * Definition for binary tree * pub原创 2014-12-15 11:21:42 · 774 阅读 · 0 评论 -
[leetcode]3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "原创 2016-10-09 00:03:03 · 254 阅读 · 0 评论 -
[leetcode]4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).Example 1:nums1 =原创 2016-10-09 21:31:38 · 232 阅读 · 0 评论 -
[leetcode]5. 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.本题是求一个字符串的最长的回文子串。原创 2016-10-10 22:09:20 · 226 阅读 · 0 评论 -
[leetcode]7. Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Have you thought about this?Here are some good questions to ask before c原创 2016-10-10 22:47:49 · 269 阅读 · 0 评论 -
[leetcode] 9. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of convertin原创 2016-10-13 23:07:45 · 278 阅读 · 0 评论 -
[leetcode]11. 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原创 2016-10-13 23:21:08 · 263 阅读 · 0 评论 -
[leetcode]26. 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-12-08 17:56:25 · 380 阅读 · 0 评论