leetcode hard level(part)

这部分hard level没有时间仔细思考,汇总一下感受。这部分hard level加上之前的题目详解,基本上覆盖hard题目。记录一下,第二遍复习使用。

301. Remove Invalid Parentheses
精妙解法:https://leetcode.com/discuss/81478/easy-short-concise-and-fast-java-dfs-3-ms-solution


297. Serialize and Deserialize Binary Tree
java 引用传递


295. Find Median from Data Stream
二分

Find the Duplicate Number
转化成链路有环入口检测;与数组中寻找单一出现次数元素的题目相比较


273. Integer to English Words
题目翻译英语竟然没有and,枉费我想的那么全面。


239. Sliding Window Maximum 
优先权队列


233. Number of Digit One
仔细分情况讨论,数学计算数量


224. Basic Calculator
稍微麻烦一点


218. The Skyline Problem
二分查找位置,逐个模块更新外观。最佳beat100%

*************************************************
重写了KMP,竟然还没有使用Java的内置函数加暴力快。

28. Implement strStr() 
public static int strStr(String haystack, String needle) {
        char[] s = haystack.toCharArray();
        char[] t = needle.toCharArray();
        int[] next = next(t);  
        int i = 0,j = 0;  
        while (i <= s.length - 1 && j <= t.length - 1) {  
            if (j == -1 || s[i] == t[j]) {  
                i++;  
                j++;  
            } else {  
                j = next[j];  
            }  
        }  
        if (j < t.length) {  
            return -1;  
        } else  
            return i - t.length;
    }
    public static int[] next(char[] t) {  
        int[] next = new int[t.length];
        if(t.length > 0)
            next[0] = -1;  
        int i = 0;  
        int j = -1;  
        while (i < t.length - 1) {  
            if (j == -1 || t[i] == t[j]) {  
                i++;  
                j++;  
                if (t[i] != t[j]) {  
                    next[i] = j;  
                } else {  
                    next[i] = next[j];  
                }  
            } else {  
                j = next[j];  
            }  
        }  
        return next;  
    }
****************************************************


214 Shortest Palindrome
最长回文子串 manacher 算法
此题最佳是KMP变形算法
KMP有两种写法,一种是上面的优美写法,另外一种是记录最长前缀后缀相等的长度。
两个思想都非常精妙。。。
*********************************************************

void makeNext(const char P[],int next[])
{
    int q,k;//q:模版字符串下标;k:最大前后缀长度
    int m = strlen(P);//模版字符串长度
    next[0] = 0;//模版字符串的第一个字符的最大前后缀长度为0
    for (q = 1,k = 0; q < m; ++q)//for循环,从第二个字符开始,依次计算每一个字符对应的next值
    {
        while(k > 0 && P[q] != P[k])//递归的求出P[0]···P[q]的最大的相同的前后缀长度k
            k = next[k-1];          //不理解没关系看下面的分析,这个while循环是整段代码的精髓所在,确实不好理解  
        if (P[q] == P[k])//如果相等,那么最大相同前后缀长度加1
        {
            k++;
        }
        next[q] = k;
    }
}
**********************************************************************


212. Word Search II 
使用tri树过的。。。顺便a了tri树的midium
最佳代码:

public List<String> findWords(char[][] board, String[] words) {
    List<String> res = new ArrayList<>();
    TrieNode root = buildTrie(words);
    for(int i = 0; i < board.length; i++) {
        for(int j = 0; j < board[0].length; j++) {
            dfs(board, i, j, root, res);
        }
    }
    return res;
}


public void dfs(char[][] board, int i, int j, TrieNode p, List<String> res) {
    char c = board[i][j];
    if(c == '#' || p.next[c - 'a'] == null) return;
    p = p.next[c - 'a'];
    if(p.word != null) {   // found one
        res.add(p.word);
        p.word = null;     // de-duplicate
    }


    board[i][j] = '#';
    if(i > 0) dfs(board, i - 1, j ,p, res); 
    if(j > 0) dfs(board, i, j - 1, p, res);
    if(i < board.length - 1) dfs(board, i + 1, j, p, res); 
    if(j < board[0].length - 1) dfs(board, i, j + 1, p, res); 
    board[i][j] = c;
}


public TrieNode buildTrie(String[] words) {
    TrieNode root = new TrieNode();
    for(String w : words) {
        TrieNode p = root;
        for(char c : w.toCharArray()) {
            int i = c - 'a';
            if(p.next[i] == null) p.next[i] = new TrieNode();
            p = p.next[i];
       }
       p.word = w;
    }
    return root;
}


class TrieNode {
    TrieNode[] next = new TrieNode[26];
    String word;
}
明白了如何写ArrayList 的遍历和如何将HashSet转化成ArrayList,还有递归不需要使用Used数组,最重要的是递归以后一定注意状态回滚要恢复。


188. Best Time to Buy and Sell Stock IV
dp难度很大的题目,真心不会做,虽然代码量很小


174. Dungeon Game
dp中等,也没做出来,思路走向了:起点到终点,累积和最小值最大的路径。


164. Maximum Gap 
设计平均gap,分桶排序
基数排序也可以.两种方法已过。


154. Find Minimum in Rotated Sorted Array II 
二分分情况讨论


149. Max Points on a Line
暴力+减枝

http://blog.csdn.net/ljiabin/article/details/38904757/
的解法也不错


146. LRU Cache
双向链表(实现任意位置删除元素;在尾部加元素)+hashMap(查找位置)


145. Binary Tree Postorder Traversal
可以破坏原树,就不需要记录栈中的节点是否已经查询完毕。


140. Word Break II 
没怎么理解,递归做法和迭代DP不同。递归:使用HashMap存储dfs查找


138. Copy List with Random Pointer   
使用hashMap存储复制的对应关系


135. Candy
相邻大,分的糖果多。遍历两遍即可。我的做法稍微复杂:找到最低端的,向两边扩展


132. Palindrome Partitioning II
两层dp 非常不错的题目:最小分割数量:后向dp,求解子串回文:回文dp。两个dp汇成一个两层循环。


128。Longest Consecutive Sequence
hashMap计算左边的长度(包括他自己)和右边的长度(包括他自己)每次加入一个元素Num,先更新自己的left和right,然后找到包含这个元素的最左边的更新right,找到最右边更新left。


126 word ladder2
第一反应是使用dijie求解最短路顺便记录路径---超时:估计是在构建边,和查找最小值时超时
做法: bfs :字符串每一位只有26个变化,这种方式仍然会超时。使用双向BFS查找并记录所有连接的可能性,然后dfs输出所有的路径
没做出来


124 Binary Tree Maximum Path Sum
根据树,从上到下递归求解。注意:左子树、右子树小于0,取0


123 Best Time to Buy and Sell Stock III
一种方法: 使用复杂dp,包含local和global双层dp,只不过将k设置成2
第二种方法: 由k等于1时的O(n)方法改进, 只需要将数组分割成两半进行计算即可


117. Populating Next Right Pointers in Each Node II
使用链表的特性:preList和nowList的关系


115. Distinct Subsequences
dp写法精炼优美,但是不太好想,有dp想法没有想明白


99. Recover Binary Search Tree
使用first second 和pre就可以找出错误的节点,交换即可。
也可以使用Morri算法,就是线索二叉树,算法十分固定且精妙


97. Interleaving String
dp分析,一次AC


87. Scramble String
递归判断,很有意思的题目


85. Maximal Rectangle

84 Largest Rectangle in Histogram\
这两个题目是一种解法:计算递增序列的最大面积


76 Minimum Window Substring
查询到一个匹配,删除多余字母


72 Edit Distance
dp[i][j]= if s[i-1]==t[j-1]: dp[i-1][j-1]
          else: max{dp[i-1][j],dp[i][j-1],dp[i-1][j-1]}+1


68. Text Justification 
模拟题


65. Valid Number 
考察思考的情况:
12 1.2  12e12 1.2e12  +1.2e-12


57. Insert Interval 
二分+合并


56. Merge Intervals 
排序+合并


52. N-Queens II 
HashSet 搜索


45. Jump Game II 
简单dp会超时,移动窗口O(N)


41. First Missing Positive
非常有意思的算法,计算missing value的左右界限,直到界限为空


30. Substring with Concatenation of All Words
还是窗口移动算法,窗口移动算法很容易就是O(N)


25. Reverse Nodes in k-Group
思考全面,链表窗口


23. Merge k Sorted Lists
合理使用priority'


10. Regular Expression Matching
dp中等难度,需要思考好各种情况和边界

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值