LeetBoss

这篇博客里记录俩类题目 

1. AC 15%一下

2. 经典DP

BOSS 1 : Word Ladder

Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformation sequence from beginWord to endWord, such that:

  1. Only one letter can be changed at a time
  2. Each intermediate word must exist in the dictionary

For example,

Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]

As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.


先看一段吉祥三宝的分析,个人认为是这个题目最好大理解方式。

"

Analysis:

This problem can be abstracted as a tree level-order traversal problem. In binary tree level-order traversal, we find the next level node via the left and right pointer, but in this problem, we search the string in dictionary, if the their distance is 1, we add it to queue for next level traversal, and delete it from queue.

To record the distance, we also maintain another queue, in which stores the current string’s distance to the root string.

When we delete a string from dict, it is possible that the deleted string will be used in another path. To illustrate this problem, see:

(1) When dogs are in the same level:

        hot
        / 
      hog  dot
       |    |
      dog  dog
       |    |
      dig  dig

Where start is “hot”, and end is “dig”, when we visit hot->hog->dog, dog is deleted, but dog is also needed in hot->dot->dog. When dog are in the same level, it is fine since the distance is the same.

(2) When dog is reused in deeper level, see:

        hot
        / 
      hog  dot
       |    |
      dog  dot
       |    |
      dig  dag
            |
           dog
            |
           dig

It is still fine. Because the minimum distance is already found when dog needs to be reused.

The search of possible 1-distance strings takes O(N*L) time, where N is the number of alphabetical letters, and L is the length of a string in dictionary

"分析来自

---吉祥三宝的博客


代码相对清晰,简洁。注意return 的时候不需要加一,加一是在把该item存入queue的时候做的,所以在item入queue时候,对应的存距离的queue2里存的就是它最终的距离。

然后这里记录的是每个item 的距离,不需要level这个概念了。也就不需要一层一层处理,直接一个一个来。

    public int ladderLength(String start, String end, Set
  
    dict) {
        //Just another version of lvl order traversal of BFS
        LinkedList
   
     q1 = new LinkedList
    
     ();
        LinkedList
     
       q2 = new LinkedList
      
       ();
        dict.add(end);
        q1.add(start);
        q2.add(1);//q2 record the distance(no. of items) from start to the corresponding str in q1
        
        while(!q1.isEmpty()){
            String item = q1.poll();
            int minDist = q2.poll();
            
            if(item.equals(end))
                return minDist;
            //add all possible edges of current item to expand nxt lvl
            //after the last item of current lvl is processed, we have a complete list of items of nxt lvl in our queue then
            //we don't need to differentiate lvl here, just go till the end, because we keep distance for each item but for lvl 
            for(int i=0; i
       
      
     
    
   
  

BOSS2 : Word Break

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words.

For example, given
s = "leetcode",
dict = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

这个题做了多少遍了,每一遍对这个DP都有新的认识。。。

这个题我在我DP之章里分析写的很细了,这里不重复里。

这个题recursion和dp俩个方法很绕,正好反着,但是如果真正理解了,也不会混淆。

这个题recursion就简单很多,包括Word Break2, 都是很经典的Back Tracking的题目。

 public boolean wordBreak(String s, Set
  
    wordDict) {
        boolean[][] dp = new boolean[s.length()][s.length()];

        for(int i=s.length()-1;i>=0;i--){
            for(int j=i; j
   
  

同时我把  palindrome partition || 的DP做法也放在这里,这俩道题神似。




identity 身份认证 购VIP最低享 7 折! triangle vip 30元优惠券将在58:6:9后过期 去使用 triangle QT+Poppler+PDFviewer.zip 是一个用于在Windows操作系统下,使用QT5框架结合Poppler库开发PDF阅读器的项目。这个项目的核心是利用Poppler库解析PDF文档,并通过QT5进行用户界面的设计和交互。以下将详细介绍相关知识点: 1. **QT5框架**:QT(Qt)是一个跨平台的应用程序开发框架,支持多种操作系统,如Windows、Linux、macOS等。它提供了丰富的库函数和组件,使得开发者可以方便地构建图形用户界面(GUI)应用程序。QT5是QT的第五个主要版本,引入了许多新特性和改进,如QML(Qt Meta Object Language)用于声明式UI设计,以及更好的性能和API优化。 2. **Poppler库**:Poppler是一个开源的PDF文档处理库,源自Xpdf项目,主要用于PDF文件的解析、渲染和提取文本。Poppler提供了C++接口,使得开发者能够方便地在应用程序中集成PDF阅读和处理功能。它可以读取PDF文件,显示页面,提取文本和元数据,甚至支持对PDF文件进行注释和修改(但本项目可能仅涉及阅读功能)。 3. **PDF viewer的实现**:在本项目中,PDF viewer是基于QT5 GUI组件构建的,它利用Poppler库来加载和解析PDF文档。`mainwindow.cpp`和`mainwindow.h`包含了主窗口类的定义和实现,这是用户与应用程序交互的主要界面。`pdfcanvas.cpp`和`pdfcanvas.h`则可能包含了用于显示PDF页面的自定义画布类,该类使用Poppler库来渲染PDF页面到QT的画布上。 4. **项目构建与编译**:`newtime.pro`是QT项目的配置文件,用于指定项目依赖的库(如Poppler)、源代码文件、编译选项等。`.pro.user`文件则保存了用户的特定编译设置,如编译器路径或调试选项。开发者需要使用QT的qmake工具或直接在IDE如Qt Creator中打开此项目,进行编译和链接,确保所有依赖库都正确安装并链接。 5. **文件操作**:`main.cpp`通常是程序的入口点,负责初始化QT应用环境并运行主循环。在PDF viewer中,可能会在`main.cpp`中实例化主窗口,并调用Poppler库的相关函数来加载PDF文件。 6. **使用流程**:用户可以通过QT界面选择PDF文件,然后通过Poppler库读取文件内容,将页面渲染到QT的控件上。用户可以通过滚动、缩放等操作查看PDF内容。 Poppler库的强大功能使得PDF viewer可以支持多页显示、文本搜索、书签管理等高级特性。 7. **优化与扩展**:为了提升用户体验,开发者可能会对PDF viewer进行各种优化,比如添加平滑滚动、快速查找、页面预加载等功能。此外,还可以考虑支持批注、打印、PDF转换等更复杂的操作,以增强软件的功能性和实用性。 QT+Poppler+PDFviewer.zip项目提供了一个基础的PDF阅读器实现,开发者可以在此基础上进一步定制和扩展,以满足特定的PDF处理需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值