
数据结构与算法
文章平均质量分 62
竹影林风
写一个方法,去重构整个世界!
展开
-
求解最大子序列和
从一串数据中算出最大的子序列和,这里提供了三种方法,方案一到方案三中算法由劣到优。例如:-2,11,-4,13,-5,-2,答案为20(从A2到A4)方案一:public static int maxSubSum(int a []){ int maxSum=0; for(int i=0; i for(int j=i; j原创 2013-04-09 21:16:27 · 536 阅读 · 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 hav原创 2015-03-09 09:59:16 · 376 阅读 · 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 tripl原创 2015-03-08 19:35:20 · 494 阅读 · 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原创 2015-03-09 11:19:29 · 525 阅读 · 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:原创 2015-03-09 21:01:09 · 364 阅读 · 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原创 2015-03-10 14:44:04 · 472 阅读 · 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原创 2015-03-10 17:11:45 · 420 阅读 · 0 评论 -
leetcode 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:"((()))", "(()())", "(())()", "()(()原创 2015-03-10 19:45:03 · 367 阅读 · 0 评论 -
leetcode Merge k Sorted Lists
题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.解题思路:利用一个优先队列实现程序的模拟,优先队列根据ListNode的val值大小进行排序。代码:public ListNode mergeKLists(Array原创 2015-03-12 13:04:14 · 377 阅读 · 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原创 2015-03-12 14:37:49 · 378 阅读 · 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原创 2015-03-12 16:38:31 · 469 阅读 · 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 p原创 2015-03-13 14:25:12 · 438 阅读 · 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 len原创 2015-03-13 14:48:47 · 400 阅读 · 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.基本字符IVXLCDM相应的阿拉伯原创 2015-03-08 14:03:57 · 554 阅读 · 0 评论 -
leetcode Longest Common Prefix
题目:Write a function to find the longest common prefix string amongst an array of strings.解题思路:最长的公共字符串肯定不会超过第一个字符串的长度,所以以第一个为比较的基础,每次比较时,length都会逐步缩短。代码: public String longestCommonPrefi原创 2015-03-08 15:16:51 · 363 阅读 · 0 评论 -
leetcode Reverse Integer
题目:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321解题思路:这里需要注意几个问题,一是x尾数为0的情况,二是x反过来的时候溢出的情况。代码:public int reverse(int x) { long su原创 2015-03-05 16:08:29 · 402 阅读 · 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.方法一:解题思原创 2015-03-04 20:46:45 · 402 阅读 · 0 评论 -
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原创 2015-03-04 17:23:23 · 374 阅读 · 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原创 2015-03-05 14:27:59 · 362 阅读 · 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原创 2015-03-04 17:31:00 · 394 阅读 · 0 评论 -
leetcode TwoSum
题目如下: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 ta原创 2015-03-04 17:18:46 · 524 阅读 · 0 评论 -
leetcode Median of Two Sorted Arrays
题目:There are two sorted arrays A and B 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)).解题思路:该方法的核心是将原问题转变成一个寻找第原创 2015-03-04 17:34:01 · 509 阅读 · 0 评论 -
leetcode Palindrome Number
题目:Determine whether an integer is a palindrome. Do this without extra space.解题思路:负数不是回文数字,想办法在不增加额外空间的情况下将数字反转。代码:public boolean isPalindrome(int x) { int copy = x; int原创 2015-03-07 14:07:24 · 341 阅读 · 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原创 2015-03-07 13:19:19 · 360 阅读 · 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原创 2015-03-07 16:48:20 · 395 阅读 · 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,原创 2015-03-07 18:50:35 · 403 阅读 · 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.基本字符IVXLCDM相应的阿拉伯数字原创 2015-03-08 13:35:10 · 379 阅读 · 0 评论 -
leetcode Implement strStr()
题目:mplement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.方法一:解题思路:普通的算法思路很简单,直接两个指针遍历就ok了,但是效率一般,比较出名的是KMP算法。代码原创 2015-03-14 14:18:30 · 432 阅读 · 0 评论