数组与链表
Taohongfei_huster
这个作者很懒,什么都没留下…
展开
-
179. Largest Number
题目如下:题目说的是用数组中的元素拼接成一个新的数,求这个数的最大值。考虑最简单的情况:数组中的数都是一位数,如nums[1,5,8],要拼接出一个尽可能大的数,首先对其进行排序,为8,5,1,然后将其拼接起来,即为所求(851)。同理,这道题也可以用排序解决,只不过要重新定义一种排序的规则。排序的规则定义了如何判断两个数的大小(传统意义上,从高位开始依次比较),对于该题,排序规则...原创 2018-09-15 21:28:10 · 113 阅读 · 0 评论 -
128. Longest Consecutive Sequence
排序法就不说了。首先,将数组的元素放入set中,一是去重,二是可以在o(1)的时间内访问。选区nums中的元素n,判断 n+1,n+2...是否在set中,如果在,则更新连续子序列的长度,如果不在,换下一个元素。注意,为了避免重复,我们总是选区满足n-1不在set中这一条件下的n作为序列的开头。(如遍历过2,3,4,就不用再遍历3,4)。class Solution { publ...原创 2018-12-28 21:34:15 · 196 阅读 · 0 评论 -
560. Subarray Sum Equals K
最容易想到的是暴力法,依次算出每个连续子数组的和,与k比较:class Solution { public int subarraySum(int[] nums, int k) { int count=0; for(int i=0;i<nums.length;i++){ int sum=0; f...原创 2018-12-23 17:15:23 · 161 阅读 · 0 评论 -
300. Longest Increasing Subsequence
暴力法:采用递归,如果当前元素大于pre,则序列中包含当前元素,长度加1,如果小于等于pre,考虑下一个元素,结果返回两种情况的最大值。public class Solution { public int lengthOfLIS(int[] nums) { return lengthofLIS(nums, Integer.MIN_VALUE, 0); ...原创 2018-12-03 20:41:53 · 119 阅读 · 0 评论 -
287. Find the Duplicate Number
方法一:应用鸽笼原理进行二分法查找因为在1~n的范围内最多只能放n个不同的数字,但现在数组放了n+1个数字,所以至少有一个重复。对于在1~n范围内的某个数字m,那么数组中小于等于m的数字最少有m个,并且刚好为m的时候,1~m之间不会有重复,比m大时,重复数字必在1~m中。public class Solution { public int findDuplicate(in...原创 2018-11-30 20:22:09 · 153 阅读 · 0 评论 -
141. Linked List Cycle&&142. Linked List Cycle II
解法思想:算法思想就是设置一个快指针fp和一个慢指针sp,两个指针起始同时指向head节点,其中快指针每次走两步,慢指针每次走一步,那么如果链表有环的话他们一定能够相遇。可以想象两个人同时从操场上起跑,其中甲的速度是乙速度的2倍,那么当乙跑完一圈的时候甲也跑了两圈,他们一定能够相遇。/** * Definition for singly-linked list. * class L...原创 2018-11-26 16:59:14 · 78 阅读 · 0 评论 -
148. Sort List
采用归并排序,先将链表一分为二,对两部分进行排序,之后再合并两个排好序的链表。 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */pub...原创 2018-11-29 11:22:06 · 101 阅读 · 0 评论 -
238. Product of Array Except Self
用两个数组left和right,left[i]=nums[0]*nums[1]...nums[i-1],right[i]=nums[i+1]*nums[i+2]...*nums[len-1],最终,res[i]=left[i]*right[i]。class Solution { public int[] productExceptSelf(int[] nums) { ...原创 2018-11-18 20:26:45 · 72 阅读 · 0 评论 -
215.Kth Largest Element in an Array
有两种思路,一种是快速选择法,一种是优先队列(最小堆)。快选: public class Solution { public int findKthLargest(int[] a, int k) { int n = a.length; int p = quickSelect(a, 0, n - 1, n - k + 1); return a[p];...原创 2018-11-08 16:21:16 · 128 阅读 · 0 评论 -
232. Implement Queue using Stacks
用两个栈。 class MyQueue { Stack<Integer> s1,s2; /** Initialize your data structure here. */ public MyQueue() { s1 = new Stack<Integer>(); s2 = new Stack<In...原创 2018-11-13 20:26:17 · 106 阅读 · 0 评论 -
225. Implement Stack using Queues
class MyStack { Queue<Integer> q; /** Initialize your data structure here. */ public MyStack() { q = new LinkedList<>(); } /** Push element x ont...原创 2018-11-13 20:11:24 · 109 阅读 · 0 评论 -
349. Intersection of Two Arrays&&350. Intersection of Two Arrays II
用两个 hash sets(Time complexity: O(n))public class Solution { public int[] intersection(int[] nums1, int[] nums2) { Set<Integer> set = new HashSet<>(); Set<Intege...原创 2018-11-09 16:51:07 · 112 阅读 · 0 评论 -
234.Palindrome Linked List
直观解决方法的空间复杂度为o(n),判断回文数通常是从两端往中间进行比较。考虑到链表特性及对时间,空间复杂度的要求,有两种思路:一、将链表一分为二,后半部分翻转,然后依次比较每个元素是否相等。/** * Definition for singly-linked list. * public class ListNode { * int val; * ListN...原创 2018-10-25 16:46:59 · 190 阅读 · 0 评论 -
209.Minimum Size Sbuarray Sum
用滑动窗口法,维护两个变量left,right,当它们之间的数组和sum>=s时,逐步将left往右移,在保证sum>=s时,更新sum和len。当sum<s时,将right往右移,直到sum>=s时,重复刚刚的步骤。class Solution { public int minSubArrayLen(int s, int[] nums) { ...原创 2018-10-25 15:18:03 · 124 阅读 · 2 评论 -
454. 4Sum II
这道题让我五味杂陈(手动哭笑)。果然,求组合数和求具体的组合情况的思路还是很不一样的。class Solution { public int fourSumCount(int[] A, int[] B, int[] C, int[] D) { // K: Sum, V: freq Map<Integer, Integer> map =...原创 2018-09-28 19:07:50 · 119 阅读 · 0 评论 -
摩尔投票法
class Solution { public int majorityElement(int[] nums) { int cur = nums[0]; int count = 1; for (int i = 1; i < nums.length; i++) { if (count == 0) { ...原创 2018-10-04 13:41:16 · 579 阅读 · 1 评论 -
169. Majority Element&&229. Majority Element II
读题很重要!// Sortingpublic int majorityElement1(int[] nums) { Arrays.sort(nums); return nums[nums.length/2];}// Hashtable public int majorityElement2(int[] nums) { Map<Integer, In...原创 2018-10-04 11:53:36 · 209 阅读 · 0 评论 -
239. Sliding Window Maximum
最先想到的办法是每个窗中都求最大值,唯一的技巧是保存上一个最大值的位置pos,如果pos出了窗,则遍历当前窗求最值,否则,只需比较窗右边的值和最大值即可。class Solution { public int[] maxSlidingWindow(int[] nums, int k) { if (nums.length == 0 || nums == n...原创 2019-01-03 21:41:20 · 178 阅读 · 0 评论