- 博客(17)
- 资源 (2)
- 收藏
- 关注
原创 LeetCode—306 Additive Number
public class Solution { public boolean isAdditiveNumber(String num) { int length=num.length(); int l=Math.min(length, length*2/3+3); for (int i = 0; i < l-2; i++) { Stri
2015-11-19 14:56:45 697
原创 LeetCode—147 Insertion Sort List
思路:插入排序。。。。顾名思义,插入排就好了。/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution {
2015-11-06 17:04:47 246
原创 LeetCode—109 Convert Sorted List to Binary Search Tree
思路:双指针扫描,找中点,然后中点作为父节点,前后两段链表递归查询,空间复杂度常数,时间复杂度线性/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * }
2015-11-06 16:05:01 295
原创 LeetCode—300 Longest Increasing Subsequence
思路:遍历数组,建立另外一个数组维护,维护规则,每次读到的数跟这个数组有效距离最后一个数比较,如果比最后一个数大(或者该数组为空)则插入这个数,并使得有效长度+1;如果小于最后一个数,则在这个数组中找到第一个比自己大的数替换掉,如果发现这个数组中有跟自己相同的数,那么就不操作这个数。因为是有序数组,所以插入的时间复杂度为O(lgn);public class Solution { int[]
2015-11-06 14:24:33 334
原创 LeetCode—260 Single Number III
思路:遍历数组,所有的数异或,得到的值,处理,取到该结果的二进制形式下某位为1对应的数。然后再次遍历结果,与这个数异或,异或结果按照是否为0分开异或,得到2个数值。public class Solution { public int[] singleNumber(int[] nums) { int x=0; for(int i=0;i<nums.leng
2015-11-05 21:45:31 335
原创 LeetCode—137 Single Number II
思路:循环读32次,每次读数组的时候跟1000000000000000000(31个0,Integer.MIN_VALUE)取与,不为0则记录下来,读一遍之后%3看是否为0,不为0则表示该位有值。public class Solution { public int singleNumber(int[] nums) { int min=Integer.MIN_VALUE;
2015-11-05 21:30:47 242
原创 LeetCode—173 Binary Search Tree Iterator
思路:建立一个list,从root开始读left进list,如果left==null则停止。hasNext只要判断list.size是否为0;next取出list最后一个节点,然后判断该节点是否有right,有的话写入list,继续判断是否有left,有的话写入。/** * Definition for binary tree * public class TreeNode { *
2015-11-05 18:30:14 283
原创 LeetCode—32 Longest Valid Parentheses
思路:建立一维数组vals[],vals[i]表示从i开始(包含该字符)到结尾,这个数组的合法括号长度。然后从倒数第二个字符开始读,读到‘(’检测其后面的vals[i+1]个位置是否为‘)’,如果是,则表明正好构成了一个新的合法括号,用该值加上2,再加上该值后面一个数,即截断后剩下的字符串能够成的最大合法长度。public class Solution { public int lon
2015-11-05 15:29:18 237
原创 LeetCode—204 Count Primes
思路:建立一个布尔数组,从2开始读取这个数组,读到false进去维护下,并给返回的值+1public class Solution { public int countPrimes(int n) { boolean[] flag=new boolean[n]; int ans=0; for (int i = 2; i < flag.length; i++)
2015-11-05 11:07:57 269
原创 LeetCode—212 Word Search II
思路:调用word Search 1方法,因为测试的词肯定比较多,所以先做一遍预处理。public class Solution { Map> map=new HashMap>(); Map finish=new HashMap(); public List findWords(char[][] board, String[] words) { List ans=new
2015-11-04 16:36:06 383
原创 LeetCode—187.Repeated DNA Sequences
思路:居然可以用这么无脑的方法。。。。public class Solution { public List findRepeatedDnaSequences(String s) { Map map=new HashMap(); Map trash=new HashMap(); List ans=new ArrayList();
2015-11-04 10:45:04 300
原创 LeetCode—72.Edit Distance
思路:动态规划。比较word1和word2各自位置上的某两个字符,如果这两个字符相等,那么答案等于各自去掉这个字符的答案,如果不等于,则等于增删改3种方法最小的那种+1。建立二维数组存储动态规划的结果。public class Solution { public int minDistance(String word1, String word2) { int l1=w
2015-11-03 16:55:17 294
原创 LeetCode—51.N-Queens
思路:用一个数组装纵坐标,节省一次判断,然后使用回溯算法。public class Solution { public List> solveNQueens(int n) { //装n皇后的数组 int[] nums=new int[n]; //答案 List> ans=new ArrayList>(); int i=0; w
2015-11-03 09:58:22 274
原创 LeetCode—37.Sudoku Solver
思路:回溯算法,第一次玩,写的比较丑。先找到需要填的格子,然后每次向前试探,如果碰到不需要填的跳过,试到自己为9如果依然错误则开始回退,会退到需要填写的且不为9的继续。public class Solution { public void solveSudoku(char[][] board) { //true表示需要填的 boolean[][] flag=n
2015-11-02 15:35:34 230
原创 LeetCode—10.Regular Expression Matching
思路:注意一点,*表示前一个字符重复任意次(可以为0次),所以反向匹配,先判断最后一个是不是*,如果是*,再判断前面不是.,然后递归判断。public class Solution { public boolean isMatch(String s, String p) { if (p.length()==0) { return s.length()==0;
2015-11-02 11:23:33 251
原创 LeetCode—221.Maximal Square
思路:核心思想:(x,y)点的最大边长f(x,y)=Min(f(x-1,y),f(x,y-1),f(x-1,y-1))+1。public class Solution { public int maximalSquare(char[][] matrix) { int cross=matrix.length; if (cross==0) { return 0;
2015-11-02 10:02:23 278
原创 Algorithms—299.Bulls and Cows
思路:题目没有难点,就是考虑到各种重复数据如何判定的情况,尝试了很多次。public class Solution { public String getHint(String secret, String guess) { int length=secret.length(); if (length==0) { return "0A0B"; }
2015-11-02 09:43:28 307
2积分系列——经典算法与人工智能在外卖物流调度中的应用
2018-04-26
Outlier Analysis 2nd Edition.pdf ——2积分系列
2018-03-28
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人