• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅
上一页 1 ··· 15 16 17 18 19 20 21 下一页
2014年9月12日
Leetcode: Jump Game II
摘要: 难度:89,这道题参考了网上的解法:和Jump Game很像,只是原来的全局最优现在要分成step步最优和step-1步最优(假设当前步数是step)。当走到超过step-1步最远的位置时,说明step-1不能到达当前一步,我们就可以更新步数,将step+1。时间复杂度仍然是O(n),空间复杂度也是 阅读全文
posted @ 2014-09-12 12:49 neverlandly 阅读(316) 评论(0) 推荐(0)
Leetcode: Jump Game
摘要: Greedy Solution 阅读全文
posted @ 2014-09-12 05:28 neverlandly 阅读(401) 评论(0) 推荐(0)
2014年9月11日
Leetcode: N-Queens II
摘要: Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.跟N-Queen的考虑方式完全一样,NP问题,用循环递... 阅读全文
posted @ 2014-09-11 12:49 neverlandly 阅读(355) 评论(0) 推荐(0)
Leetcode: N-Queens
摘要: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integern, return all di... 阅读全文
posted @ 2014-09-11 10:01 neverlandly 阅读(500) 评论(0) 推荐(0)
Leetcode: Sudoku Solver
摘要: Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be o... 阅读全文
posted @ 2014-09-11 06:56 neverlandly 阅读(416) 评论(0) 推荐(0)
2014年9月8日
Leetcode: Binary Tree Maximum Path Sum
摘要: 难度:75. 参见了他人的思路,这道题是求树的路径和的题目,不过和平常不同的是这里的路径不仅可以从根到某一个结点,而且路径可以从左子树某一个结点,然后到达右子树的结点,就像题目中所说的可以起始和终结于任何结点。函数的返回值定义为以自己为根的一条从根到叶子结点的最长路径,这个返回值是为了提供给它的父结 阅读全文
posted @ 2014-09-08 13:12 neverlandly 阅读(400) 评论(0) 推荐(0)
Leetcode: Gas Station
摘要: 难度:60.这道题用Brute Force的方法解比较好想,就是从每一个站开始,一直走一圈,累加过程中的净余的油量,看它是不是有出现负的,如果有则失败,从下一个站开始重新再走一圈;如果没有负的出现,则这个站可以作为起始点,成功。可以看出每次需要扫描一圈,对每个站都要做一次扫描,所以时间复杂度是O(n 阅读全文
posted @ 2014-09-08 12:53 neverlandly 阅读(370) 评论(0) 推荐(0)
Leetcode: Convert Sorted List to Binary Search Tree
摘要: 目前做法: 第一遍做法:难度70,与Convert Sorted Array to Binary Search Tree问题相似,这道题的做法就是,我们需要中点来做每一层树的根,问题在于,对于一个链表我们是不能常量时间访问它的中间元素的。于是我的想法是花O(N)的时间把链表里面的元素先存到一个数组或 阅读全文
posted @ 2014-09-08 05:21 neverlandly 阅读(607) 评论(0) 推荐(0)
2014年9月7日
Leetcode: Permutations II
摘要: Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique per... 阅读全文
posted @ 2014-09-07 23:46 neverlandly 阅读(431) 评论(0) 推荐(0)
Leetcode: Reorder List && Summary: Reverse a LinkedList
摘要: 这是一道比较综合的链表操作的题目,要按照题目要求给链表重新连接成要求的结果。其实理清思路也比较简单,分三步完成:(1)将链表切成两半,也就是找到中点,然后截成两条链表;(2)将后面一条链表进行reverse操作,就是反转过来;(3)将两条链表按顺序依次merge起来。 这几个操作都是我们曾经接触过的 阅读全文
posted @ 2014-09-07 12:07 neverlandly 阅读(476) 评论(0) 推荐(0)
Leetcode: Unique Binary Search Trees II
摘要: 难度:98。没有做出来的一道题,很难,参考了其他人的想法,发现基本上只有一个版本,在纸上按照它的步骤推了几步终于看懂了。分析如下: 第一次遇到这种让你construct a BST的问题,而且结果并不是建立一个ArrayList<ArrayList<Integer>>型的,而是建立一个ArrayLi 阅读全文
posted @ 2014-09-07 07:26 neverlandly 阅读(446) 评论(0) 推荐(0)
2014年9月6日
Leetcode: Subsets II
摘要: 第二遍Better做法: 第11行 i>start && nums[i]==nums[i-1] 就skip很不错 第一遍做法:需要已访问数组,当前后元素一样且前面元素并未访问,这个时候就是重复的case, continue, 注意这里的判重复条件跟3Sum那种是不一样的, 3SUM防止重复方法是 i 阅读全文
posted @ 2014-09-06 07:11 neverlandly 阅读(501) 评论(0) 推荐(0)
Leetcode: Unique Paths II
摘要: Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space i... 阅读全文
posted @ 2014-09-06 05:22 neverlandly 阅读(474) 评论(0) 推荐(0)
Leetcode: Minimum Path Sum
摘要: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. 阅读全文
posted @ 2014-09-06 04:24 neverlandly 阅读(475) 评论(0) 推荐(0)
2014年9月5日
Leetcode: Multiply Strings
摘要: 大整数的字符串乘法,我本来对字符串表示的大整数就不是太熟悉,至于溢出该怎么处理也是不大清楚,所以参考了别人的做法,然后自己凭着印象重做了一次 直接乘会溢出,所以每次都要两个single digit相乘,最大81,不会溢出。 比如385 * 97, 就是个位=5 * 7,十位=8 * 7 + 5 * 阅读全文
posted @ 2014-09-05 11:06 neverlandly 阅读(590) 评论(0) 推荐(0)
Leetcode: Distinct Subsequences
摘要: DP新思路,不同于以往的DP做法 阅读全文
posted @ 2014-09-05 05:18 neverlandly 阅读(357) 评论(0) 推荐(0)
2014年6月27日
Leetcode: Permutations
摘要: Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1]... 阅读全文
posted @ 2014-06-27 13:14 neverlandly 阅读(455) 评论(0) 推荐(0)
Leetcode: First Missing Positive
摘要: Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm sho... 阅读全文
posted @ 2014-06-27 07:55 neverlandly 阅读(277) 评论(0) 推荐(0)
Summary: Binary Search
摘要: Iterative ways: 1 int binarySearch (int[] a, int x) { 2 int low = 0; 3 int high = a.length - 1; 4 int mid; 5 6 while (low x) {12 ... 阅读全文
posted @ 2014-06-27 06:43 neverlandly 阅读(199) 评论(0) 推荐(0)
Leetcode: Find First and Last Position of Element in Sorted Array
摘要: Analysis: 这道题是二分查找Search Insert Position的变体,思路并不复杂,就是先用二分查找找到其中一个target,然后再往左右找到target的边缘。找边缘的方法跟二分查找仍然是一样的,只是相等的情况依然要往左找(找左边缘)或往右找(找右边缘)。这样下来总共进行了三次二 阅读全文
posted @ 2014-06-27 06:26 neverlandly 阅读(341) 评论(0) 推荐(0)
2014年6月26日
Leetcode: Unique Binary Search Trees
摘要: Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's... 阅读全文
posted @ 2014-06-26 12:26 neverlandly 阅读(234) 评论(0) 推荐(0)
Leetcode: Group Anagrams
摘要: if want lexicographic order,然后要用Collections.sort() 来保证字典序 for (List<String> each : map.values()) { Collections.sort(each); res.add(new ArrayList<Strin 阅读全文
posted @ 2014-06-26 08:10 neverlandly 阅读(393) 评论(0) 推荐(0)
2014年6月25日
Leetcode: 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->NULL, m = 2 and n = 4,return 1->4->3->2->5... 阅读全文
posted @ 2014-06-25 12:35 neverlandly 阅读(261) 评论(0) 推荐(0)
Leetcode: Longest Valid Parentheses
摘要: Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the lon... 阅读全文
posted @ 2014-06-25 06:18 neverlandly 阅读(340) 评论(0) 推荐(0)
2014年6月24日
Leetcode: Partition List
摘要: Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the o... 阅读全文
posted @ 2014-06-24 23:11 neverlandly 阅读(500) 评论(0) 推荐(0)
2014年6月23日
Leetcode: 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->NULL and k = 2,return 4->5->1->2->3->... 阅读全文
posted @ 2014-06-23 23:28 neverlandly 阅读(255) 评论(0) 推荐(0)
2014年6月21日
Leetcode: Divide Two Integers
摘要: Best method(跟discuss vote最高相似):只有一种情况整数除以整数会overflow,那就是Integer.MIN_VALUE除以-1,这种情况特殊分析。 之所以要用long a, b代替dividend和divisor是因为:比如Integer.MAX_VALUE除以1,第21 阅读全文
posted @ 2014-06-21 06:31 neverlandly 阅读(369) 评论(0) 推荐(0)
2014年6月20日
Leetcode: Linked List Cycle II
摘要: Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Follow up:Can you solve it without using extra space?一次... 阅读全文
posted @ 2014-06-20 08:04 neverlandly 阅读(240) 评论(0) 推荐(0)
Leetcode: Single Number II
摘要: Given an array of integers, every element appears three times except for one. Find that single one.Note:Your algorithm should have a linear runtime co... 阅读全文
posted @ 2014-06-20 07:49 neverlandly 阅读(269) 评论(0) 推荐(0)
Leetcode: Linked List Cycle
摘要: Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?Analysis: typical Runner Technique. 一次过 1 ... 阅读全文
posted @ 2014-06-20 07:14 neverlandly 阅读(243) 评论(0) 推荐(0)
2014年6月19日
Leetcode: Binary Tree Preorder Traversal
摘要: Analysis: 第一反应肯定是recursion(递归), 非常直接,但是题目要求不能用递归。如果要使用迭代的方法来写preorder traversal,最大的问题是要如何确定遍历节点的顺序,因为树的pre-order traversal其实很类似图的DFS,DFS可以用Stack来写,所以这 阅读全文
posted @ 2014-06-19 12:54 neverlandly 阅读(440) 评论(0) 推荐(0)
Leetcode: Single Number
摘要: Analysis: 需求里面要求O(N)时间以及无额外空间,这就排除了使用boolean array, hashmap这些个方法,只能在原数组上进行查找。O(N)基本上就相当于遍历数组. 最好的方法: 第二遍做法: 17行 & 运算符优先等级低于 == 所以一定要打括号 一样的思路另一个做法: 另外 阅读全文
posted @ 2014-06-19 06:37 neverlandly 阅读(316) 评论(0) 推荐(0)
Leetcode: Combination Sum
摘要: 典型的recursion方法,找符合要求的path,存入result的ArrayList中。所以方法还是建立一个ArrayList<ArrayList<Integer>> result, 建立一个ArrayList<Integer> path,用recursion当找到符合条件的path时,存入re 阅读全文
posted @ 2014-06-19 05:57 neverlandly 阅读(378) 评论(0) 推荐(0)
2014年6月18日
Leetcode: Rotate Image
摘要: 抠细节的题,题目思想如下:to implement the rotation in layers, if size is n, so there will be (int)(n / 2) layers that should be rotated. 需要注意的是:在下面这段code里面,需要注意的是 阅读全文
posted @ 2014-06-18 12:05 neverlandly 阅读(439) 评论(0) 推荐(0)
2014年6月17日
Leetcode: Longest Palindromic Substring && Summary: Palindrome
摘要: Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. Example 1: Example 2: Analysis: 想到 阅读全文
posted @ 2014-06-17 12:07 neverlandly 阅读(592) 评论(0) 推荐(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 le... 阅读全文
posted @ 2014-06-17 04:58 neverlandly 阅读(500) 评论(0) 推荐(0)
2014年6月14日
Leetcode: Substring with Concatenation of All Words
摘要: You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatena... 阅读全文
posted @ 2014-06-14 00:40 neverlandly 阅读(340) 评论(0) 推荐(0)
2014年6月12日
Leetcode: Merge k Sorted List
摘要: 参看别人的思路,类似MergeSort的思路,思路是先分成两个子任务,然后递归求子任务,最后回溯回来。这个题目也是这样,先把k个list分成两半,然后继续划分,直到剩下两个list就合并起来,合并时会用到Merge Two Sorted Lists这道题。 MergeSort的方法:我们来分析一下上 阅读全文
posted @ 2014-06-12 12:53 neverlandly 阅读(445) 评论(0) 推荐(0)
Summary: Java中函数参数的传递
摘要: 函数调用参数传递类型(java)的用法介绍.java方法中传值和传引用的问题是个基本问题,但是也有很多人一时弄不清。(一)基本数据类型:传值,方法不会改变实参的值。 1 public class TestFun { 2 3 public static void testInt(int ... 阅读全文
posted @ 2014-06-12 05:54 neverlandly 阅读(3045) 评论(0) 推荐(0)
Leetcode: Generate Parentheses
摘要: 又是把所有满足条件的结果存到一个ArrayList里面, 之前也有类似的如Letter combination of a Phone Number这道题。这种类型的题其实形成了一个套路,套路就是,recursion参数包括最终结果的集合(ArrayList),input(String),递归层次le 阅读全文
posted @ 2014-06-12 01:55 neverlandly 阅读(428) 评论(0) 推荐(0)
上一页 1 ··· 15 16 17 18 19 20 21 下一页
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3