leetcode
文章平均质量分 55
m0_38141582
这个作者很懒,什么都没留下…
展开
-
64. Minimum Path Sum
class Solution { public: int minPathSum(vectorvectorint>>& grid) { int m = grid.size(); int n = grid[0].size(); vectorint> cur(m, grid[0][0]); for (int i = 1; i < m转载 2017-07-11 10:30:42 · 125 阅读 · 0 评论 -
链表的归并排序算法
public static ListNode mergeTwoList(ListNode headA,ListNode headB){ ListNode rehead=new ListNode(0); ListNode pre=rehead; while(headA!=null && headB!=null){ if(headA.val pre.next=headA; headA=he原创 2017-09-19 10:14:43 · 342 阅读 · 0 评论 -
leetcode 154.Find Minimum in Rotated Sorted Array II
public int findMin(int[] num) { 3 if (num == null || num.length == 0) { 4 return 0; 5 } 6 7 int len = num.length; 8 if (len == 1) { 9转载 2017-09-27 11:38:55 · 108 阅读 · 0 评论 -
139. Word Break
1.对于这个问题首先想到的是递归方法,关于遍历的问题,不一定非要通过字符串下标来遍历字符串,通过遍历字典中的字符串更简洁明了,但是一般来说递归总是会有超时问题,遇到这种问题尽量使用动态规划方法: public boolean wordBreak(String s, List wordDict) { if(wordDict.size()==0){ return false; } if(转载 2017-09-11 09:54:35 · 138 阅读 · 0 评论 -
Leetcode 120. Triangle
解法一: 给出一个三角形状的整数list,从第一行向下移动,每次只能向下一行相邻的数移动,要求求出从第一行到最后一行所经过的数字的和最小的方法。这是一道动态规划的题目,第一种解法可以用一个二维数组存储从第一行到某行某个数的经过数的最小值,这是空间为o(n*n),对其中第i行(i>1)的第j个数(0 利用动态规划常规思想去解从上到下,但是有个问题就是由于是三角形形数据,二维数组不少空间都转载 2017-09-02 11:21:02 · 112 阅读 · 0 评论 -
Leetcode 132. Palindrome Partitioning II
public int minCut(String s) { int min = 0; int len = s.length() ; boolean[][] matrix = new boolean[len][len]; int[] cuts = new int[len + 1]; if (s.length() == 0 || s == null) { r转载 2017-09-03 11:08:32 · 99 阅读 · 0 评论 -
leetcode 140. Word Break II
140. Word Break II public boolean wordBreakcheck(String s, List dict) { if (s == null || s.length() == 0) return true; boolean[] res = new boolean[s.length() + 1]; res[0] = true;转载 2017-09-12 10:35:14 · 133 阅读 · 0 评论 -
Leetcode 188. Best Time to Buy and Sell Stock IV
public int maxProfit(int k, int[] prices) { int n = prices.length; if (n 1) return 0; //if k >= n/2, then you can make maximum number of transactions. if (k >= n/2) { int maxPro = 0; for转载 2017-10-21 21:33:02 · 151 阅读 · 0 评论 -
179. Largest Number
public String largestNumber(int[] nums) { if(nums==null || nums.length==0){ return ""; } String[] strs=new String[nums.length]; int count=0; for(int i=0;i if(nums[i]==0){ count++; } strs[i原创 2017-10-12 20:10:54 · 128 阅读 · 0 评论 -
Leetcode 189. Rotate Array
1. public void rotate(int[] nums, int k) { if(nums.length return; } int n=nums.length; int step=k%nums.length; int[] tmp=new int[step]; for(int i=0;i tmp[i]=nums[n-step+i]; } for(int i=转载 2017-10-22 19:59:36 · 103 阅读 · 0 评论 -
Leetcode 190. Reverse Bits
public int reverseBits(int n) { int result = 0; for (int i = 0; i result = result result+=n&1; n = n >> 1; } return result; }转载 2017-10-23 10:37:02 · 130 阅读 · 0 评论 -
Leetcode 198. House Robber
这道题应用动态规划的思想求解,动态规划需要注意的一个地方是数组有时没必要,可以用常量代替。 public int rob1(int[] nums){ int[][] dp=new int[nums.length+1][2]; for(int i=1;i dp[i][0]=Math.max(dp[i-1][0], dp[i-1][1]); dp[i][1]=dp[i-1][0]+n转载 2017-10-24 10:41:04 · 171 阅读 · 0 评论 -
Leetcode 199. Binary Tree Right Side View
1. public List rightSideView(TreeNode root) { List res=new ArrayList(); rightView(root,res,0); return res; } public void rightView(TreeNode cur, List res, int i) {原创 2017-10-25 09:25:06 · 166 阅读 · 0 评论 -
Leetcode 138. Copy List with Random Pointer
1.本道题的目标是对链表进行深度复制,首先第一种解法是利用map方便操作。旧结点为key,新节点为value。这么做的目的是为了第二遍扫描的时候我们按照这个哈希表把结点的随机指针接上。时间复杂度为O(n),空间复杂度为O(n)。 public RandomListNode copyRandomList(RandomListNode head) { if(head == null)转载 2017-09-10 10:11:31 · 122 阅读 · 0 评论 -
147. Insertion Sort List
public ListNode insertionSortList(ListNode head) { if(head==null){ return null; } if(head.next==null){ return head; } ListNode rehead=new ListNode(-1); rehead.next=head; ListNode pre=head; L转载 2017-09-17 21:56:48 · 231 阅读 · 0 评论 -
leetcode 72 edit distance
求两个字符串的编辑距离。核心思想依然是动态规划,但是递归的时间复杂度太大,尽量用数组解决这种问题。 public int minDistance(String word1, String word2) { int len1=word1.length()+1; int len2=word2.length()+1; int原创 2017-07-17 10:41:59 · 156 阅读 · 0 评论 -
Scramble String 动态规划
动态规划问题,涉及到动态规划问题基本思想是尽量使用数组 public boolean isScramble(String s1, String s2) { if (s1.length() != s2.length()) { return false; } int n = s1.length(); if (s1.equal原创 2017-07-30 11:06:37 · 238 阅读 · 0 评论 -
leetcode
Unique path解题,核心思想是动态规划,还有就是减少空间复杂度问题。 class Solution { int uniquePaths(int m, int n) { if (m > n) return uniquePaths(n, m); vectorint> cur(m, 1); for (int j = 1; j < n; j转载 2017-07-09 21:34:02 · 135 阅读 · 0 评论 -
Leetcode 97Interleaving String
本题核心思想是动态规划,涉及此类问题尽量不要用递归。 public boolean isInterleave(String s1, String s2, String s3) { // Note: The Solution object is instantiated only once and is reused by each test case. if (s原创 2017-08-08 21:24:56 · 136 阅读 · 0 评论 -
Leetcode Best Time to Buy and Sell Stock III
题目要求限定两次交易求最大收益,解决方法自然应该想到了二分法和动态规划 public int maxProfit(int[] prices) { if(prices==null || prices.length return 0; } int n=prices.length; int[] preProfit=new int[n]; int[] postProfit=new int[n原创 2017-08-21 11:36:12 · 127 阅读 · 0 评论 -
143. Reorder List
本题比较难想,关键是要找到思路。思路是按照三步来, 1.找到中间节点,断开。 2.把后半截单链表 反转一下一下。 3.再合并两个单链表。 public void reorderList(ListNode head) { if(head==null){ return; } ListNode slow=head; ListNode fast=head; while(原创 2017-09-14 10:15:21 · 154 阅读 · 0 评论 -
142. Linked List Cycle II
设:链表头是X,环的第一个节点是Y,slow和fast第一次的交点是Z。各段的长度分别是a,b,c,如图所示。环的长度是L。slow和fast的速度分别是qs,qf。 第一次相遇时slow走过的距离:a+b,fast走过的距离:a+b+c+b。 因为fast的速度是slow的两倍,所以fast走的距离是slow的两倍,有 2(a+b) = a+b+c+b,可以得到a=c(这个结论很重要!)转载 2017-09-13 11:22:06 · 127 阅读 · 0 评论 -
leetcode 130. Surrounded Regions
public void solve(char[][] board) { if(board==null || board.length return; } for(int i=0;i fill(board, 0, i); fill(board, board.length-1, i); } for(int i=0;i fill(board, i, 0); fill(board, i转载 2017-08-29 11:10:37 · 150 阅读 · 0 评论 -
128. Longest Consecutive Sequence
public int longestConsecutive(int[] nums) { if(nums==null || nums.length==0){ return 0; } HashSet set=new HashSet(); int len=1; for(int e:nums){ set.add(e)转载 2017-08-29 11:12:47 · 156 阅读 · 0 评论 -
leetcode 151. Reverse Words in a String
本题难度不大,核心在于对于小细节的处理,字符串的预处理很重要 public class Solution { public void reverseArray(String[] words) { int i = 0, n = words.length - 1; while (n > 2 * i) { String x = words[i]; words[i]原创 2017-09-23 10:23:28 · 120 阅读 · 0 评论 -
Leetcode 131. Palindrome Partitioning
public void solve(char[][] board) { if(board==null || board.length return; } for(int i=0;i fill(board, 0, i); fill(board, board.length-1, i); } for(int i=0;i fill(board, i, 0); fill(board, i转载 2017-08-30 10:33:55 · 135 阅读 · 0 评论 -
LRU Cache leetcode
1.自己想的 public class LRUCache { public LinkedList used=new LinkedList(); public Map map=new HashMap(); public int len; public int capacity; public LRUCache(int capacity) { this.capacity = capa原创 2017-09-16 10:52:41 · 153 阅读 · 0 评论 -
Leetcode 201. Bitwise AND of Numbers Range
第二种方法速度更快 1. public int rangeBitwiseAnd(int m, int n) { int i = 0; while (m != n) { m = m >> 1; n = n >> 1; i++; } return m } 2. public int rangeBitwiseAnd(int m, int n)转载 2017-10-25 11:10:18 · 131 阅读 · 0 评论