上一页 1 ··· 20 21 22 23 24 25 26 27 28 ··· 43 下一页
摘要: Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring.For"(()", the longest valid parentheses substring is"()", which has length = 2.Another example is")()())", where the longest 阅读全文
posted @ 2013-08-01 13:26 feiling 阅读(305) 评论(0) 推荐(0) 编辑
摘要: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).The replacement must be in-place, do not allocate extra memory.He 阅读全文
posted @ 2013-07-31 20:40 feiling 阅读(493) 评论(0) 推荐(0) 编辑
摘要: 思路:从左往右扫描主字符串,用两个map来统计字符串的出现次数这里L数组中可能会出现两个相同的字符串 1 import java.util.Collection; 2 public class Solution { 3 public ArrayList findSubstring(String S, String[] L) { 4 ArrayList results = new ArrayList(); 5 int len = L.length; 6 if (len == 0) { 7 results.add(0... 阅读全文
posted @ 2013-07-31 20:17 feiling 阅读(377) 评论(0) 推荐(0) 编辑
摘要: Divide two integers without using multiplication, division and mod operator.1.naive的做法是用除数一个一个减被除数,直到被除数 0 && divis 0 && i >= 0) {25 if(divid >= a[i]){26 divid = divid - a[i];27 res = res + (1 << i);28 }29 i--;30 }31 ... 阅读全文
posted @ 2013-07-30 21:22 feiling 阅读(272) 评论(0) 推荐(0) 编辑
摘要: Implement strStr().Returns a pointer to the first occurrence of needle in haystack, ornullif needle is not part of haystack.此题应用KMP算法来解,但忘记了,写了个普通解line 10: 当needle == "" 时, 返回haystackline 11: 循环终止条件需注意 1 public class Solution { 2 public static String strStr(String haystack, String needle) 阅读全文
posted @ 2013-07-26 07:45 feiling 阅读(307) 评论(0) 推荐(0) 编辑
摘要: Implement strStr().Returns a pointer to the first occurrence of needle in haystack, ornullif needle is not part of haystack.此题应用KMP算法来解,但忘记了,写了个普通解line 10: 当needle == "" 时, 返回haystackline 11: 循环终止条件需注意 1 public class Solution { 2 public static String strStr(String haystack, String needle) 阅读全文
posted @ 2013-07-24 10:17 feiling 阅读(198) 评论(0) 推荐(0) 编辑
摘要: 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 length.思路:两层循环,注意删除元素时指针要回溯,此方法的时间复杂度为O(n^2) 1 public class Solution { 2 public int removeElement(int[] A, int e 阅读全文
posted @ 2013-07-24 08:50 feiling 阅读(178) 评论(0) 推荐(0) 编辑
摘要: k个元素一组指针反转函数调用问题!!!! 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next = null; 9 * }10 * }11 */12 public class Solution {13 public ListNode reverseKGroup(L... 阅读全文
posted @ 2013-07-19 20:13 feiling 阅读(270) 评论(0) 推荐(0) 编辑
摘要: 双指针互换,要考虑一些边界条件,比如链表为空,链表长度为1,链表长度为2.加一个safeGuard可以避开链表长度为2的检测。 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) { 7 * val = x; 8 * next = null; 9 * }10 * }11 */12 public class Solut... 阅读全文
posted @ 2013-07-18 22:00 feiling 阅读(289) 评论(0) 推荐(0) 编辑
摘要: Mergeksorted linked lists and return it as one sorted list. Analyze and describe its complexity.[解题思路]以前的解法的时间复杂度过高,通过在网上搜索,得到优化的时间复杂度:O(n*lgk)维护一个大小为k的最小堆,每次得到一个最小值,重复n次 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * Lis... 阅读全文
posted @ 2013-07-17 22:52 feiling 阅读(510) 评论(0) 推荐(0) 编辑
上一页 1 ··· 20 21 22 23 24 25 26 27 28 ··· 43 下一页