www.flydean.com
  • README
  • blog
    • 新版博客回归啦
    • projects
      • 一键自动化博客发布工具,用过的人都说好(简书篇)
      • 一键自动化博客发布工具,chrome和firfox详细配置
      • 一键自动化博客发布工具,用过的人都说好(segmentfault篇)
      • 一键自动化博客发布工具,用过的人都说好(oschina篇)
      • 一键自动化博客发布工具,用过的人都说好(阿里云篇)
      • 一键自动化博客发布工具,用过的人都说好(cnblogs篇)
      • 一键自动化博客发布工具,用过的人都说好(infoq篇)
      • 一键自动化博客发布工具,用过的人都说好(csdn篇)
      • 一键自动化博客发布工具,用过的人都说好(51cto篇)
      • 一键自动化博客发布工具,用过的人都说好(掘金篇)
      • 一键自动化博客发布工具,用过的人都说好(腾讯云篇)
      • 一键自动化博客发布工具,用过的人都说好(头条篇)
      • 一键自动化博客发布工具,用过的人都说好(知乎篇)
      • 一键自动化博客发布工具,用过的人都说好(公众号篇)
      • moneyPrinterPlus
        • MoneyPrinterPlus:AI自动短视频生成工具,赚钱从来没有这么容易过
        • MoneyPrinterPlus:AI自动短视频生成工具,详细使用教程
        • MoneyPrinterPlus:AI自动短视频生成工具-阿里云配置详解
        • MoneyPrinterPlus:AI自动短视频生成工具-腾讯云配置详解
        • MoneyPrinterPlus:AI自动短视频生成工具-微软云配置详解
        • 重磅!免费一键批量混剪工具它来了,一天上万短视频不是梦
        • 福利来了!MoneyPrinterPlus可以自动配置环境和自动运行了
        • 重磅来袭!MoneyPrinterPlus一键发布短视频到视频号,抖音,快手,小红书上线了
        • MoneyPrinterPlus全面支持本地Ollama大模型
        • 在MoneyPrinterPlus中使用本地chatTTS语音模型
        • fasterWhisper和MoneyPrinterPlus无缝集成
        • 再升级!MoneyPrinterPlus集成GPT_SoVITS
    • tools
      • 来了,永久免费的图床服务
      • 给picgo上传的图片加个水印
      • 手动给docusaurus添加一个搜索
  • docs
    • blockchain
      • 00-blockchain
      • 01-bitcoin
        • 01-bitcoin-overview
        • 02-bitcoin-blockchain-network
        • 03-bitcoin-consensus
        • 04-bitcoin-transactions
        • 05-bitcoin-mine-consensus
        • 06-bitcoin-in-trouble
      • 03-hyperledger
        • 01-Introduction-to-distributed-ledgers
        • 02-hyperledger-fabric-basics
        • 03-technical-advantages-fabric
        • 04-blockchain-vscode-extension
        • 05-use-vs-connect-ibc
        • 06-run-Fabric-on-ibm-Cloud
      • 04-libra
        • 01-libra-white-paper-interpretation
        • 2. Libra教程之:数据结构和存储
        • 3. Libra教程之:执行Transactions
        • 4. Libra教程之:move语言的特点和例子
        • 5. Libra教程之:Libra协议的关键概念
        • 6. Libra protocol的逻辑数据模型
        • 7. Transaction的生命周期
        • 8. 来了,你最爱的Move语言
        • 9. 运行自定义move modules
        • 10. Libra testnet使用指南
      • 02-ethereum
        • Solidity
          • 1. Solidity的Bytecode和Opcode简介
    • cryptology
      • 01-consistency-hash
      • 02-sybil-attack
      • 03-tor
      • 04-hmac
      • 05-erc20-short-address-attack
      • 06-mac-attack
      • 07-one-time-password
      • 8. DES
      • 9. AES
      • 10. 分组密码与模式
      • 11. 私钥公钥系统
      • 12-RSA算法
      • 13. 什么是中间人攻击
      • 14-混合密码系统
      • 15-单向散列函数
      • 16. 数字签名
      • 17. 一文读懂密码学中的证书
      • 18. 密钥详解
      • 19. 更加安全的密钥生成方法Diffie-Hellman
      • 20. 基于口令的密码(PBE)
      • 21. 一篇文章让你彻底弄懂SSL/TLS协议
      • 22-known-plaintext-attack
      • 23-Content-sniffing
      • 24-csrf
      • 25-SHA1-2-3
      • 26-IDEA
      • 27-memory-hard
      • 27-memory-hard_zhihu
      • 28-safer
      • 29-collision-attack
      • 30-birthday-attack
      • 30 Side Channel Attack
      • 31-feistel-cipher
      • 32-blowfish
      • 33-twofish
      • 34 Memory Bound
      • 35-MD-length-extension
      • 36 Sponge Function
      • 37 Bcrypt
      • 38-Argon2
      • 39-Pbkdf2
      • 40-scrypt
      • 41-CORS
      • 42-pki-x509
      • 43-pki-ocsp
      • 44-openssl-ocsp
      • 45-openssl-private-ca
      • 46-ASN.1
      • 47-x690-ber-cer-der
      • 48-PEM-PKCS7812
    • db
      • 01-IndexedDB-kickoff
    • java
      • java程序员从小工到专家成神之路(2024版)
      • 1-java-base
        • 前言
        • 01-string-all-in-one
        • 02-java-string-encodings
        • 03-base-shallow-copy-deep-copy
        • 04-do-you-know-class-name
        • 05-duration-period-ChronoUnit
        • 06-inner-class-inner-interface
        • 07-java-serialization
        • 8. 什么?注释里面的代码居然能够执行
        • 9. Java函数式编程和Lambda表达式
        • 10-lambda-closure
        • 11-type-inference-lambda
        • 12-marker-interface-annotation-processor
        • 13-java-jar-in-detail
        • 14-java-spi-for-extensible-app
        • 15-wordcount-in-one-line
        • 16-how-to-stop-thread
        • 17-why-use-peek
        • 18-checked-exception-in-lambda
      • 2-io-nio
        • 简介
        • 01-io-nio-overview
        • 02-io-file
        • 03-io-try-with
        • 4. 小师妹学JavaIO之:文件读取那些事
        • 5. 小师妹学JavaIO之:文件写入那些事
        • 6. 小师妹学JavaIO之:目录还是文件
        • 7. 小师妹学JavaIO之:文件系统和WatchService
        • 8. 小师妹学JavaIO之:文件File和路径Path
        • 9. 小师妹学JavaIO之:Buffer和Buff
        • 10. 小师妹学JavaIO之:File copy和File filter
        • 11. 小师妹学JavaIO之:NIO中Channel的妙用
        • 12. 小师妹学JavaIO之:MappedByteBuffer多大的文件我都装得下
        • 13. 小师妹学JavaIO之:NIO中那些奇怪的Buffer
        • 14. 小师妹学JavaIO之:用Selector来说再见
        • 15. 小师妹学JavaIO之:文件编码和字符集Unicode
      • 3-concurrent
        • 简介
        • 1. java.util.concurrent简介
        • 2. java并发中的Synchronized关键词
        • 3. java中的Volatile关键字使用
        • 4. java中wait和sleep的区别
        • 5. java中Future的使用
        • 6. java并发中ExecutorService的使用
        • 7. java中Runnable和Callable的区别
        • 8. java中ThreadLocal的使用
        • 9. java中线程的生命周期
        • 10. java中join的使用
        • 11. 怎么在java中关闭一个thread
        • 12. java中的Atomic类
        • 13. java中interrupt,interrupted和isInterrupted的区别
        • 14. java中的daemon thread
        • 15. java中ThreadPool的介绍和使用
        • 16. java 中的fork join框架
        • 17. java并发中CountDownLatch的使用
        • 18. java中CyclicBarrier的使用
        • 19. 在java中使用JMH(Java Microbenchmark Harness)做性能测试
        • 20. java中ThreadLocalRandom的使用
        • 21. java中FutureTask的使用
        • 22. java中CompletableFuture的使用
        • 23. java中使用Semaphore构建阻塞对象池
        • 24. 在java中构建高效的结果缓存
        • 25. java中CompletionService的使用
        • 26. 使用ExecutorService来停止线程服务
        • 27. 我们的线程被饿死了
        • 28. java中有界队列的饱和策略(reject policy)
        • 29. 由于不当的执行顺序导致的死锁
        • 30. 非阻塞同步机制和CAS
        • 31. 非阻塞算法(Lock-Free)的实现
        • 32. java内存模型(JMM)和happens-before
        • 33. java多线程之Phaser
        • 34. java中Locks的使用
        • 35. ABA问题的本质及其解决办法
        • 36. 并发和Read-copy update(RCU)
        • 37. 同步类的基础AbstractQueuedSynchronizer(AQS)
        • 38. java并发Exchanger的使用
      • 4-stream
        • 简介
        • 00001-java-8-streams-Introduction
        • 00002-functional-interface
        • 00003-lambda-best-practices
        • 00004-java-8-stream-ifelse
        • 00005-java-8-stream-map
        • 00006-java-rethrow
        • 00007-java-Collectors
        • 00008-java-8-stream-reduce
        • 00009-java-8-Spliterator
        • 00010-java-8-stream-foreach-break
        • 00011-java-8-predicate-chain
        • 00012-java-8-infinite-stream
        • 00013-java-8-stream-cust-pool
        • 00014-java-8-stream-peek
        • 00015-java-custom-collector
        • 00016-java-8-lambda-exception
      • 5-collections
        • 前言
        • 01-asList-arraylist
        • 02-Comparable-Comparator
        • 03-enumMap-enumSet
        • 04-Generics-in-deep
        • 05-hashMap-LinkedHashMap
        • 06-HashMap-TreeMap
        • 07-how-to-copy-list
        • 08-iterator-to-list
        • 09-java-fail-safe-fail-fast
        • 10-queue-overview
        • 11-PriorityQueue
        • 12-SynchronousQueue
        • 13-type-erase
        • 14-reference-referenceType
        • 15-skiplist-ConcurrentSkipListMap
        • 16-DelayQueue
      • 6-jvm
        • 00-java-jvm-all-in-one
        • 1. 小师妹学JVM之:JVM的架构和执行过程
        • 2. 终于我用JOL打破了你对java对象的所有想象
        • 3. 小师妹学JVM之:java的字节码byte code简介
        • 4. 小师妹学JVM之:Dirty cards和PLAB
        • 5. 小师妹学JVM之:JVM中栈的frames详解
        • 6. 如果你想写自己的Benchmark框架
        • 7. JVM详解之:java class文件的密码本
        • 8. JVM系列之:String,数组和集合类的内存占用大小
        • 9. JVM系列之:Contend注解和false-sharing
        • 10. JVM系列之:对象的锁状态和同步
        • 11. JVM系列之:String.intern和stringTable
        • 12. JVM系列之:String.intern的性能
        • 13. JVM详解之:本地变量的生命周期
        • 14. JVM详解之:HotSpot VM中的Intrinsic methods
        • 15. JVM系列之:通过一个例子分析JIT的汇编代码
        • 16. JVM详解之:类的加载链接和初始化
        • 17. 小师妹学JVM之:逃逸分析和TLAB
        • 18. JVM系列之:JIT中的Virtual Call
        • 19. JVM系列之:JIT中的Virtual Call接口
        • 20. JVM详解之:运行时常量池
        • 21. 小师妹学JVM之:JDK14中JVM的性能优化
        • 22. JVM系列之:从汇编角度分析Volatile
        • 23. JVM系列之:从汇编角度分析NullCheck
        • 24. 小师妹学JVM之:GC的垃圾回收算法
        • 25. 小师妹学JVM之:JVM中的Safepoints
        • 26. JVM系列之:再谈java中的safepoint
        • 27. troubleshoot之:用control+break解决线程死锁问题
        • 28. troubleshoot之:使用JFR解决内存泄露
        • 29. troubleshoot之:分析OutOfMemoryError异常
        • 30. troubleshoot之:使用JFR分析性能问题
        • 31. troubleshoot之:GC调优到底是什么
        • 32. JVM系列之:详解java object对象在heap中的结构
        • 33. 小师妹学JVM之:深入理解JIT和编译优化-你看不懂系列
        • 34. 小师妹学JVM之:JIT中的LogCompilation
        • 35. 小师妹学JVM之:JIT中的PrintCompilation
        • 36. 小师妹学JVM之:JIT中的PrintAssembly
        • 37. 小师妹学JVM之:JIT中的PrintAssembly续集
        • 38. 小师妹学JVM之:深入理解编译优化之循环展开和粗化锁
        • 39. 小师妹学JVM之:JIT的Profile神器JITWatch
        • 40. 小师妹学JVM之:cache line对代码性能的影响
      • 7-security
        • 00001-java-security-code-line-DOS
        • 00002-java-security-code-line-base
        • 00003-java-security-code-line-object
        • 00004-java-security-code-line-DLC
        • 00005-java-security-code-line-expresion
        • 00006-java-security-code-line-number
        • 00007-java-security-code-line-string
        • 00008-java-security-code-line-heap-pollution
        • 00009-java-security-code-line-object-copy
        • 00010-java-security-code-line-injection
        • 00011-java-security-code-line-input
        • 00012-java-security-code-line-mutability
        • 00013-java-security-code-line-method
        • 00014-java-security-code-line-exception
        • 00015-java-security-code-line-visibility-atomicity
        • 00016-java-security-code-line-lock
        • 00017-java-security-code-line-dead-lock
        • 00018-java-security-code-line-double-check-lock
        • 00019-java-security-code-line-thread
        • 00020-java-security-code-line-threadsafe
        • 00021-java-security-code-line-file-io
        • 00022-java-security-code-line-file-security
        • 00023-java-security-code-line-serialization
        • 00024-java-security-code-line-threadpool
      • 8-new-feature
        • 00-java-new-feature-all-in-one
        • 1. JDK11的重要新特性
        • 2. JDK12的五大重要新特性
        • 3. JDK13的六大重要新特性
        • 04-JDK9-java-module
        • 05-JDK9-String-Compact
        • 06-JDK9-jvm-xlog
        • 07-JDK10-var-usage
        • 08-JDK10-var-genericity-multiple-implements
        • 09-JDK10-var-anonymous-class
        • 10-JDK11-http-reactive
        • 11-JDK11-http-new
        • 12-JDK12-collectors-teeing
        • 13-JDK12-CompactNumberFormat
        • 14-JDK13-appCDS
        • 15. 一览为快,JDK14的新特性
        • 16. JDK 14的新特性:更加好用的NullPointerExceptions
        • 17-JDK14-records
        • 18-JDK14-text-block
        • 19-JDK14-switch
        • 20-JDK14-java-tools
        • 21-JDK14-jcmd
        • 22. JDK14的新特性:instanceof模式匹配
        • 23-JDK14-jfr-jmc-event-stream
        • 24-JDK15-new-features
        • 25-JDK15-release-new-features
        • 26-JDK16-new-features
        • 27-JDK17-new-features
      • 9-advanced-feature
        • 01-Java-Thread-Affinity
        • jna
          • 01-jni-overview
          • 02-jna-overview
          • 03-jna-Library-Mapping
          • 04-jna-type-mapping
          • 05-jna-type-mapping-details
          • 06-jna-memory
          • 07-jna-function
          • 08-jna-structure
          • 09-jna-callbacks
      • netty
        • 01 Netty Startup
        • 02 Netty Bytebuf
        • 03 Netty Architecture
        • 03-netty-bootstrap-ServerBootstrap
        • 04 Netty Channel
        • 04-netty-ChannelHandlerContext
        • 04-netty-ChannelPipeline
        • 04-netty-channel-group
        • 04-netty-channel-types
        • 04-netty-channel-vs-serverChannel
        • 04-netty-socketaddress
        • 05 Netty Channel Event
        • 05-netty-EventExecutor-EventExecutorGroup
        • 05-netty-eventloop-eventloopgroup
        • 05-netty-nioeventloop
        • 06 Netty Cheerup China
        • 07 Netty Stream Based Transport
        • 08 Netty Pojo Buf
        • 09 Netty Reconnect
        • 10 Netty Chat
        • 11 Netty Udp
        • 12 Netty Securechat
        • 13 Netty Customprotocol
        • 14-java-base64
        • 14-netty-ReplayingDecoder
        • 14-netty-codec-base64
        • 14-netty-codec-bytes
        • 14-netty-codec-json
        • 14-netty-codec-msg-to-bytebuf
        • 14-netty-codec-msg-to-msg
        • 14-netty-codec-object
        • 14-netty-codec-string
        • 14-netty-codec-xml
        • 14 Netty Cust Codec
        • 14-netty-frame-decoder
        • 15 Netty Buildin Frame Detection
        • 16 Netty Buildin Codec Common
        • 17-jboss-marshalling
        • 17-netty-marshalling
        • 17-netty-protobuf-UDP
        • 17 Netty Protobuf
        • 18 Netty Http Request
        • 19 Netty Http Client Request
        • 20 Netty Fileserver
        • 21 Netty Http Fileupload
        • 22 Netty Cors
        • 23 Netty Websocket Server
        • 24 Netty Websocket Server 2
        • 25 Netty Websocket Client
        • 26 Netty Secure Http 2
        • 27 Netty Http 2
        • 28 Netty Wrap Http 2
        • 29 Netty Flowcontrol
        • 30 Netty Http 2 Client
        • 31 Netty Framecodec Http 2
        • 32 Netty Http 2 Client Framecodec
        • 33 Netty Multiplex Http 2 Server
        • 34 Netty Multiple Server
        • 35 Netty Simple Proxy
        • 36 Netty Socks Support
        • 37 Netty Cust Socks Server
        • 38-netty-cust-port-unification
        • 39-netty-SelectorProvider-channelFactory
        • 40-netty-udt-support
        • 41-netty-udt-byte-message
        • 42-netty-rendezvous
        • 43-netty-reference-cound
        • 44-netty-tcp-fast-open
        • 45-netty-ByteBuf-ByteBuffer
        • 46-netty-future-executor
        • 47-netty-Thread-local-object-pool
        • 48-netty-fastThreadLocal
        • 49-netty-extensible-enum
        • 50-netty-Hashed-wheel-timer
        • 51-netty-Thread-Affinity
        • 52-netty-native-transport
        • 53-1-netty-kqueue-transport
        • 53-2-netty-epoll-transport
        • 54-netty-dns-over-tcp
        • 55-netty-dns-over-udp
        • 56-netty-dns-over-tls
        • 57-netty-dns-tcpserver
        • 58-netty-haproxy
      • 10-ORM
        • mybatis
          • 01-difference-between-#-and-$
    • reactive
      • reactive system初探
      • 02-reactive-stream
      • r2dbc
        • 01-r2dbc-introduce
        • 02-r2dbc-h2-in-depth
        • 03-r2dbc-mysql-in-depth
        • 04-spring-data-r2dbc
      • reactor
        • 01-introduction-to-reactor
        • 02-reactor-core-in-depth
        • 03-reactor-handle-errors
        • 04-reactor-thread-schedulers
    • scala
      • 00001 Scala Oo
      • 00002 Scala Base
      • 00003 Scala Functional
      • 00004 Scala Statically Typed
      • 5. 可扩展的scala
      • 00006 Scala Parameter
      • 00007 Scala Option Some Null
      • 00008 Scala Enumerations
      • 00009 Scala Partial Function
      • 00010 Scala Futures Promise
      • 00011 Scala Mutable Immutable Collection
      • 00012 Scala Either
      • 00013 Scala Covariance Contravariant
      • 00014 Scala Visibility
      • 00015 Scala Self Type
      • 00016 Scala Existential Type
      • 00017 Scala Higher Kinded
    • web-tech
      • 01-storage-api-limit
      • 02-web-storage-api
      • 03-webworker-kickoff
    • AI
      • 02-math
        • 01-singular-value
        • 02-probability-god-mod
        • 03-Turing-machine
        • 04-p-np-npc-problem
      • 03-machine-learning
        • 01-machine-learning-overview
      • 01-llma
        • langchain
          • 001-langchain-overview
          • 002-langchain-Prompts
          • 003-langchain-custprompts
          • 004-langchain-cust-example-selector
          • 005-langchain-llm
          • 006-langchain-chatmod
          • 007-langchain-output-parthcer
          • 008-langchain-retrieval-overview
          • 009-langchain-retrieval-document-loaders
    • AIGC
      • stable-diffusion
        • Stable diffusion 初学者指南
        • 构建一个优秀的Prompt
        • 轻松复现一张AI图片
        • Stable Diffusion中的常用术语解析
        • Stable diffusion中这些重要的参数你一定要会用
        • Stable Diffusion中的embedding
        • Stable diffusion中的models
        • Stable Diffusion WebUI详细使用指南
        • Stable diffusion采样器详解
        • 原来Stable Diffusion是这样工作的
        • hypernetwork在SD中是怎么工作的
        • SD中的VAE,你不能不懂
        • 手把手教你生成一幅好看的AI图片
        • 什么?这动物图片可以上国家地理?
        • After Detailer让图像自动修复
        • AI图像放大工具,图片放大无所不能
        • LoRA大模型微调的利器
    • Architecture
      • REST
        • 01 REST RES Tful
        • 02 REST Resource
        • 03 REST HATEOAS
      • auth
        • 01-SAML-startup
        • 02-openid-connect-startup
        • 03-OAuth-2.0-in-depth
        • 04-SAML-vs-OAuth2
        • 05-openid-connnect-with-onelogin
        • 06-keycloak-startup
        • 07-keycloak-saml-wildfly
        • 08-keycloak-with-other-system
        • 09-openid-Implicit-onelogin
      • common
        • 01-reactive-system
        • 02-reactive-stream
        • 03-authorization-service
        • 04-keycloak-cluster-in-depth
        • 05-concurrency-parallelism
        • 06-software-architecture
        • 07-data-flow-architecture
        • 09 Microservices Guide
        • 10 Microservices Monolith
        • 11 Serverless Architecture
      • distribution
        • 01 Basic Paxos
        • 02 Generalized Byzantine Paxos
        • 03 Cheap Paxos Fast Paxos
        • 04 Multi Paxos
        • 05 Raft
    • algorithm
      • 01-anime
        • 01-algorithm-bubble-sort
        • 02-algorithm-insertion-sort
        • 03-algorithm-selection-sort
        • 04-algorithm-merge-sort
        • 05-algorithm-quick-sort
        • 06-algorithm-count-sort
        • 07-algorithm-radix-sort
        • 08-algorithm-linked-list
        • 09-algorithm-doubly-linked-list
        • 10-algorithm-stack
        • 11-algorithm-AVL-tree
        • 12-algorithm-queue
        • 13-algorithm-dequeue
        • 14-algorithm-hashtable
        • 15-algorithm-binary-search-tree
        • algorithm-binary-heap
        • algorithm-cyclefinding
        • algorithm-fenwicktree
        • algorithm-recursion
        • algorithm-segmenttree
        • algorithm-suffix-array
        • algorithm-suffix-tree
        • algorithm-ternary-search-tree
        • algorithm-tire
    • cheatSheet
      • cheatsheet
        • 01-jdk8-GC-cheatsheet
        • 02-JDK9-GC-cheatsheet
        • 03-JDK10-GC-cheatsheet
        • 04-JDK11-GC-cheatsheet
        • 05-JDK12-13-14-GC-cheatsheet
      • mindmap
        • Architect
        • Bigdata
        • 区块链技术大合集
        • Golang
        • Java
        • Js
        • Patten
      • tips
        • 01 Db Primary Foregin Keys
        • 01-googleCloud-azure-aws
        • 02 Db Result Set Meta Data
        • 02 New Gitbook To Pdf
        • 03-semantic-version
        • 03 Swagger To Html Pdf
        • 04 Unicode Sorting
        • 05 Git Personal Access Token
        • 06-jetbrains-fleet
        • 07-git-largefile
        • 08-beidou-how-to-work
    • flutter
      • dart
        • 01-dart-variables
        • 02-dart-buildin-type
        • 03-dart-function
        • 04-dart-operator
        • 05-dart-exception
        • 06-dart-class
        • 07-dart-extend
        • 08-dart-Generics
        • 09-dart-packages
        • 10-dart-pubspec
        • 11-dart-create-package
        • 12-dart-async
        • 13-dart-generators
        • 14-dart-number-string
        • 15-dart-collection
        • 16-dart-url
        • 17-dart-date-time
        • 18-dart-math
        • 19-dart-decode-encode
        • 20-dart-html
        • 21-dart-http
        • 22-dart-websockets
        • 23-dart-file
        • 24-dart-null-safety
        • 25-dart-Isolates
        • 26-dart-extension-method
        • 27-dart-style
        • 28-dart-Libraries-effective
        • 29-dart-null-effective
        • 30-dart-collection
      • flutter
        • 01-flutter-architectural
        • 02-flutter-widget
        • 03-flutter-state
        • 04-flutter-BuildContext
        • 05-01-flutter-gestures-demo
        • 05-flutter-gestures
        • 06-flutter-Material-materialApp
        • 07-flutter-ui-layout-overview
        • 08-flutter-ui-layout-container
        • 09-flutter-ui-layout-gridview
        • 10-01-flutter-ui-layout-listview-more
        • 10-flutter-ui-layout-listview
        • 11-flutter-ui-layout-stack
        • 12-flutter-ui-layout-card
        • 13-flutter-ui-constraints
        • 14-flutter-ui-AspectRatio-FractionallySizedBox
        • 15-flutter-ui-boxes
        • 16-flutter-ui-builder
        • 17-flutter-ui-indexed-stack
        • 18-flutter-ui-wrap
        • 19-flutter-ui-offstage
        • 20-flutter-ui-flow
        • 21-flutter-ui-Transform
        • 22-flutter-ui-SliverAppBar
        • 23-flutter-ui-SliverList-SliverGrid
        • 24-flutter-ui-navigation-1
        • 25-flutter-ui-navigation-2
        • 26-flutter-ui-custom-themes
        • 26-flutter-ui-navigation-3
        • 27-flutter-ui-play-video
        • 28-flutter-ui-use-camera
        • 29-flutter-ui-animate-router
        • 30-flutter-ui-animate-resize
        • 31-flutter-ui-animate-controller
        • 32-flutter-ui-animate-download-button
        • 33-flutter-ui-animate-menu
        • 40-flutter-ui-effect-photo-filter
        • 50-flutter-MediaQuery
    • interview
      • architecture
        • 分布式系统
        • 设计模式
      • arithmetic
        • 数组字符串
        • 双指针
        • 滑动窗口
        • 矩阵
        • Hash表格
        • 区间
        • 栈
        • 链表
        • 二叉树
        • 图
        • 字典树
        • 回溯
        • 分治
        • Kadane算法
        • 二分查找
        • 堆
        • 位运算
        • 数学
        • 020-arithmetic-dynamic-planning
        • more
          • 001-arithmetic-01
          • 002-arithmetic-02
          • 算法基础面试题(三)
      • prepare
        • 经典IQ测试题
      • db
        • mysql
          • 001-mysql-01
          • 002-mysql-02
        • redis
          • 001-redis-01
      • java
        • base
          • java基础面试问题(一)
          • 面向对象
          • java基础面试问题(三)
          • Java异常面试题
          • more
            • 001-java-exception
        • collections
          • java集合面试问题(一)
          • java集合面试问题(二)
          • java集合面试问题(三)
          • java集合高级面试问题(一)
          • more
            • 深入理解java List
            • 深入理解java Map
        • concurrent
          • java并发和多线程面试题(一)
          • java并发和多线程面试题(二)
          • java并发和多线程面试题(三)
          • java并发高级面试题(一)
          • java并发高级面试题(二)
          • java并发高级面试题(三)
          • more
            • 007-java-do-you-know-lock
        • io
          • IO面试问题(一)
          • IO面试问题(二)
          • more
            • 高效IO 与 NIO
            • 高级IO应用
        • jvm
          • 001-java-jvm-01
          • 002-java-jvm-02
          • more
            • class字节码和类加载机制
            • 内存泄露
    • javascript
      • ecmascript
        • ecmascript-10
        • ecmascript-11
        • ecmascript-12
        • ecmascript-6
        • ecmascript-7
        • ecmascript-8
        • ecmascript-9
        • es6-Iterables-Iterator
        • es6-promise-generator
        • es8-shared-memory
        • es9-async-iteration
        • es9-regexp
        • js-built-in-objects-structures
        • js-closure
        • js-memory-management
        • js-modules
        • js-use-strict
        • object-oriented-js
      • koa
        • koa-startup
      • nodejs
        • 00001-nodejs-kickoff
        • 00002-nodejs-npm
        • 00003-nodejs-async
        • 00004-nodejs-http-express
        • 00005-nodejs-file-system
        • 00006-nodejs-profile
        • 00007-nodejs-docker-best-practices
        • 00008-nodejs-event
        • 00009-nodejs-event-more
        • 00010-nodejs-block-eventloop
        • 00011-nodejs-http-in-depth
        • 00012-nodejs-worker-thread
        • 00013-nodejs-childprocess
        • 00014-nodejs-cluster
        • 00015-nodejs-debug
    • python
      • 01-python-base
        • 01-python3-cheatsheet
        • 02-python-ipython
        • 03-python-number-list-string
        • 04-python-condition-control
        • 05-python-function
        • 06-python-data-structure
        • 07-python-module
        • 08-python-io
        • 09-python-error-exception
        • 10-python-class
        • 11-python-inner-obj
        • 12-Jupyter-Notebook
        • 13-python-struct-format-char
      • 02-numpy
        • 01-python-numpy-basic
        • 02-python-numpy-datatype
        • 03-python-numpy-scalar
        • 04-python-numpy-datatype-obj
        • 05-python-Structured-arrays
        • 06-python-numpy-genfromtxt
        • 07-python-numpy-broadcasting
        • 08-python-numpy-linear-algebra
        • 09-python-numpy-ndarray
        • 10-python-numpy-func
      • 03-pandas
        • 01-python-pandas-overview
        • 02-python-pandas-advanced
        • 03-python-pandas-data-structures
        • 04-python-pandas-merge
        • 05-python-pandas-reshaping-pivot
        • 06-python-pandas-text
        • 07-python-pandas-missingdata
        • 08-python-pandas-category
        • 09-python-pandas-plot
        • 10-python-pandas-statistical
        • 11-python-pandas-groupby
        • 12-python-pandas-window
        • 13-python-pandas-sparse-data
        • 14-python-pandas-options
        • 15-python-pandas-time
      • 04-flask
        • 0001-flask-overview
      • 05-statistic-demo
        • 01-pandas-titanic
        • 02-pandas-restaurant
    • server
      • computer-science
        • 01-network-and-performance
        • 02-http1.1-vs-http2
        • 03 Http 3
        • 04 Http Cache
        • 05 Http Cookie
        • 06 Web Socket
        • 07 Websocket Message
        • 08-ssl-tls-npn-alpn
        • 09 SOCKS
        • 10 SOCKS 5 More
        • 11 UDT
        • 12-MIME
        • 13-transfer-encodings
        • 14-kqueue-epoll
        • 15-stream-socket
        • 16-datagram-socket
        • 17-unix-domain-socket
        • 18-base64-encoding
        • 19-domain-name-service
        • 20-haproxy-protocol
        • 21-sctp
        • 22-sctp-package-in-detail
        • 23-memcached-text-protocol
        • 24-memcached-binary-protocol
        • 25-redis-protocol
        • 26-mqtt-protocol
        • 27-stomp-protocol
      • linux
        • 01 That Is Kill
        • 02-du-and-df
      • server
        • nginx
          • 01-nginx-http2
          • 02-nginx-proxy-protocol
        • tomcat
          • 00001-tomcat-native-startup
        • wildfly
          • 00001-wildfly-startup
          • 00002-wildfly-config-resource
          • 00003-wildfly-domain
          • 00004-wildfly-app-deployment
          • 00005-wildfly-cluster-domain
    • spring
      • 01-springbase
        • 1. Spring MVC 中的http Caching
        • 2. @SessionAttributes 和 @SessionAttribute的区别
        • 5. Spring中的IOC容器
        • 6. 在Spring中创建Bean
        • 7. 依赖注入
        • 8. Bean作用域简介
        • 9. Spring Bean 的生命周期回调
        • 10. IOC扩展
        • 11. spring中的注解
        • 12. 组件扫描
        • 13. jsr330 annotation
        • 14. Spring的Environment接口
        • 15. 事件机制
        • 16. 资源resources
        • 17. Spring中的BeanWrapper
        • 18. SpEL
        • 19. AOP
        • 20. AspectJ注解
        • 21. 基于Schema的AOP
        • 22. AOP代理
        • 23. Spring中的@Configurable
        • 24. 深入探讨Spring多级缓存:原理与性能优化
      • 02-springBoot
        • 1. Spring Boot中的测试
        • 2. Spring Boot的TestRestTemplate使用
        • 3. Spring Boot中使用Swagger CodeGen生成REST client
        • 4. 将Spring Boot应用程序注册成为系统服务
        • 5. Spring Boot中的Properties
        • 6. Spring Boot中Spring data注解的使用
        • 7. Spring Boot中使用@JsonComponent
        • 8. Shutdown SpringBoot App
        • 9. Spring Boot 之Spring data JPA简介
        • 10. Spring Boot JPA 中transaction的使用
        • 11. Spring Boot JPA中关联表的使用
        • 12. Spring Boot JPA的查询语句
        • 13. Spring Boot JPA中使用@Entity和@Table
        • 14. Spring Boot JPA中java 8 的应用
        • 15. 在Spring Boot中加载初始化数据
        • 16. 在Spring Boot中自定义filter
        • 17. 在Spring Boot中使用内存数据库
        • 18. Spring Boot国际化支持
        • 19. 在Spring Boot使用H2内存数据库
        • 20. Spring Boot 自定义banner
        • 21. 使用spring boot创建fat jar APP
        • 22. Spring Boot devtool的使用
        • 23. SpringBoot @ConfigurationProperties详解
        • 24. 自定义spring boot的自动配置
        • 25. Spring Boot的exit code
        • 26. Spring Boot注解
        • 27. Spring Boot Admin的使用
        • 00028-Spring-Boot-Starters
        • 29. Spring Boot Actuator
        • 30. 使用maven和fat jar/war运行应用程序的对比
        • 31. Maven Wrapper简介
        • 32. 自定义parent POM
        • 00033-Change-Default-Port-in-Spring-Boot
        • 00034-Bootstrap-a-Simple-Application
        • 35. 在Spring Boot中配置web app
        • 38. 从Spring迁移到Spring Boot
        • 39. Spring Boot @EnableAutoConfiguration和@Configuration的区别
        • 00040-springboot-docker-image
        • 00041-springboot-reactive-web
        • 00042-springboot-HATEOAS
        • 00043-springboot-HATEOAS-Fundamentals
      • 03-springBoot3
        • 0001-what-is-new-in-springboot3
        • 0002-use-native-image-in-springboot3
      • 04-springCloud
        • 1. Spring Cloud OpenFeign Demo
        • 2. Spring Cloud sleuth with zipkin over RabbitMQ demo
    • tools
      • gradle
        • 01-gradle-kick-off
        • 02-gradle-build-script
        • 03-gradle-incremental-build
        • 04-gradle-task-in-depth
        • 05-gradle-vs-maven
        • 06-gradle-build-java-projects
        • 07-Gradle-Nexus-Publish-Plugin
      • java
        • 1. 5个2020年你不能不知道的java IDE神器
        • 02-jvm-jconsole
        • 03-jvm-jmap-jhat
        • 04-jvm-jstack
        • 05-jvm-jstat
      • maven
        • 01-apache-maven-lifecycle
        • 02-apache-maven-toolchains
        • 03-apache-maven-git-repository
        • 04-maven-OSSRH
      • protocolbuf
        • 01 Protocolbuf Guide
        • 02 Protocolbuf Detail
        • 03 Protobuf Encoding
由 GitBook 提供支持
在本页
  • 网格 DFS 遍历的框架代码
  • 695. 岛屿的最大面积
  • 827. 最大人工岛
  • 463. 岛屿的周长
  • 200. 岛屿数量
  • 130. 被围绕的区域
  • 133. 克隆图
  • 399. 除法求值
  • 207. 课程表
  • 210. 课程表 II
  • 909. 蛇梯棋
  • 433. 最小基因变化
  • 127. 单词接龙--同443最小基因突变

这有帮助吗?

  1. docs
  2. interview
  3. arithmetic

图

网格 DFS 遍历的框架代码

如何避免这样的重复遍历呢?答案是标记已经遍历过的格子。以岛屿问题为例,我们需要在所有值为 1 的陆地格子上做 DFS 遍历。每走过一个陆地格子,就把格子的值改为 2,这样当我们遇到 2 的时候,就知道这是遍历过的格子了。

void dfs(int[][] grid, int r, int c) {
    // 判断 base case
    // 如果坐标 (r, c) 超出了网格范围,直接返回
    if (!inArea(grid, r, c)) {
        return;
    }
  // 如果这个格子不是岛屿,直接返回
    if (grid[r][c] != 1) {
        return;
    }
    grid[r][c] = 2; // 将格子标记为「已遍历过」

    // 访问上、下、左、右四个相邻结点
    dfs(grid, r - 1, c);
    dfs(grid, r + 1, c);
    dfs(grid, r, c - 1);
    dfs(grid, r, c + 1);
}

// 判断坐标 (r, c) 是否在网格中
boolean inArea(int[][] grid, int r, int c) {
    return 0 <= r && r < grid.length 
        	&& 0 <= c && c < grid[0].length;
}

给你一个大小为 m x n 的二进制矩阵 grid 。

岛屿 是由一些相邻的 1 (代表土地) 构成的组合,这里的「相邻」要求两个 1 必须在 水平或者竖直的四个方向上 相邻。你可以假设 grid 的四个边缘都被 0(代表水)包围着。

岛屿的面积是岛上值为 1 的单元格的数目。

计算并返回 grid 中最大的岛屿面积。如果没有岛屿,则返回面积为 0 。

public int maxAreaOfIsland(int[][] grid) {
    int res = 0;
    for (int r = 0; r < grid.length; r++) {
        for (int c = 0; c < grid[0].length; c++) {
            if (grid[r][c] == 1) {
                int a = area(grid, r, c);
                res = Math.max(res, a);
            }
        }
    }
    return res;
}

int area(int[][] grid, int r, int c) {
    if (!inArea(grid, r, c)) {
        return 0;
    }
    if (grid[r][c] != 1) {
        return 0;
    }
    grid[r][c] = 2;
    
    return 1 
        + area(grid, r - 1, c)
        + area(grid, r + 1, c)
        + area(grid, r, c - 1)
        + area(grid, r, c + 1);
}

boolean inArea(int[][] grid, int r, int c) {
    return 0 <= r && r < grid.length 
        	&& 0 <= c && c < grid[0].length;
}

给你一个大小为 n x n 二进制矩阵 grid 。最多 只能将一格 0 变成 1 。

返回执行此操作后,grid 中最大的岛屿面积是多少?

岛屿 由一组上、下、左、右四个方向相连的 1 形成。

大致的思路我们不难想到,我们先计算出所有岛屿的面积,在所有的格子上标记出岛屿的面积。然后搜索哪个海洋格子相邻的两个岛屿面积最大。

这种做法可能遇到一个问题。如下图中红色方框内的海洋格子,它的上边、左边都与岛屿相邻。

那么,我们不能在方格中标记岛屿的面积,而应该标记岛屿的索引(下标),另外用一个数组记录每个岛屿的面积。

可以看到,这道题实际上是对网格做了两遍 DFS:第一遍 DFS 遍历陆地格子,计算每个岛屿的面积并标记岛屿;第二遍 DFS 遍历海洋格子,观察每个海洋格子相邻的陆地格子。

class Solution {
    // 方向数组,用于表示上、下、左、右四个方向的坐标偏移
    static int[] d = {0, -1, 0, 1, 0};

    // 主函数,计算最大人工岛的面积
    public int largestIsland(int[][] grid) {
        int n = grid.length, res = 0;
        int[][] tag = new int[n][n]; // 用于标记岛屿的标签
        Map<Integer, Integer> area = new HashMap<Integer, Integer>(); // 存储每个岛屿的面积
        // 遍历整个grid,标记每个岛屿并计算面积
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 1 && tag[i][j] == 0) {
                  //相邻岛屿的tag都设置为同一个t值
                    int t = i * n + j + 1; // 计算岛屿的唯一标签
                    area.put(t, dfs(grid, i, j, tag, t)); // 计算并存储岛屿面积
                    res = Math.max(res, area.get(t)); // 更新最大岛屿面积
                }
            }
        }
        // 遍历grid中的海洋格子,计算将海洋格子变成陆地后的最大人工岛面积
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (grid[i][j] == 0) {
                    int z = 1;
                    Set<Integer> connected = new HashSet<Integer>(); // 用于记录与当前海洋格子相邻的岛屿标签
                    for (int k = 0; k < 4; k++) {
                        int x = i + d[k], y = j + d[k + 1];
                        // 判断相邻的岛屿是否有效并且未被记录
                        if (!valid(n, x, y) || tag[x][y] == 0 || connected.contains(tag[x][y])) {
                            continue;
                        }
                        z += area.get(tag[x][y]); // 计算当前岛屿面积
                        connected.add(tag[x][y]); // 记录当前岛屿标签
                    }
                    res = Math.max(res, z); // 更新最大人工岛面积
                }
            }
        }
        return res;
    }

    // 深度优先搜索,标记并计算岛屿面积
    public int dfs(int[][] grid, int x, int y, int[][] tag, int t) {
        int n = grid.length, res = 1;
        tag[x][y] = t; // 标记当前坐标
        // 遍历当前坐标的四个相邻坐标
        for (int i = 0; i < 4; i++) {
            int x1 = x + d[i], y1 = y + d[i + 1];
            // 判断相邻坐标是否有效、是否是陆地、是否未被标记
            if (valid(n, x1, y1) && grid[x1][y1] == 1 && tag[x1][y1] == 0) {
                res += dfs(grid, x1, y1, tag, t); // 递归搜索并计算岛屿面积
            }
        }
        return res;
    }

    // 判断坐标是否有效
    public boolean valid(int n, int x, int y) {
        return x >= 0 && x < n && y >= 0 && y < n;
    }
}

给定一个 row x col 的二维网格地图 grid ,其中:grid[i][j] = 1 表示陆地, grid[i][j] = 0 表示水域。

网格中的格子 水平和垂直 方向相连(对角线方向不相连)。整个网格被水完全包围,但其中恰好有一个岛屿(或者说,一个或多个表示陆地的格子相连组成的岛屿)。

岛屿中没有“湖”(“湖” 指水域在岛屿内部且不和岛屿周围的水相连)。格子是边长为 1 的正方形。网格为长方形,且宽度和高度均不超过 100 。计算这个岛屿的周长。

public int islandPerimeter(int[][] grid) {
    for (int r = 0; r < grid.length; r++) {
        for (int c = 0; c < grid[0].length; c++) {
            if (grid[r][c] == 1) {
                // 题目限制只有一个岛屿,计算一个即可
                return dfs(grid, r, c);
            }
        }
    }
    return 0;
}

int dfs(int[][] grid, int r, int c) {
    // 函数因为「坐标 (r, c) 超出网格范围」返回,对应一条黄色的边
    if (!inArea(grid, r, c)) {
        return 1;
    }
    // 函数因为「当前格子是海洋格子」返回,对应一条蓝色的边
    if (grid[r][c] == 0) {
        return 1;
    }
    // 函数因为「当前格子是已遍历的陆地格子」返回,和周长没关系
    if (grid[r][c] != 1) {
        return 0;
    }
    grid[r][c] = 2;
    return dfs(grid, r - 1, c)
        + dfs(grid, r + 1, c)
        + dfs(grid, r, c - 1)
        + dfs(grid, r, c + 1);
}

// 判断坐标 (r, c) 是否在网格中
boolean inArea(int[][] grid, int r, int c) {
    return 0 <= r && r < grid.length 
        	&& 0 <= c && c < grid[0].length;
}

给你一个由 '1'(陆地)和 '0'(水)组成的的二维网格,请你计算网格中岛屿的数量。

岛屿总是被水包围,并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。

此外,你可以假设该网格的四条边均被水包围。

class Solution {
    int ans=0;
    public int numIslands(char[][] grid) {
        
    for(int i=0; i< grid.length; i++){
        for (int j=0; j<grid[0].length;j++){
            if(grid[i][j] == '1'){
                ans++;
            dfs(grid,i,j);
            }
        }
    }
    return ans;
    }

    void dfs(char[][] grid, int r, int c) {
    // 判断 base case
    // 如果坐标 (r, c) 超出了网格范围,直接返回
    if (!inArea(grid, r, c)) {
        return;
    }
  // 如果这个格子不是岛屿,直接返回
    if (grid[r][c] != '1') {
        return;
    }

    grid[r][c] = '2'; // 将格子标记为「已遍历过」

    // 访问上、下、左、右四个相邻结点
    dfs(grid, r - 1, c);
    dfs(grid, r + 1, c);
    dfs(grid, r, c - 1);
    dfs(grid, r, c + 1);
}

// 判断坐标 (r, c) 是否在网格中
boolean inArea(char[][] grid, int r, int c) {
    return 0 <= r && r < grid.length 
        	&& 0 <= c && c < grid[0].length;
}
}

给你一个 m x n 的矩阵 board ,由若干字符 'X' 和 'O' ,找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充。

被围绕的区间不会存在于边界上,换句话说,任何边界上的 'O' 都不会被填充为 'X'。 任何不在边界上,或不与边界上的 'O' 相连的 'O' 最终都会被填充为 'X'。如果两个元素在水平或垂直方向相邻,则称它们是“相连”的。

反向寻找,找在边界上的O

class Solution {
    int n, m;

    public void solve(char[][] board) {
        n = board.length;
        if (n == 0) {
            return;
        }
        m = board[0].length;
       //遍历第一列和最后一列
        for (int i = 0; i < n; i++) {
            dfs(board, i, 0);
            dfs(board, i, m - 1);
        }
        //遍历第一行和最后一行(i需要排除已经遍历过的地方)
        for (int i = 1; i < m - 1; i++) {
            dfs(board, 0, i);
            dfs(board, n - 1, i);
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (board[i][j] == 'A') {
                    board[i][j] = 'O';
                } else if (board[i][j] == 'O') {
                    board[i][j] = 'X';
                }
            }
        }
    }

    public void dfs(char[][] board, int x, int y) {
        if (x < 0 || x >= n || y < 0 || y >= m || board[x][y] != 'O') {
            return;
        }
        board[x][y] = 'A';
        dfs(board, x + 1, y);
        dfs(board, x - 1, y);
        dfs(board, x, y + 1);
        dfs(board, x, y - 1);
    }
}

图中的每个节点都包含它的值 val(int) 和其邻居的列表(list[Node])。

class Node {
    public int val;
    public List<Node> neighbors;
}

解法:递归,用map保存是否已经克隆过

class Solution {
    private HashMap <Node, Node> visited = new HashMap <> ();
    public Node cloneGraph(Node node) {
        if (node == null) {
            return node;
        }
        // 如果该节点已经被访问过了,则直接从哈希表中取出对应的克隆节点返回
        if (visited.containsKey(node)) {
            return visited.get(node);
        }
        // 克隆节点,注意到为了深拷贝我们不会克隆它的邻居的列表
        Node cloneNode = new Node(node.val, new ArrayList());
        // 哈希表存储
        visited.put(node, cloneNode);

        // 遍历该节点的邻居并更新克隆节点的邻居列表
        for (Node neighbor: node.neighbors) {
            cloneNode.neighbors.add(cloneGraph(neighbor));
        }
        return cloneNode;
    }
}

给你一个变量对数组 equations 和一个实数值数组 values 作为已知条件,其中 equations[i] = [Ai, Bi] 和 values[i] 共同表示等式 Ai / Bi = values[i] 。每个 Ai 或 Bi 是一个表示单个变量的字符串。

另有一些以数组 queries 表示的问题,其中 queries[j] = [Cj, Dj] 表示第 j 个问题,请你根据已知条件找出 Cj / Dj = ? 的结果作为答案。

返回 所有问题的答案 。如果存在某个无法确定的答案,则用 -1.0 替代这个答案。如果问题中出现了给定的已知条件中没有出现的字符串,也需要用 -1.0 替代这个答案。

**注意:**输入总是有效的。你可以假设除法运算中不会出现除数为 0 的情况,且不存在任何矛盾的结果。

**注意:**未在等式列表中出现的变量是未定义的,因此无法确定它们的答案。

TODO ---并查集

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution {

    public double[] calcEquation(List<List<String>> equations, double[] values, List<List<String>> queries) {
        int equationsSize = equations.size();

        UnionFind unionFind = new UnionFind(2 * equationsSize);
        // 第 1 步:预处理,将变量的值与 id 进行映射,使得并查集的底层使用数组实现,方便编码
        Map<String, Integer> hashMap = new HashMap<>(2 * equationsSize);
        int id = 0;
        for (int i = 0; i < equationsSize; i++) {
            List<String> equation = equations.get(i);
            String var1 = equation.get(0);
            String var2 = equation.get(1);

            if (!hashMap.containsKey(var1)) {
                hashMap.put(var1, id);
                id++;
            }
            if (!hashMap.containsKey(var2)) {
                hashMap.put(var2, id);
                id++;
            }
            unionFind.union(hashMap.get(var1), hashMap.get(var2), values[i]);
        }

        // 第 2 步:做查询
        int queriesSize = queries.size();
        double[] res = new double[queriesSize];
        for (int i = 0; i < queriesSize; i++) {
            String var1 = queries.get(i).get(0);
            String var2 = queries.get(i).get(1);

            Integer id1 = hashMap.get(var1);
            Integer id2 = hashMap.get(var2);

            if (id1 == null || id2 == null) {
                res[i] = -1.0d;
            } else {
                res[i] = unionFind.isConnected(id1, id2);
            }
        }
        return res;
    }

    private class UnionFind {

        private int[] parent;

        /**
         * 指向的父结点的权值
         */
        private double[] weight;


        public UnionFind(int n) {
            this.parent = new int[n];
            this.weight = new double[n];
            for (int i = 0; i < n; i++) {
                parent[i] = i;
                weight[i] = 1.0d;
            }
        }

        public void union(int x, int y, double value) {
            int rootX = find(x);
            int rootY = find(y);
            if (rootX == rootY) {
                return;
            }

            parent[rootX] = rootY;
          	// 关系式的推导请见「参考代码」下方的示意图
            weight[rootX] = weight[y] * value / weight[x];
        }

        /**
         * 路径压缩
         *
         * @param x
         * @return 根结点的 id
         */
        public int find(int x) {
            if (x != parent[x]) {
                int origin = parent[x];
                parent[x] = find(parent[x]);
                weight[x] *= weight[origin];
            }
            return parent[x];
        }

        public double isConnected(int x, int y) {
            int rootX = find(x);
            int rootY = find(y);
            if (rootX == rootY) {
                return weight[x] / weight[y];
            } else {
                return -1.0d;
            }
        }
    }
}

你这个学期必须选修 numCourses 门课程,记为 0 到 numCourses - 1 。

在选修某些课程之前需要一些先修课程。 先修课程按数组 prerequisites 给出,其中 prerequisites[i] = [ai, bi] ,表示如果要学习课程 ai 则 必须 先学习课程 bi 。

  • 例如,先修课程对 [0, 1] 表示:想要学习课程 0 ,你需要先完成课程 1 。

请你判断是否可能完成所有课程的学习?如果可以,返回 true ;否则,返回 false 。

示例 1:

输入:numCourses = 2, prerequisites = [[1,0]]
输出:true
解释:总共有 2 门课程。学习课程 1 之前,你需要完成课程 0 。这是可能的。

示例 2:

输入:numCourses = 2, prerequisites = [[1,0],[0,1]]
输出:false
解释:总共有 2 门课程。学习课程 1 之前,你需要先完成课程 0 ;并且学习课程 0 之前,你还应先完成课程 1 。这是不可能的。

构建图:就是构建每个节点和其他节点之间的关系。可以用edges = new ArrayList<List>()这个二维list来表示。

解法1:拓扑排序,对图进行一遍深度优先搜索。当每个节点进行回溯的时候,我们把该节点放入栈中。最终从栈顶到栈底的序列就是一种拓扑排序。

由于我们只需要判断是否存在一种拓扑排序,而栈的作用仅仅是存放最终的拓扑排序结果,因此我们可以只记录每个节点的状态,而省去对应的栈。

对于图中的任意一个节点,它在搜索的过程中有三种状态,即:

「未搜索」:我们还没有搜索到这个节点;

「搜索中」:我们搜索过这个节点,但还没有回溯到该节点,即该节点还没有入栈,还有相邻的节点没有搜索完成);

「已完成」:我们搜索过并且回溯过这个节点,即该节点已经入栈,并且所有该节点的相邻节点都出现在栈的更底部的位置,满足拓扑排序的要求。

通过上述的三种状态,我们就可以给出使用深度优先搜索得到拓扑排序的算法流程,在每一轮的搜索搜索开始时,我们任取一个「未搜索」的节点开始进行深度优先搜索。

我们将当前搜索的节点 u 标记为「搜索中」,遍历该节点的每一个相邻节点 v:

如果 v 为「未搜索」,那么我们开始搜索 v,待搜索完成回溯到 u;

如果 v 为「搜索中」,那么我们就找到了图中的一个环,因此是不存在拓扑排序的;

如果 v 为「已完成」,那么说明 v 已经在栈中了,而 u 还不在栈中,因此 u 无论何时入栈都不会影响到 (u,v) 之前的拓扑关系,以及不用进行任何操作。

当 u 的所有相邻节点都为「已完成」时,我们将 u 放入栈中,并将其标记为「已完成」。

class Solution {
    List<List<Integer>> edges;
    int[] visited;
    boolean valid = true;

    public boolean canFinish(int numCourses, int[][] prerequisites) {
      //每门课都有一条边
        edges = new ArrayList<List<Integer>>();
        for (int i = 0; i < numCourses; ++i) {
            edges.add(new ArrayList<Integer>());
        }
      //存储课程是否被访问过
        visited = new int[numCourses];
        for (int[] info : prerequisites) {
          //添加课程之间的关联关系
            edges.get(info[1]).add(info[0]);
        }
        for (int i = 0; i < numCourses && valid; ++i) {
            if (visited[i] == 0) {
              //遍历每一条edges边
                dfs(i);
            }
        }
        return valid;
    }

    public void dfs(int u) {
      //搜索中
        visited[u] = 1;
        for (int v: edges.get(u)) {
            if (visited[v] == 0) {
                dfs(v);
                if (!valid) {
                    return;
                }
            } else if (visited[v] == 1) {
                valid = false;
                return;
            }
        }
      //搜索完成
        visited[u] = 2;
    }
}

解法2:广度优先

我们使用一个队列来进行广度优先搜索。初始时,所有入度为 0 的节点都被放入队列中,它们就是可以作为拓扑排序最前面的节点,并且它们之间的相对顺序是无关紧要的。

在广度优先搜索的每一步中,我们取出队首的节点 u:

我们将 u 放入答案中;

我们移除 u 的所有出边,也就是将 u 的所有相邻节点的入度减少 1。如果某个相邻节点 v 的入度变为 0,那么我们就将 v 放入队列中。

在广度优先搜索的过程结束后。如果答案中包含了这 n 个节点,那么我们就找到了一种拓扑排序,否则说明图中存在环,也就不存在拓扑排序了。

class Solution {
    List<List<Integer>> edges;
    int[] indeg;

    public boolean canFinish(int numCourses, int[][] prerequisites) {
        edges = new ArrayList<List<Integer>>();
        for (int i = 0; i < numCourses; ++i) {
            edges.add(new ArrayList<Integer>());
        }
        indeg = new int[numCourses];
        for (int[] info : prerequisites) {
            edges.get(info[1]).add(info[0]);
            ++indeg[info[0]];
        }

        Queue<Integer> queue = new LinkedList<Integer>();
        for (int i = 0; i < numCourses; ++i) {
            if (indeg[i] == 0) {
                queue.offer(i);
            }
        }
        int visited = 0;
        while (!queue.isEmpty()) {
            ++visited;
            int u = queue.poll();
            for (int v: edges.get(u)) {
                --indeg[v];
                if (indeg[v] == 0) {
                    queue.offer(v);
                }
            }
        }
        return visited == numCourses;
    }
}

现在你总共有 numCourses 门课需要选,记为 0 到 numCourses - 1。给你一个数组 prerequisites ,其中 prerequisites[i] = [ai, bi] ,表示在选修课程 ai 前 必须 先选修 bi 。

  • 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示:[0,1] 。

返回你为了学完所有课程所安排的学习顺序。可能会有多个正确的顺序,你只要返回 任意一种 就可以了。如果不可能完成所有课程,返回 一个空数组 。

解法: 1. 构建图(二维列表),2.遍历图,3.返回之后的结果。

深度优先

class Solution {
    // 存储有向图
    List<List<Integer>> edges;
    // 标记每个节点的状态:0=未搜索,1=搜索中,2=已完成
    int[] visited;
    // 用数组来模拟栈,下标 n-1 为栈底,0 为栈顶
    int[] result;
    // 判断有向图中是否有环
    boolean valid = true;
    // 栈下标
    int index;

    public int[] findOrder(int numCourses, int[][] prerequisites) {
        edges = new ArrayList<List<Integer>>();
        for (int i = 0; i < numCourses; ++i) {
            edges.add(new ArrayList<Integer>());
        }
        visited = new int[numCourses];
        result = new int[numCourses];
        index = numCourses - 1;
        for (int[] info : prerequisites) {
            edges.get(info[1]).add(info[0]);
        }
        // 每次挑选一个「未搜索」的节点,开始进行深度优先搜索
        for (int i = 0; i < numCourses && valid; ++i) {
            if (visited[i] == 0) {
                dfs(i);
            }
        }
        if (!valid) {
            return new int[0];
        }
        // 如果没有环,那么就有拓扑排序
        return result;
    }

    public void dfs(int u) {
        // 将节点标记为「搜索中」
        visited[u] = 1;
        // 搜索其相邻节点
        // 只要发现有环,立刻停止搜索
        for (int v: edges.get(u)) {
            // 如果「未搜索」那么搜索相邻节点
            if (visited[v] == 0) {
                dfs(v);
                if (!valid) {
                    return;
                }
            }
            // 如果「搜索中」说明找到了环
            else if (visited[v] == 1) {
                valid = false;
                return;
            }
        }
        // 将节点标记为「已完成」
        visited[u] = 2;
        // 将节点入栈,因为是递归,所以从最后面开始放
        result[index--] = u;
    }
}

广度优先

class Solution {
    // 存储有向图
    List<List<Integer>> edges;
    // 存储每个节点的入度
    int[] indeg;
    // 存储答案
    int[] result;
    // 答案下标
    int index;

    public int[] findOrder(int numCourses, int[][] prerequisites) {
        edges = new ArrayList<List<Integer>>();
        for (int i = 0; i < numCourses; ++i) {
            edges.add(new ArrayList<Integer>());
        }
        indeg = new int[numCourses];
        result = new int[numCourses];
        index = 0;
        for (int[] info : prerequisites) {
            edges.get(info[1]).add(info[0]);
            ++indeg[info[0]];
        }

        Queue<Integer> queue = new LinkedList<Integer>();
        // 将所有入度为 0 的节点放入队列中
        for (int i = 0; i < numCourses; ++i) {
            if (indeg[i] == 0) {
                queue.offer(i);
            }
        }

        while (!queue.isEmpty()) {
            // 从队首取出一个节点
            int u = queue.poll();
            // 放入答案中,广度优先,从前面开始放
            result[index++] = u;
            for (int v: edges.get(u)) {
                --indeg[v];
                // 如果相邻节点 v 的入度为 0,就可以选 v 对应的课程了
                if (indeg[v] == 0) {
                    queue.offer(v);
                }
            }
        }

        if (index != numCourses) {
            return new int[0];
        }
        return result;
    }
}

玩家从棋盘上的方格 1 (总是在最后一行、第一列)开始出发。

每一回合,玩家需要从当前方格 curr 开始出发,按下述要求前进:

  • 选定目标方格

    next

    ,目标方格的编号符合范围

    [curr + 1, min(curr + 6, n2)]

    。

    • 该选择模拟了掷 六面体骰子 的情景,无论棋盘大小如何,玩家最多只能有 6 个目的地。

  • 传送玩家:如果目标方格 next 处存在蛇或梯子,那么玩家会传送到蛇或梯子的目的地。否则,玩家传送到目标方格 next 。

  • 当玩家到达编号 n2 的方格时,游戏结束。

返回达到编号为 n2 的方格所需的最少移动次数,如果不可能,则返回 -1。

r 行 c 列的棋盘,按前述方法编号,棋盘格中可能存在 “蛇” 或 “梯子”;如果 board[r][c] != -1,那个蛇或梯子的目的地将会是 board[r][c]。编号为 1 和 n2 的方格上没有蛇或梯子。

注意,玩家在每回合的前进过程中最多只能爬过蛇或梯子一次:就算目的地是另一条蛇或梯子的起点,玩家也 不能 继续移动。

  • 举个例子,假设棋盘是 [[-1,4],[-1,3]] ,第一次移动,玩家的目标方格是 2 。那么这个玩家将会顺着梯子到达方格 3 ,但 不能 顺着方格 3 上的梯子前往方格 4 。

返回达到编号为 n2 的方格所需的最少移动次数,如果不可能,则返回 -1。

解法:广度优先搜索:将节点编号和到达该节点的移动次数作为搜索状态,顺着该节点的出边扩展新状态,直至到达终点 N^2 ,返回此时的移动次数。若无法到达终点则返回 −1。

代码实现时,我们可以用一个队列来存储搜索状态,初始时将起点状态 (1,0) 加入队列,表示当前位于起点 1,移动次数为 0。然后不断取出队首,每次取出队首元素时扩展新状态,即遍历该节点的出边,若出边对应节点未被访问,则将该节点和移动次数加一的结果作为新状态,加入队列。如此循环直至到达终点或队列为空。

class Solution {
    public int snakesAndLadders(int[][] board) {
        int n = board.length;
        boolean[] vis = new boolean[n * n + 1];
        Queue<int[]> queue = new LinkedList<int[]>();
      //表示当前位于起点 1,移动次数为 0
        queue.offer(new int[]{1, 0});
        while (!queue.isEmpty()) {
            int[] p = queue.poll();
            for (int i = 1; i <= 6; ++i) {
                int nxt = p[0] + i;
                if (nxt > n * n) { // 超出边界
                    break;
                }
                int[] rc = id2rc(nxt, n); // 得到下一步的行列
                if (board[rc[0]][rc[1]] > 0) { // 存在蛇或梯子
                    nxt = board[rc[0]][rc[1]];
                }
                if (nxt == n * n) { // 到达终点
                    return p[1] + 1;
                }
                if (!vis[nxt]) {
                    vis[nxt] = true;
                    queue.offer(new int[]{nxt, p[1] + 1}); // 扩展新状态
                }
            }
        }
        return -1;
    }

    public int[] id2rc(int id, int n) {
        int r = (id - 1) / n, c = (id - 1) % n;
        if (r % 2 == 1) {
            c = n - 1 - c;
        }
        return new int[]{n - 1 - r, c};
    }
}

基因序列可以表示为一条由 8 个字符组成的字符串,其中每个字符都是 'A'、'C'、'G' 和 'T' 之一。

假设我们需要调查从基因序列 start 变为 end 所发生的基因变化。一次基因变化就意味着这个基因序列中的一个字符发生了变化。

  • 例如,"AACCGGTT" --> "AACCGGTA" 就是一次基因变化。

另有一个基因库 bank 记录了所有有效的基因变化,只有基因库中的基因才是有效的基因序列。(变化后的基因必须位于基因库 bank 中)

给你两个基因序列 start 和 end ,以及一个基因库 bank ,请你找出并返回能够使 start 变化为 end 所需的最少变化次数。如果无法完成此基因变化,返回 -1 。

注意:起始基因序列 start 默认是有效的,但是它并不一定会出现在基因库中。

解法:广度搜索

class Solution {
    public int minMutation(String start, String end, String[] bank) {
        Set<String> cnt = new HashSet<String>();
        Set<String> visited = new HashSet<String>();
        char[] keys = {'A', 'C', 'G', 'T'};        
        for (String w : bank) {
            cnt.add(w);
        }
        if (start.equals(end)) {
            return 0;
        }
        if (!cnt.contains(end)) {
            return -1;
        }
        Queue<String> queue = new ArrayDeque<String>();
        queue.offer(start);
       //作用是判断在变化过程中有没有循环
        visited.add(start);
        int step = 1;
        while (!queue.isEmpty()) {
            int sz = queue.size();
            for (int i = 0; i < sz; i++) {
                String curr = queue.poll();
              //8 *4种变化
                for (int j = 0; j < 8; j++) {
                    for (int k = 0; k < 4; k++) {
                        if (keys[k] != curr.charAt(j)) {
                            StringBuffer sb = new StringBuffer(curr);
                            sb.setCharAt(j, keys[k]);
                            String next = sb.toString();
                            if (!visited.contains(next) && cnt.contains(next)) {
                                if (next.equals(end)) {
                                    return step;
                                }
                                queue.offer(next);
                                visited.add(next);
                            }
                        }
                    }
                }
            }
            step++;
        }
        return -1;
    }
}

字典 wordList 中从单词 beginWord 和 endWord 的 转换序列 是一个按下述规格形成的序列 beginWord -> s1 -> s2 -> ... -> sk:

  • 每一对相邻的单词只差一个字母。

  • 对于 1 <= i <= k 时,每个 si 都在 wordList 中。注意, beginWord 不需要在 wordList 中。

  • sk == endWord

给你两个单词 beginWord 和 endWord 和一个字典 wordList ,返回 从 beginWord 到 endWord 的 最短转换序列 中的 单词数目 。如果不存在这样的转换序列,返回 0 。

看到最短首先想到的就是广度优先搜索。想到广度优先搜索自然而然的就能想到图。

图就是一个二维列表。

解法1.广度优先,从开始单词开始,一个字符一个字符进行判断是否在字段里面,如果不在则添加到queue中。然后继续遍历。

public class Solution {

    public int ladderLength(String beginWord, String endWord, List<String> wordList) {
        // 第 1 步:先将 wordList 放到哈希表里,便于判断某个单词是否在 wordList 里
        Set<String> wordSet = new HashSet<>(wordList);
        if (wordSet.size() == 0 || !wordSet.contains(endWord)) {
            return 0;
        }
        wordSet.remove(beginWord);
        
        // 第 2 步:图的广度优先遍历,必须使用队列和表示是否访问过的 visited 哈希表
        Queue<String> queue = new LinkedList<>();
        queue.offer(beginWord);
        Set<String> visited = new HashSet<>();
        visited.add(beginWord);
        
        // 第 3 步:开始广度优先遍历,包含起点,因此初始化的时候步数为 1
        int step = 1;
        while (!queue.isEmpty()) {
            int currentSize = queue.size();
            for (int i = 0; i < currentSize; i++) {
                // 依次遍历当前队列中的单词
                String currentWord = queue.poll();
                // 如果 currentWord 能够修改 1 个字符与 endWord 相同,则返回 step + 1
                if (changeWordEveryOneLetter(currentWord, endWord, queue, visited, wordSet)) {
                    return step + 1;
                }
            }
            step++;
        }
        return 0;
    }

    /**
     * 尝试对 currentWord 修改每一个字符,看看是不是能与 endWord 匹配
     *
     * @param currentWord
     * @param endWord
     * @param queue
     * @param visited
     * @param wordSet
     * @return
     */
    private boolean changeWordEveryOneLetter(String currentWord, String endWord,
                                             Queue<String> queue, Set<String> visited, Set<String> wordSet) {
        char[] charArray = currentWord.toCharArray();
        for (int i = 0; i < endWord.length(); i++) {
            // 先保存,然后恢复
            char originChar = charArray[i];
            for (char k = 'a'; k <= 'z'; k++) {
                if (k == originChar) {
                    continue;
                }
                charArray[i] = k;
                String nextWord = String.valueOf(charArray);
                if (wordSet.contains(nextWord)) {
                    if (nextWord.equals(endWord)) {
                        return true;
                    }
                    if (!visited.contains(nextWord)) {
                        queue.add(nextWord);
                        // 注意:添加到队列以后,必须马上标记为已经访问
                        visited.add(nextWord);
                    }
                }
            }
            // 恢复
            charArray[i] = originChar;
        }
        return false;
    }
}

解法2.双向广度优先遍历

  • 已知目标顶点的情况下,可以分别从起点和目标顶点(终点)执行广度优先遍历,直到遍历的部分有交集。这种方式搜索的单词数量会更小一些;

  • 更合理的做法是,每次从单词数量小的集合开始扩散;

也就是先从起点开始遍历,然后再从终点开始遍历。再继续遍历下一层。因为起点和终点只有一个,所以这样可以减少数目。

分别用左边和右边扩散的哈希表代替单向 BFS 里的队列,它们在双向 BFS 的过程中交替使用
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Solution {

    public int ladderLength(String beginWord, String endWord, List<String> wordList) {
        // 第 1 步:先将 wordList 放到哈希表里,便于判断某个单词是否在 wordList 里
        Set<String> wordSet = new HashSet<>(wordList);
        if (wordSet.size() == 0 || !wordSet.contains(endWord)) {
            return 0;
        }

        // 第 2 步:已经访问过的 word 添加到 visited 哈希表里
        Set<String> visited = new HashSet<>();
        // 分别用左边和右边扩散的哈希表代替单向 BFS 里的队列,它们在双向 BFS 的过程中交替使用
        Set<String> beginVisited = new HashSet<>();
        beginVisited.add(beginWord);
        Set<String> endVisited = new HashSet<>();
        endVisited.add(endWord);

        // 第 3 步:执行双向 BFS,左右交替扩散的步数之和为所求
        int step = 1;
        while (!beginVisited.isEmpty() && !endVisited.isEmpty()) {
            // 优先选择小的哈希表进行扩散,考虑到的情况更少
            if (beginVisited.size() > endVisited.size()) {
                Set<String> temp = beginVisited;
                beginVisited = endVisited;
                endVisited = temp;
            }
            // 逻辑到这里,保证 beginVisited 是相对较小的集合,nextLevelVisited 在扩散完成以后,会成为新的 beginVisited
            Set<String> nextLevelVisited = new HashSet<>();
            for (String word : beginVisited) {
                if (changeWordEveryOneLetter(word, endVisited, visited, wordSet, nextLevelVisited)) {
                    return step + 1;
                }
            }
            // 原来的 beginVisited 废弃,从 nextLevelVisited 开始新的双向 BFS
            beginVisited = nextLevelVisited;
            step++;
        }
        return 0;
    }


    /**
     * 尝试对 word 修改每一个字符,看看是不是能落在 endVisited 中,扩展得到的新的 word 添加到 nextLevelVisited 里
     *
     * @param word
     * @param endVisited
     * @param visited
     * @param wordSet
     * @param nextLevelVisited
     * @return
     */
    private boolean changeWordEveryOneLetter(String word, Set<String> endVisited,
                                             Set<String> visited,
                                             Set<String> wordSet,
                                             Set<String> nextLevelVisited) {
        char[] charArray = word.toCharArray();
        for (int i = 0; i < word.length(); i++) {
            char originChar = charArray[i];
            for (char c = 'a'; c <= 'z'; c++) {
                if (originChar == c) {
                    continue;
                }
                charArray[i] = c;
                String nextWord = String.valueOf(charArray);
                if (wordSet.contains(nextWord)) {
                    if (endVisited.contains(nextWord)) {
                        return true;
                    }
                    if (!visited.contains(nextWord)) {
                        nextLevelVisited.add(nextWord);
                        visited.add(nextWord);
                    }
                }
            }
            // 恢复,下次再用
            charArray[i] = originChar;
        }
        return false;
    }
}
上一页二叉树下一页字典树

最后更新于1年前

这有帮助吗?

给你无向 图中一个节点的引用,请你返回该图的 (克隆)。

给你一个大小为 n x n 的整数矩阵 board ,方格按从 1 到 n2 编号,编号遵循 ,从左下角开始 (即,从 board[n - 1][0] 开始)每一行交替方向。

695. 岛屿的最大面积
827. 最大人工岛
463. 岛屿的周长
200. 岛屿数量
130. 被围绕的区域
133. 克隆图
连通
深拷贝
399. 除法求值
207. 课程表
210. 课程表 II
909. 蛇梯棋
转行交替方式
433. 最小基因变化
127. 单词接龙--同443最小基因突变