LeetCode题解
文章平均质量分 69
EdmondYoung
这个作者很懒,什么都没留下…
展开
-
LeetCode2.1.5(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. e overall run time complexity should be O(log(m + n)).事实上,本题可以演变为在log(m+n)的时间内找出两个排序数组合并后原创 2015-08-22 16:29:38 · 326 阅读 · 0 评论 -
LeetCode7.3(Search a 2D Matrix)
Write an efficient algorithm that searches for a value in anmn matrix. is matrix has the followingproperties:• Integers in each row are sorted from left to right.• The first integer of each ro原创 2015-09-01 22:33:17 · 274 阅读 · 0 评论 -
leetCode3.1(Valid Palindrome)
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoringcases.For example,”A man, a plan, a canal: Panama” is a palindrome.”race a car” is not a p原创 2015-09-01 21:43:09 · 242 阅读 · 0 评论 -
LeetCode7.2(Search Insert Position)
Given a sorted array and a target value, return the index if the target is found. If not, return the indexwhere it would be if it were inserted in order.You may assume no duplicates in the array.原创 2015-09-01 22:31:09 · 241 阅读 · 0 评论 -
leetCode3.3(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 andask yourself what are the possible input cas原创 2015-09-01 21:56:39 · 240 阅读 · 0 评论 -
leetCode4.1.2(Longest Valid Parentheses)
Given a string containing just the characters ’(’ and ’)’, find the length of the longest valid (wellformed) parentheses substring.For ”(()”, the longest valid parentheses substring is ”()”, which h原创 2015-09-01 22:17:24 · 276 阅读 · 0 评论 -
LeetCode7.1(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 in t原创 2015-09-01 22:28:05 · 249 阅读 · 0 评论 -
LeetCode题解2.1.7~2.1.8
2.1.7 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 ad原创 2015-08-20 17:14:00 · 326 阅读 · 0 评论 -
LeetCode2.1.12(Next Permutation)
2.1.12 Next Permutation描述Implement next permutation, which rearranges numbers into the lexicographically next greater permutationof numbers.If such arrangement is not possible, it must rearran原创 2015-08-22 16:42:55 · 293 阅读 · 0 评论 -
LeetCode2.1.20(Set Matrix Zeroes)
2.1.20 Set Matrix Zeroes描述Given a m n matrix, if an element is 0, set its entire row and column to 0. Do it in place.Follow up: Did you use extra space?这道题其实不难,关键是要注意顺序,先检查第一行与第一列有无零元素,然后保存之原创 2015-08-22 17:04:42 · 377 阅读 · 0 评论 -
LeetCode2.1.21(Gas Station)
很经典的一道题也是很有意思的题目,网上的参考文章很多,思路就不给出了,直接上代码吧 public static int solution2_1_21(int[] gas,int[] cost){ int start=0,tank=0,total=0; for(int i=0;i<gas.length;i++){ tank+=gas[i]-cost[i]; total+=ga原创 2015-08-22 17:08:08 · 251 阅读 · 0 评论 -
LeetCode2.1.22 (Candy)
2.1.22 Candy描述ere 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原创 2015-08-22 17:10:39 · 266 阅读 · 0 评论 -
LeetCode2.1.11(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原创 2015-08-22 16:40:32 · 217 阅读 · 0 评论 -
LeetCode6.7(Sort Colors)
Given an array with n objects colored red, white or blue, sort them so that objects of the same colorare adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0原创 2015-09-01 22:24:49 · 204 阅读 · 0 评论 -
LeetCode3.2(Implement strStr())
Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack这道题最好的方法自然是KMP算法,但是事实上,使用暴力穷举的方法也能获得相当不错的结果。public static int solutio原创 2015-09-01 21:46:03 · 290 阅读 · 0 评论 -
leetCode4.1.1(Valid Parentheses)
描述Given a string containing just the characters ’(’, ’)’, ’{’, ’}’, ’[’ and ’]’, determine if theinput string is valid.The brackets must close in the correct order, ”()” and ”()[]” are all valid原创 2015-09-01 22:13:29 · 232 阅读 · 0 评论 -
LeetCode2.1.17(Plus One)
2.1.17 Plus One描述Given a number represented as an array of digits, plus one to the number.//注意这里的数组长度比数据本身长度长一位,用来处理最高位进位情况 public static void solution2_1_17(int[] array,int n){ int flag=原创 2015-08-22 16:47:29 · 232 阅读 · 0 评论 -
LeetCode2.1.10(4Sum)
LeetCode中的4Sum问题,基本是3Sum的改进版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.原创 2015-08-22 16:37:48 · 316 阅读 · 0 评论 -
LeetCode2.2.1(Add Two Numbers)
You are given two linked lists representing two non-negative numbers. e digits are stored in reverseorder and each of their nodes contain a single digit. Add the two numbers and return it as a link原创 2015-08-29 09:29:14 · 273 阅读 · 0 评论 -
LeetCode2.2.2(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->nullptr, m = 2 and n = 4,return 1->4->3->2->5->nullptr.Note: Given m, n satisfy the原创 2015-08-29 09:34:42 · 664 阅读 · 0 评论 -
LeetCode2.2.3(Partition List)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greaterthan or equal to x.You should preserve the original relative order of the nodes in each of原创 2015-08-29 09:37:57 · 246 阅读 · 0 评论 -
LeetCode2.2.5(Remove Duplicates from Sorted List II)
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbersfrom the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1-原创 2015-08-29 09:48:28 · 197 阅读 · 0 评论 -
LeetCode2.2.6(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->nullptr and k = 2, return 4->5->1->2->3->nullptr.这道题思路很巧妙,将尾节点next 指针指向首节点,形成一个环,接原创 2015-08-29 10:02:42 · 232 阅读 · 0 评论 -
LeetCode2.2.7(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 linked l原创 2015-08-29 10:08:20 · 224 阅读 · 0 评论 -
LeetCode2.2.9(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-08-29 10:17:24 · 286 阅读 · 0 评论 -
LeetCode题解2.1.3~2.1.4
2.1.3 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 t原创 2015-08-16 15:15:47 · 427 阅读 · 0 评论 -
LeetCode题解2.1.6
2.1.6 Longest Consecutive Sequence描述Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example, Given [100, 4, 200, 1, 3, 2], longest consecut原创 2015-08-16 16:20:49 · 276 阅读 · 0 评论 -
LeetCode题解第二章线性表2.1.1~2.1.2
代码如下:public static int solution2_1_1(int[] array,int n){ int j=1,k=1; while(j<n){ //注意这里应该是a[j]!=a[k-1]而非a[j]!=a[j-1] //对于这道题目,后一种写法照样可以得出正确答案;但是考虑下一题就会发现,前一种写法明显更加普适 if(array[j]!=array原创 2015-08-15 22:21:48 · 392 阅读 · 0 评论 -
LeetCode2.1.23~2.1.24(Single Number与Single Number II)
这又是两道利用逻辑运算和位运算来得出结果的题目代码如下2.1.23 Single Number描述Given an array of integers, every element appears twice except for one. Find that single one.Note: Your algorithm should have a linear runtime原创 2015-08-22 17:16:45 · 267 阅读 · 0 评论