• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅
上一页 1 ··· 16 17 18 19 20 21 下一页
2014年6月11日
Leetcode: Longest Substring Without Repeating Characters
摘要: Use HashSet Use HashMap 阅读全文
posted @ 2014-06-11 03:24 neverlandly 阅读(357) 评论(0) 推荐(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 ... 阅读全文
posted @ 2014-06-11 03:12 neverlandly 阅读(294) 评论(0) 推荐(0)
Leetcode: Letter Combinations of a Phone Number
摘要: 像这种DFS的题目,常见的写法无外乎两种,使用recursion, 或者用Stack。本文采用了Recursion的方式。做完后积累的经验有:像这种在一个ArrayList里面罗列可能的path的题目,recursion的参数一般包括:包含最终结果的集合(ArrayList),input(Strin 阅读全文
posted @ 2014-06-11 02:24 neverlandly 阅读(671) 评论(0) 推荐(0)
Summary: Depth-first Search(DFS)
摘要: There are generally two methods to write DFS algorithm, one is using recursion, another one is using stack. (reference from Wiki Pedia)Pseudocode for ... 阅读全文
posted @ 2014-06-11 02:09 neverlandly 阅读(627) 评论(0) 推荐(0)
2014年6月10日
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.一次过 1 public class Solution { 2 public S... 阅读全文
posted @ 2014-06-10 13:07 neverlandly 阅读(336) 评论(0) 推荐(0)
Leetcode: Container With Most Water
摘要: 这道题我考虑过类似Trapping most water的做法,不大一样。那道题寻找左右最大元素,我可以确定那就是最优解,但是在这里,即使寻到了左右最大元素,我也不敢确定那是最优解。 然后我有考虑了DP全局最优&局部最优的做法,发现局部最优的递推式很难写。所以没有法了,网上看到说这道题用夹逼方法 T 阅读全文
posted @ 2014-06-10 13:06 neverlandly 阅读(293) 评论(0) 推荐(0)
2014年6月6日
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 ... 阅读全文
posted @ 2014-06-06 23:56 neverlandly 阅读(249) 评论(0) 推荐(0)
2014年6月5日
Leetcode: Word Ladder II
摘要: Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:Only one letter can be ch... 阅读全文
posted @ 2014-06-05 21:07 neverlandly 阅读(392) 评论(0) 推荐(0)
2014年5月29日
Leetcode: Triangle
摘要: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the fol... 阅读全文
posted @ 2014-05-29 23:57 neverlandly 阅读(273) 评论(0) 推荐(0)
Leetcode: Best Time to Buy and Sell Stock II
摘要: Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complet... 阅读全文
posted @ 2014-05-29 04:03 neverlandly 阅读(269) 评论(0) 推荐(0)
Leetcode: Best Time to Buy and Sell Stock
摘要: 这道题求进行一次交易能得到的最大利润。如果用brute force的解法就是对每组交易都看一下利润,取其中最大的,总用有n*(n-1)/2个可能交易,所以复杂度是O(n^2)。跟Maximum Subarray非常类似,用“局部最优和全局最优解法”。思路是维护两个变量,一个是到目前为止最好的交易,另 阅读全文
posted @ 2014-05-29 03:37 neverlandly 阅读(384) 评论(0) 推荐(0)
2014年5月28日
Leetcode: Pascal's Triangle II
摘要: Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use ... 阅读全文
posted @ 2014-05-28 05:44 neverlandly 阅读(413) 评论(0) 推荐(0)
Leetcode: Pascal's Triangle
摘要: Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6... 阅读全文
posted @ 2014-05-28 04:29 neverlandly 阅读(233) 评论(0) 推荐(0)
2014年5月23日
Leetcode: Path Sum II
摘要: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and su 阅读全文
posted @ 2014-05-23 06:14 neverlandly 阅读(460) 评论(0) 推荐(0)
Leetcode: Convert Sorted Array to Binary Search Tree
摘要: Given an array where elements are sorted in ascending order, convert it to a height balanced BST.基本上一次过,要注意边界条件的问题:如果在recursion里有两个参数int begin, end, 递... 阅读全文
posted @ 2014-05-23 03:39 neverlandly 阅读(298) 评论(0) 推荐(0)
Leetcode: Merge Sorted Array
摘要: Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space (size that is greater or equal... 阅读全文
posted @ 2014-05-23 02:56 neverlandly 阅读(262) 评论(0) 推荐(0)
2014年5月22日
Leetcode: Word Search
摘要: Remember to change the character back after loop through all the possibility 非常聪明的方法1:O(1)空间的话可以不用visited数组,直接改board。改了之后,再访问的时候因为board[i][j] != word. 阅读全文
posted @ 2014-05-22 23:45 neverlandly 阅读(657) 评论(0) 推荐(0)
2014年5月21日
Leetcode: Remove Duplicates from Sorted Array II
摘要: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function shou 阅读全文
posted @ 2014-05-21 12:33 neverlandly 阅读(245) 评论(0) 推荐(0)
Leetcode: Subsets
摘要: Adopted approach: Notice that in line 11, you should create a copy of current path, and add it to res. Otherwise, it'll be edited later, and be wiped 阅读全文
posted @ 2014-05-21 12:25 neverlandly 阅读(572) 评论(1) 推荐(0)
Leetcode: Combinations
摘要: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4... 阅读全文
posted @ 2014-05-21 05:54 neverlandly 阅读(326) 评论(0) 推荐(0)
2014年5月20日
Leetcode: Climbing Stairs
摘要: 这道题目是求跑楼梯的可行解法数量。每一步可以爬一格或者两个楼梯,可以发现,递推式是f(n)=f(n-1)+f(n-2),也就是等于前一格的可行数量加上前两格的可行数量。熟悉的朋友可能发现了,这个递归式正是斐波那契数列的定义. 这里base case 是f(1)=1, f(2)=2. Fibonacc 阅读全文
posted @ 2014-05-20 07:10 neverlandly 阅读(377) 评论(0) 推荐(0)
Leetcode: Set Matrix Zeroes
摘要: 一次过,空间复杂度为O(m+n), 下一次做的时候寻找constant space solution。用boolean array也可以,用bit vector可能会更节省. 其实还可以再优化,我们考虑使用第一行和第一列来记录上面所说的行和列的置0情况,这里问题是那么第一行和第一列自己怎么办?想要记 阅读全文
posted @ 2014-05-20 07:08 neverlandly 阅读(305) 评论(0) 推荐(0)
Leetcode: Search a 2D Matrix
摘要: 1 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: 2 3 Integers in each row are ... 阅读全文
posted @ 2014-05-20 06:56 neverlandly 阅读(358) 评论(0) 推荐(0)
2014年5月14日
Leetcode: Add Binary
摘要: Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".我自己的做法:用StringBuffer, a和b都从末尾扫描到开头,而StringBu... 阅读全文
posted @ 2014-05-14 11:26 neverlandly 阅读(235) 评论(0) 推荐(0)
Leetcode: Unique Paths
摘要: 推荐方法:DP矩阵做法其实还可以节省一维:2D矩阵变成一维数组DP: 一维DP方法2: 阅读全文
posted @ 2014-05-14 06:01 neverlandly 阅读(372) 评论(0) 推荐(0)
Leetcode: Trapping Rain Water
摘要: 这道题做的比较艰辛,一开始自己想的是一个用stack的解法,感觉过于繁琐(出栈,入栈,计算容积),但未尝不是一个好的尝试,这个方法还是有点小问题,过后会好好想清楚。看了网上提示完成了最终的方法,这个方法两次遍历数组,第一次遍历找每个元素右边最大的元素,第二次遍历寻找每个元素左边最大的元素,同时计算该 阅读全文
posted @ 2014-05-14 04:37 neverlandly 阅读(599) 评论(0) 推荐(0)
2014年5月13日
Leetcode: Count and Say
摘要: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1 is read off as "one 1" or 11.11 is read off ... 阅读全文
posted @ 2014-05-13 12:30 neverlandly 阅读(286) 评论(0) 推荐(0)
2014年5月10日
Leetcode: Valid Sudoku
摘要: Best Approach: '4' in row 7 is encoded as "(4)7". '4' in column 7 is encoded as "7(4)". '4' in the top-right block is encoded as "0(4)2". 然后在解Sudoku S 阅读全文
posted @ 2014-05-10 06:45 neverlandly 阅读(409) 评论(0) 推荐(0)
Leetcode: Search Insert Position
摘要: Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in or... 阅读全文
posted @ 2014-05-10 02:33 neverlandly 阅读(243) 评论(0) 推荐(0)
Leetcode: Swap Nodes in Pairs
摘要: 1 Given a linked list, swap every two adjacent nodes and return its head.2 3 For example,4 Given 1->2->3->4, you should return the list as 2->1->4->3.... 阅读全文
posted @ 2014-05-10 01:32 neverlandly 阅读(257) 评论(0) 推荐(0)
2014年5月9日
Leetcode: Remove Nth Node From End of List
摘要: 这道题要注意的Corner Case是:如果n比这个LinkedList的size大,那么就需要直接返回Head,如果相等,就把head删掉,返回head.next;基本做的方法呢,还是Runner Technique. 由于有可能head会被删掉,所以最好还是使用一个dummy node,dumm 阅读全文
posted @ 2014-05-09 04:55 neverlandly 阅读(375) 评论(0) 推荐(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 阅读全文
posted @ 2014-05-09 04:20 neverlandly 阅读(331) 评论(0) 推荐(0)
Leetcode: Merge Two Sorted Lists
摘要: 方法二(推荐方法1): 新的Linkedlist不以任何现有List为依托,维护一个dummmy node和当前节点ListNode cur,把两个list的元素往里面插入作为cur.next,每次不new一个新的ListNode, 而是用已有的。相较于法一最后需要讨论两个list各自没有走完的情况 阅读全文
posted @ 2014-05-09 03:38 neverlandly 阅读(819) 评论(0) 推荐(0)
2014年5月8日
Leetcode: Longest Common Prefix
摘要: 这道题做的不够顺利,许多次通过,但是居然是卡在一个小问题上了,判断strs是否为空,我想当然地就写成了if(strs == null) return null; 报错 java中null表示还没new出对象,就是还没开辟空间;“”表示new出了对象,但是这个对象装的是空字符串。这里显然是要应对str 阅读全文
posted @ 2014-05-08 11:56 neverlandly 阅读(314) 评论(0) 推荐(0)
Leetcode: Roman to Integer
摘要: 从右向左,preVal, curVal 1. curVal >= preVal: res+=curVal 2. curVal < preVal: res -= curVal 阅读全文
posted @ 2014-05-08 05:23 neverlandly 阅读(305) 评论(0) 推荐(0)
Leetcode: String to Integer
摘要: 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 a... 阅读全文
posted @ 2014-05-08 03:27 neverlandly 阅读(387) 评论(0) 推荐(0)
2014年5月6日
Leetcode: Palindrome Numbers
摘要: Determine whether an integer is a palindrome. Do this without extra space.尝试用两头分别比较的方法,结果发现无法解决1000021这种问题 1 public class Solution { 2 public bool... 阅读全文
posted @ 2014-05-06 04:06 neverlandly 阅读(323) 评论(0) 推荐(0)
Leetcode: Reverse Integer
摘要: Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321一次通过,它的spoiler里面的提示有两个:1. 末尾为0的情况,这个考虑到了2.Did you notice that... 阅读全文
posted @ 2014-05-06 01:06 neverlandly 阅读(508) 评论(0) 推荐(0)
Leetcode: Two Sum
摘要: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have ex 阅读全文
posted @ 2014-05-06 00:20 neverlandly 阅读(407) 评论(0) 推荐(0)
2014年5月5日
Leetcode: Path Sum
摘要: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.Fo... 阅读全文
posted @ 2014-05-05 12:02 neverlandly 阅读(286) 评论(0) 推荐(0)
上一页 1 ··· 16 17 18 19 20 21 下一页
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3