07 2013 档案
摘要: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
阅读全文
摘要:思路:从左往右扫描主字符串,用两个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...
阅读全文
摘要: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 ...
阅读全文
摘要: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)
阅读全文
摘要: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)
阅读全文
摘要: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
阅读全文
摘要: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...
阅读全文
摘要:双指针互换,要考虑一些边界条件,比如链表为空,链表长度为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...
阅读全文
摘要: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...
阅读全文
摘要:Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"典型的递归。一步步构造字符串。当左括号出现次数 generateParenthesis(int n) { 3 // S
阅读全文
摘要:Given a string containing just the characters'(',')','{','}','['and']', determine if the input string is valid.The brackets must close in the correct order,"()"and"()[]{}"are all valid but"(]"and"([)]"are not.思路:
阅读全文
摘要:Given a linked list, remove thenthnode from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.思路:两个指针,未能一次通过。。。。当需删除的元素是第一个元素时,直接按24-27进行删除 1 /** 2 * Defin
阅读全文
摘要:Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string "23"Output: ["ad", "ae", "af", "bd", "be", &q
阅读全文
摘要:Given an arraySofnintegers, are there elementsa,b,c, anddinSsuch thata+b+c+d= target? Find all unique quadruplets in the array which gives the sum of target.Note:Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie,a?b?c?d)The solution set must not contain duplicate quadruplets. .
阅读全文
摘要:Given an arraySofnintegers, find three integers inSsuch that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is closest to ...
阅读全文
摘要:Given an arraySofnintegers, are there elementsa,b,cinSsuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c) must be in non-descending order. (ie,a?b?c)The solution set must not contain duplicate triplets. For example, given array S...
阅读全文