├── .all-contributorsrc ├── .github ├── dependabot.yml └── workflows │ ├── jvm.yml │ └── rust.yml ├── .gitignore ├── .scalafmt.conf ├── .sonarcloud.properties ├── .travis.yml ├── CONTRIBUTING-en.md ├── CONTRIBUTING.md ├── LICENSE ├── README-en.md ├── README.md ├── TechnologyStack-2020-11-16.png ├── build.gradle ├── buildSrc ├── build.gradle ├── settings.gradle └── src │ └── main │ └── groovy │ ├── custom.java.gradle │ ├── custom.kotlin.gradle │ └── custom.scala.gradle ├── build_before_commit.sh ├── countByAuthors.md ├── countByAuthors.sh ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── java-examples ├── README.md ├── build.gradle └── src │ └── main │ ├── java │ ├── cn │ │ └── edu │ │ │ └── jxnu │ │ │ ├── examples │ │ │ ├── concurrent │ │ │ │ ├── InterruptedException.java │ │ │ │ ├── InterruptedThread.java │ │ │ │ ├── MyThread.java │ │ │ │ ├── MyThreadBrother.java │ │ │ │ ├── MyThreadInfo.java │ │ │ │ ├── ThreadSafeStop.java │ │ │ │ ├── ThreadStateDemo.java │ │ │ │ └── package-info.java │ │ │ ├── design │ │ │ │ ├── AbstractFactoryMethod.java │ │ │ │ ├── Adapter.java │ │ │ │ ├── BuilderPattern.java │ │ │ │ ├── Composite.java │ │ │ │ ├── Decorator.java │ │ │ │ ├── Facade.jpg │ │ │ │ ├── FactoryMethod.java │ │ │ │ ├── FlyweightFactory.java │ │ │ │ ├── Mediator.java │ │ │ │ ├── Memento.java │ │ │ │ ├── Prototype.java │ │ │ │ ├── Proxy.java │ │ │ │ ├── ResponsibilityChain.java │ │ │ │ ├── Singleton_0.java │ │ │ │ ├── Singleton_ThreadSafe.java │ │ │ │ ├── Singleton_UnThreadSafe.java │ │ │ │ ├── Strategy.java │ │ │ │ ├── TemplateMethod.java │ │ │ │ ├── adapter │ │ │ │ │ ├── Adaptee.java │ │ │ │ │ ├── Adapter.java │ │ │ │ │ ├── DemoTest.java │ │ │ │ │ ├── TargetInterface.java │ │ │ │ │ ├── object │ │ │ │ │ │ ├── Adapter.java │ │ │ │ │ │ ├── DemoTest.java │ │ │ │ │ │ └── TargetInterface.java │ │ │ │ │ └── 适配器.jpg │ │ │ │ ├── builder │ │ │ │ │ └── UserDto.java │ │ │ │ ├── decorator │ │ │ │ │ ├── Component.java │ │ │ │ │ ├── ConcreteComponent.java │ │ │ │ │ ├── ConcreteDecorator.java │ │ │ │ │ ├── Decorator.java │ │ │ │ │ ├── TestDecorator.java │ │ │ │ │ └── 装饰器.jpeg │ │ │ │ ├── observer │ │ │ │ │ ├── Concrete2Observer.java │ │ │ │ │ ├── ConcreteObserver.java │ │ │ │ │ ├── ConcreteSubject.java │ │ │ │ │ ├── Observer.java │ │ │ │ │ ├── ObserverPatternSpec.java │ │ │ │ │ └── Subject.java │ │ │ │ ├── proxy.jpg │ │ │ │ ├── strategy │ │ │ │ │ ├── ChildUnder150CM.java │ │ │ │ │ ├── Context.java │ │ │ │ │ ├── ElderMoreThen60YearsOld.java │ │ │ │ │ ├── Main.jpg │ │ │ │ │ ├── PeopleComeTimeMoreThenThree.java │ │ │ │ │ ├── Strategy.java │ │ │ │ │ ├── Student.java │ │ │ │ │ ├── package-info.java │ │ │ │ │ └── 策略.jpg │ │ │ │ ├── visitor │ │ │ │ │ ├── ClientTest.java │ │ │ │ │ ├── ConcreteElement.java │ │ │ │ │ ├── ConcreteElement2.java │ │ │ │ │ ├── ConcreteVisitor.java │ │ │ │ │ ├── ElementNode.java │ │ │ │ │ ├── ObjectStruture.java │ │ │ │ │ ├── Visitor.java │ │ │ │ │ └── 访问者.png │ │ │ │ ├── 中介者.jpg │ │ │ │ ├── 享元.jpg │ │ │ │ ├── 原型.jpg │ │ │ │ ├── 备忘录_多重检查点.jpg │ │ │ │ ├── 工厂方法.jpg │ │ │ │ ├── 建造者.gif │ │ │ │ ├── 抽象工厂.jpg │ │ │ │ ├── 白箱备忘录.jpg │ │ │ │ ├── 组合.jpg │ │ │ │ ├── 观察者.jpg │ │ │ │ ├── 设计原则.txt │ │ │ │ ├── 访问者模式.png │ │ │ │ ├── 责任链.jpg │ │ │ │ ├── 适配器模式.jpg │ │ │ │ └── 黑箱备忘录.jpg │ │ │ ├── io │ │ │ │ ├── AIOEchoServer.java │ │ │ │ ├── HeavySocketClient.java │ │ │ │ ├── MultiThreadEchoServer.java │ │ │ │ ├── MultiThreadNIOEchoClient.java │ │ │ │ ├── MultiThreadNIOEchoServer.java │ │ │ │ ├── NIODemo1.java │ │ │ │ ├── NIODemo2.java │ │ │ │ ├── NIODemo3.java │ │ │ │ ├── NIODemo4.java │ │ │ │ ├── NIODemo5.java │ │ │ │ ├── TestBlockingNIO.java │ │ │ │ ├── TestBlockingNIO2.java │ │ │ │ ├── TestNonBlockingNIO.java │ │ │ │ ├── TestNonBlockingNIO2.java │ │ │ │ └── TestPipe.java │ │ │ ├── lambda │ │ │ │ ├── CompletableFutureDemo.java │ │ │ │ ├── CompletableFutureDemo2.java │ │ │ │ ├── CompletableFutureDemo3.java │ │ │ │ ├── IAnimal.java │ │ │ │ ├── Ihorse.java │ │ │ │ ├── Lambda.java │ │ │ │ ├── Lambda2.java │ │ │ │ ├── LongAdderDemo.java │ │ │ │ ├── Mule.java │ │ │ │ ├── NonFunc.java │ │ │ │ └── StampedLockDemo1.java │ │ │ ├── other │ │ │ │ ├── Base.java │ │ │ │ ├── JavaIntersectionList.java │ │ │ │ ├── LRU.java │ │ │ │ ├── LRUCache.java │ │ │ │ ├── MuchThreadDown.java │ │ │ │ ├── TestJava.java │ │ │ │ ├── TestJava1.java │ │ │ │ ├── TestJava2.java │ │ │ │ ├── TestJava3.java │ │ │ │ ├── TestJava5.java │ │ │ │ ├── TestJava6.java │ │ │ │ ├── TestJava7.java │ │ │ │ ├── TestJava8.java │ │ │ │ ├── TestJava9.java │ │ │ │ ├── TestJavaThread.java │ │ │ │ ├── TestMain1.java │ │ │ │ ├── TestMain2.java │ │ │ │ ├── TestMain3.java │ │ │ │ ├── TestMain4.java │ │ │ │ ├── TestMain5.java │ │ │ │ ├── TestMain6.java │ │ │ │ ├── TestMain7.java │ │ │ │ ├── TestMain8.java │ │ │ │ ├── TestStaticVar.java │ │ │ │ └── 接口.png │ │ │ ├── proxy │ │ │ │ ├── CommonProxy.java │ │ │ │ ├── HandlerInvocation.java │ │ │ │ ├── Interceptor.java │ │ │ │ └── LoggerInterceptor.java │ │ │ ├── reflect │ │ │ │ ├── Adapt.java │ │ │ │ ├── Annotations.java │ │ │ │ ├── ArraySet.java │ │ │ │ ├── BFCompiler.java │ │ │ │ ├── BFCompilerTest.java │ │ │ │ ├── Compile.java │ │ │ │ ├── DependencyVisitor.java │ │ │ │ ├── DumpMethods.java │ │ │ │ ├── Expression.java │ │ │ │ ├── Helloworld.java │ │ │ │ ├── IndyCompile.java │ │ │ │ ├── Jbfc.java │ │ │ │ ├── NotNull.java │ │ │ │ ├── RT.java │ │ │ │ ├── ReflectTest.java │ │ │ │ ├── User.java │ │ │ │ └── package-info.java │ │ │ ├── socket │ │ │ │ ├── ClientDemo.java │ │ │ │ └── ServerSocketDemo.java │ │ │ ├── sort │ │ │ │ ├── BubbleSort.java │ │ │ │ ├── BucketSort.java │ │ │ │ ├── Constant.java │ │ │ │ ├── CountSort.java │ │ │ │ ├── HeapSort.java │ │ │ │ ├── InsertionSort.java │ │ │ │ ├── MergeSort.java │ │ │ │ ├── QuickSort.java │ │ │ │ ├── RadixSort.java │ │ │ │ ├── SelectSort.java │ │ │ │ └── ShellSort.java │ │ │ └── sql │ │ │ │ ├── ConnectionDriver.java │ │ │ │ ├── ConnectionPool.java │ │ │ │ ├── ConnectionPoolTest.java │ │ │ │ └── SQL执行过程简介.jpg │ │ │ ├── functional │ │ │ └── README.md │ │ │ └── reactive │ │ │ ├── JavaStreamExamples.java │ │ │ ├── Rooter.java │ │ │ ├── RxJavaExample.java │ │ │ ├── RxJavaExampleDriver.java │ │ │ └── SideEffecting.java │ └── io │ │ └── github │ │ └── dreamylost │ │ └── compiler │ │ ├── ClassFileManager.java │ │ ├── DynamicCompiler.java │ │ ├── JavaClassObject.java │ │ ├── JavaCodeObject.java │ │ ├── RunJavaCompiler.java │ │ └── Utils.java │ └── resources │ └── HelloWorld.java.tpl ├── java-leetcode ├── README.md ├── build.gradle └── src │ ├── main │ └── java │ │ └── io │ │ └── github │ │ ├── ccccmaster │ │ ├── Leetcode_1160.java │ │ ├── Leetcode_674.java │ │ ├── Leetcode_JZ_53.java │ │ └── ccccmaster.md │ │ ├── dreamylost │ │ ├── BFS.java │ │ ├── BinarySearch.java │ │ ├── Interview_02_03.java │ │ ├── Interview_04_06.java │ │ ├── Interview_04_08.java │ │ ├── LeetCode_237.java │ │ ├── Leetcdoe_1379.java │ │ ├── Leetcode_11_Double_Pointer.java │ │ ├── Leetcode_122.java │ │ ├── Leetcode_128_Hash.java │ │ ├── Leetcode_130_DFS.java │ │ ├── Leetcode_131_Backtracking.java │ │ ├── Leetcode_141.java │ │ ├── Leetcode_141_Double_Pointer.java │ │ ├── Leetcode_142_Double_Pointer.java │ │ ├── Leetcode_155_DataStructure.java │ │ ├── Leetcode_15_Double_Pointer.java │ │ ├── Leetcode_167_Double_Pointer.java │ │ ├── Leetcode_168_Math.java │ │ ├── Leetcode_169_Math.java │ │ ├── Leetcode_16_Double_Pointer.java │ │ ├── Leetcode_172_Math.java │ │ ├── Leetcode_17_Backtracking.java │ │ ├── Leetcode_19_Double_Pointer.java │ │ ├── Leetcode_1_Hash.java │ │ ├── Leetcode_200_DFS.java │ │ ├── Leetcode_204_Math.java │ │ ├── Leetcode_205_String.java │ │ ├── Leetcode_209_Double_Pointer.java │ │ ├── Leetcode_20_DataStructure.java │ │ ├── Leetcode_216_Backtracking.java │ │ ├── Leetcode_217_Hash.java │ │ ├── Leetcode_225_DataStructure.java │ │ ├── Leetcode_232_DataStructure.java │ │ ├── Leetcode_234_Double_Pointer.java │ │ ├── Leetcode_238_Math.java │ │ ├── Leetcode_241.java │ │ ├── Leetcode_242_String.java │ │ ├── Leetcode_257_DFS.java │ │ ├── Leetcode_26_Double_Pointer.java │ │ ├── Leetcode_27.java │ │ ├── Leetcode_28.java │ │ ├── Leetcode_283_ArrayMatrix.java │ │ ├── Leetcode_290_String.java │ │ ├── Leetcode_3.java │ │ ├── Leetcode_303_Interval.java │ │ ├── Leetcode_326_Math.java │ │ ├── Leetcode_344_Double_Pointer.java │ │ ├── Leetcode_345_Double_Pointer.java │ │ ├── Leetcode_347_Sort.java │ │ ├── Leetcode_349_Double_Pointer.java │ │ ├── Leetcode_350_Double_Pointer.java │ │ ├── Leetcode_367_Math.java │ │ ├── Leetcode_37_Backtracking.java │ │ ├── Leetcode_392.java │ │ ├── Leetcode_405_Math.java │ │ ├── Leetcode_409_String.java │ │ ├── Leetcode_40_Backtracking.java │ │ ├── Leetcode_413_Interval.java │ │ ├── Leetcode_415_Math.java │ │ ├── Leetcode_417_DFS.java │ │ ├── Leetcode_42_Double_Pointer.java │ │ ├── Leetcode_441.java │ │ ├── Leetcode_448_Array.java │ │ ├── Leetcode_451_Sort.java │ │ ├── Leetcode_452.java │ │ ├── Leetcode_455.java │ │ ├── Leetcode_462_Math.java │ │ ├── Leetcode_46_Backtracking.java │ │ ├── Leetcode_47_Backtracking.java │ │ ├── Leetcode_485_Array.java │ │ ├── Leetcode_503_DataStructure.java │ │ ├── Leetcode_504_Math.java │ │ ├── Leetcode_524.java │ │ ├── Leetcode_53_Interval.java │ │ ├── Leetcode_540.java │ │ ├── Leetcode_566_Matrix.java │ │ ├── Leetcode_567_Double_Pointer.java │ │ ├── Leetcode_572.java │ │ ├── Leetcode_583_LCS.java │ │ ├── Leetcode_594_Hash.java │ │ ├── Leetcode_605.java │ │ ├── Leetcode_628_Math.java │ │ ├── Leetcode_633_Double_Pointer.java │ │ ├── Leetcode_645_Array.java │ │ ├── Leetcode_647_String.java │ │ ├── Leetcode_665.java │ │ ├── Leetcode_67_Math.java │ │ ├── Leetcode_680_Double_Pointer.java │ │ ├── Leetcode_69.java │ │ ├── Leetcode_695_DFS.java │ │ ├── Leetcode_696_String.java │ │ ├── Leetcode_713_Double_Pointer.java │ │ ├── Leetcode_72_LCS.java │ │ ├── Leetcode_739_DataStructure.java │ │ ├── Leetcode_75_Double_Pointer.java │ │ ├── Leetcode_763.java │ │ ├── Leetcode_77_Backtracking.java │ │ ├── Leetcode_78_Backtracking.java │ │ ├── Leetcode_79_Backtracking.java │ │ ├── Leetcode_844_Double_Pointer.java │ │ ├── Leetcode_86_Double_Pointer.java │ │ ├── Leetcode_881_Double_Pointer.java │ │ ├── Leetcode_88_Double_Pointer.java │ │ ├── Leetcode_90_Backtracking.java │ │ ├── Leetcode_93_DFS.java │ │ ├── Leetcode_9_String.java │ │ ├── Leetcode_Interview_15.java │ │ ├── Leetcode_Interview_17_12.java │ │ ├── Leetcode_JZ_68_1.java │ │ ├── Leetcode_JZ_68_2.java │ │ ├── Leetcode_LCP_1.java │ │ ├── Leetocde_215_Sort.java │ │ ├── ListNode.java │ │ ├── TreeNode.java │ │ ├── dp │ │ │ ├── Knapsack_Dp.java │ │ │ ├── Leetcode_121_Dp.java │ │ │ ├── Leetcode_123_Dp.java │ │ │ ├── Leetcode_139_Dp.java │ │ │ ├── Leetcode_188_Dp.java │ │ │ ├── Leetcode_198_Dp.java │ │ │ ├── Leetcode_279_Dp.java │ │ │ ├── Leetcode_300_Dp.java │ │ │ ├── Leetcode_322_Dp.java │ │ │ ├── Leetcode_343_Dp.java │ │ │ ├── Leetcode_376_Dp.java │ │ │ ├── Leetcode_377_Dp.java │ │ │ ├── Leetcode_416_Dp.java │ │ │ ├── Leetcode_474_Dp.java │ │ │ ├── Leetcode_494_Dp.java │ │ │ ├── Leetcode_646_Dp.java │ │ │ ├── Leetcode_650_Dp.java │ │ │ ├── Leetcode_70_Dp.java │ │ │ ├── Leetcode_826_Dp.java │ │ │ ├── Leetcode_845_Dp.java │ │ │ ├── Leetcode_91_Dp.java │ │ │ ├── LengthOfLCS_Dp.java │ │ │ └── package-info.java │ │ ├── dreamylost.md │ │ ├── note.java │ │ ├── practice │ │ │ ├── AddMinFunctionForStack.java │ │ │ ├── AddMinFunctionForStack2.java │ │ │ ├── AddMinFunctionForStack3.java │ │ │ ├── AddMinFunctionForStack4.java │ │ │ ├── BinaryRepresentation.java │ │ │ ├── Brackets.java │ │ │ ├── CalculateStringDistance.java │ │ │ ├── CharSort.java │ │ │ ├── ConvertBSTToList.java │ │ │ ├── DeleteDuplication.java │ │ │ ├── DifferentNumberOfBinaryBits_Java.java │ │ │ ├── DigitOccurrence.java │ │ │ ├── Duplicate.java │ │ │ ├── EntryNodeOfLoop.java │ │ │ ├── FibonacciDemo.java │ │ │ ├── FibonacciDemo2.java │ │ │ ├── FibonacciDemo3.java │ │ │ ├── Find.java │ │ │ ├── FindFirstCommon.java │ │ │ ├── FindGreatestSumOfSubArray.java │ │ │ ├── FindKthToTail.java │ │ │ ├── FindNumbers.java │ │ │ ├── FindNumsAppearOnce.java │ │ │ ├── FindPath.java │ │ │ ├── FindSequence.java │ │ │ ├── FindTheNumber.java │ │ │ ├── FirstAppearingOnce.java │ │ │ ├── Get20Integers.java │ │ │ ├── GetGcd.java │ │ │ ├── GetLeastNumbers.java │ │ │ ├── GetMaxDistance.java │ │ │ ├── GetMaximumDistance.java │ │ │ ├── GetMedianInStream.java │ │ │ ├── GetNextNode.java │ │ │ ├── GetNumberOfK.java │ │ │ ├── GetUglyNumber.java │ │ │ ├── HasPathInMatrix.java │ │ │ ├── IsBalanced_Solution.java │ │ │ ├── IsContinuous.java │ │ │ ├── IsHasSubtree.java │ │ │ ├── IsNumeric.java │ │ │ ├── IsPopOrderForStack.java │ │ │ ├── IsSymmetrical.java │ │ │ ├── IsThePowerOf2.java │ │ │ ├── KthNode.java │ │ │ ├── LIS.java │ │ │ ├── LastRemaining.java │ │ │ ├── LeftRotate.java │ │ │ ├── LinkedHashMapDemo1.java │ │ │ ├── ListNode.java │ │ │ ├── LocationOf1FromRight.java │ │ │ ├── Main0.java │ │ │ ├── Main1.java │ │ │ ├── Main10.java │ │ │ ├── Main11.java │ │ │ ├── Main12.java │ │ │ ├── Main13.java │ │ │ ├── Main14.java │ │ │ ├── Main15.java │ │ │ ├── Main16.java │ │ │ ├── Main2.java │ │ │ ├── Main3.java │ │ │ ├── Main4.java │ │ │ ├── Main5.java │ │ │ ├── Main6.java │ │ │ ├── Main7.java │ │ │ ├── Main8.java │ │ │ ├── Main9.java │ │ │ ├── Match.java │ │ │ ├── MaxInWindowSize.java │ │ │ ├── MaximumProductSubarray.java │ │ │ ├── MergeLinkList.java │ │ │ ├── MoreThanHalfNum.java │ │ │ ├── Multiply.java │ │ │ ├── NumberOf0.java │ │ │ ├── NumberOf1.java │ │ │ ├── NumberOf1Between1AndN.java │ │ │ ├── O1checkPowerOf2.java │ │ │ ├── PalindromeArray.java │ │ │ ├── PermutationString.java │ │ │ ├── PowerTest.java │ │ │ ├── PringTreeFromTopToBottom.java │ │ │ ├── PrintMatrix.java │ │ │ ├── PrintMinNumber.java │ │ │ ├── PrintTreeByLevel.java │ │ │ ├── PrintTreeLikeZ.java │ │ │ ├── PublicTreePrint.java │ │ │ ├── RandomListDemo.java │ │ │ ├── RandomListNode.java │ │ │ ├── ReOrderArray.java │ │ │ ├── RebuildBinaryTree.java │ │ │ ├── ReplaceSpace.java │ │ │ ├── ReverseLinkedList.java │ │ │ ├── ReverseReadLinkedList.java │ │ │ ├── ReverseSentence.java │ │ │ ├── RightShift.java │ │ │ ├── SearchMinInArray.java │ │ │ ├── SecondLargeNumbers.java │ │ │ ├── SerializeTree.java │ │ │ ├── SlidingWindowMedian.java │ │ │ ├── SortDemo.java │ │ │ ├── SpeciallyAdd.java │ │ │ ├── SquarePattern.java │ │ │ ├── StrToInt.java │ │ │ ├── StringDisplacement.java │ │ │ ├── Sum.java │ │ │ ├── Test1.java │ │ │ ├── ToMirror.java │ │ │ ├── TrappingRainWater.java │ │ │ ├── TreeLinkNode.java │ │ │ ├── TreeNode.java │ │ │ ├── UniquePaths.java │ │ │ ├── UpdateBits.java │ │ │ ├── UseTwoStack2BeQueue.java │ │ │ └── VerifySquenceOfBST.java │ │ └── tooffer │ │ │ ├── ListNode.java │ │ │ ├── RandomListNode.java │ │ │ ├── T1.java │ │ │ ├── T10.java │ │ │ ├── T11.java │ │ │ ├── T12.java │ │ │ ├── T13.java │ │ │ ├── T14.java │ │ │ ├── T15.java │ │ │ ├── T16.java │ │ │ ├── T17.java │ │ │ ├── T18.java │ │ │ ├── T19.java │ │ │ ├── T2.java │ │ │ ├── T20.java │ │ │ ├── T21.java │ │ │ ├── T22.java │ │ │ ├── T23.java │ │ │ ├── T24.java │ │ │ ├── T25.java │ │ │ ├── T26.java │ │ │ ├── T27.java │ │ │ ├── T28.java │ │ │ ├── T29.java │ │ │ ├── T3.java │ │ │ ├── T30.java │ │ │ ├── T31.java │ │ │ ├── T32.java │ │ │ ├── T33.java │ │ │ ├── T34.java │ │ │ ├── T35.java │ │ │ ├── T36.java │ │ │ ├── T37.java │ │ │ ├── T38.java │ │ │ ├── T39.java │ │ │ ├── T4.java │ │ │ ├── T40.java │ │ │ ├── T41.java │ │ │ ├── T42.java │ │ │ ├── T43.java │ │ │ ├── T44.java │ │ │ ├── T45.java │ │ │ ├── T46.java │ │ │ ├── T47.java │ │ │ ├── T48.java │ │ │ ├── T49.java │ │ │ ├── T5.java │ │ │ ├── T50.java │ │ │ ├── T51.java │ │ │ ├── T52.java │ │ │ ├── T53.java │ │ │ ├── T54.java │ │ │ ├── T55.java │ │ │ ├── T56.java │ │ │ ├── T57.java │ │ │ ├── T58.java │ │ │ ├── T59.java │ │ │ ├── T6.java │ │ │ ├── T60.java │ │ │ ├── T61.java │ │ │ ├── T62.java │ │ │ ├── T63.java │ │ │ ├── T64.java │ │ │ ├── T65.java │ │ │ ├── T66.java │ │ │ ├── T7.java │ │ │ ├── T8.java │ │ │ ├── T9.java │ │ │ ├── TreeLinkNode.java │ │ │ ├── TreeNode.java │ │ │ └── package-info.java │ │ ├── poorguy │ │ ├── annotation │ │ │ ├── NotNull.java │ │ │ └── Nullable.java │ │ ├── explore │ │ │ └── learn │ │ │ │ ├── arrays │ │ │ │ ├── CheckIfNAndItsDoubleExist.java │ │ │ │ ├── DuplicateZeros.java │ │ │ │ ├── FindAllNumbersDisappearedInAnArray.java │ │ │ │ ├── FindNumbersWithEvenNumberOfDigits.java │ │ │ │ ├── HeightChecker.java │ │ │ │ ├── MaxConsecutiveOnes.java │ │ │ │ ├── MergeSortedArray.java │ │ │ │ ├── MoveZeroes.java │ │ │ │ ├── RemoveDuplicatesFromSortedArray.java │ │ │ │ ├── RemoveElement.java │ │ │ │ ├── ReplaceElementsWithGreatestElementOnRightSide.java │ │ │ │ ├── SortArrayByParity.java │ │ │ │ ├── SquaresOfASortedArray.java │ │ │ │ ├── ThirdMaximumNumber.java │ │ │ │ └── ValidMountainArray.java │ │ │ │ ├── binarysearch │ │ │ │ ├── BinarySearch.java │ │ │ │ ├── FindKClosestElements.java │ │ │ │ ├── FindKthSmallestPairDistance.java │ │ │ │ ├── FindMinimumInRotatedSortedArray.java │ │ │ │ ├── FindMinimumInRotatedSortedArray2.java │ │ │ │ ├── FindPeakElement.java │ │ │ │ ├── FindSmallestLetterGreaterThanTarget.java │ │ │ │ ├── FindTheDuplicateNumber.java │ │ │ │ ├── FirstBadVersion.java │ │ │ │ ├── GuessGame.java │ │ │ │ ├── GuessNumberHigherOrLower.java │ │ │ │ ├── IntersectionOfTwoArrays.java │ │ │ │ ├── IntersectionOfTwoArrays2.java │ │ │ │ ├── MedianOfTwoSortedArrays.java │ │ │ │ ├── Pow.java │ │ │ │ ├── SearchForARange.java │ │ │ │ ├── SearchInRotatedSortedArray.java │ │ │ │ ├── Sqrt.java │ │ │ │ ├── TwoSum2.java │ │ │ │ ├── ValidPerfectSquare.java │ │ │ │ └── VersionControl.java │ │ │ │ ├── binarytree │ │ │ │ ├── BinaryTreeInorderTraversal.java │ │ │ │ ├── BinaryTreeLevelOrderTraversal.java │ │ │ │ ├── BinaryTreePostorderTraversal.java │ │ │ │ ├── BinaryTreePreorderTraversal.java │ │ │ │ ├── ConstructBinaryTreeFromInorderAndPostorderTraversal.java │ │ │ │ ├── ConstructBinaryTreeFromPreorderAndInorderTraversal.java │ │ │ │ ├── LowestCommonAncestorOfABinaryTree.java │ │ │ │ ├── MaximumDepthOfBinaryTree.java │ │ │ │ ├── Node.java │ │ │ │ ├── PathSum.java │ │ │ │ ├── PopulatingNextRightPointersInEachNode.java │ │ │ │ ├── PopulatingNextRightPointersInEachNode2.java │ │ │ │ ├── SerializeAndDeserializeBinaryTree.java │ │ │ │ ├── SymmetricTree.java │ │ │ │ └── TreeNode.java │ │ │ │ ├── dynamicprogramming │ │ │ │ ├── ClimbingStairs.java │ │ │ │ ├── LongestCommonSubsequence.java │ │ │ │ ├── LongestSubstringWithoutRepeatingCharacters.java │ │ │ │ ├── MinCostClimbingStairs.java │ │ │ │ ├── MinimumCostTreeFromLeafValues.java │ │ │ │ └── README.md │ │ │ │ ├── hashtable │ │ │ │ ├── ContainsDuplicate.java │ │ │ │ ├── ContainsDuplicate2.java │ │ │ │ ├── DesignHashMap.java │ │ │ │ ├── DesignHashSet.java │ │ │ │ ├── FindDuplicateSubtrees.java │ │ │ │ ├── FirstUniqueCharacterInAString.java │ │ │ │ ├── FourSum2.java │ │ │ │ ├── GroupAnagrams.java │ │ │ │ ├── HappyNumber.java │ │ │ │ ├── IsomorphicStrings.java │ │ │ │ ├── JewelsAndStones.java │ │ │ │ ├── LongestSubstringWithoutRepeatingCharacters.java │ │ │ │ ├── MinimumIndexSumOfTwoLists.java │ │ │ │ ├── RandomizedSet.java │ │ │ │ ├── SingleNumber.java │ │ │ │ ├── TopKFrequentElements.java │ │ │ │ ├── TreeNode.java │ │ │ │ ├── TwoSum.java │ │ │ │ └── ValidSudoku.java │ │ │ │ ├── linkedlist │ │ │ │ ├── AddTwoNumbers.java │ │ │ │ ├── CopyListWithRandomPointer.java │ │ │ │ ├── DesignLinkedList.java │ │ │ │ ├── FlattenAMultilevelDoublyLinkedList.java │ │ │ │ ├── IntersectionOfTwoLinkedLists.java │ │ │ │ ├── LinkedListCycle.java │ │ │ │ ├── LinkedListCycleII.java │ │ │ │ ├── ListNode.java │ │ │ │ ├── MergeTwoSortedLists.java │ │ │ │ ├── Node.java │ │ │ │ ├── OddEvenLinkedList.java │ │ │ │ ├── PalindromeLinkedList.java │ │ │ │ ├── RemoveLinkedListElements.java │ │ │ │ ├── RemoveNthNodeFromEndOfList.java │ │ │ │ ├── ReverseLinkedList.java │ │ │ │ └── RotateList.java │ │ │ │ ├── queuestack │ │ │ │ ├── CloneGraph.java │ │ │ │ ├── DailyTemperatures.java │ │ │ │ ├── DecodeString.java │ │ │ │ ├── DesignCircularQueue.java │ │ │ │ ├── EvaluateReversePolishNotation.java │ │ │ │ ├── ImplementQueueUsingStacks.java │ │ │ │ ├── ImplementStackUsingQueues.java │ │ │ │ ├── MinStack.java │ │ │ │ ├── Node.java │ │ │ │ ├── NumberOfIslands.java │ │ │ │ ├── OpenTheLock.java │ │ │ │ ├── PerfectSquares.java │ │ │ │ ├── TargetSum.java │ │ │ │ └── ValidParentheses.java │ │ │ │ ├── recursion │ │ │ │ ├── ClimbingStairs.java │ │ │ │ ├── FibonacciNumber.java │ │ │ │ ├── KthSymbolInGrammar.java │ │ │ │ ├── ListNode.java │ │ │ │ ├── MaximumDepthOfBinaryTree.java │ │ │ │ ├── MergeTwoSortedLists.java │ │ │ │ ├── PascalsTriangle2.java │ │ │ │ ├── Pow.java │ │ │ │ ├── ReverseLinkedList.java │ │ │ │ ├── ReverseString.java │ │ │ │ ├── SearchInABinarySearchTree.java │ │ │ │ ├── SwapNodesInPairs.java │ │ │ │ ├── TreeNode.java │ │ │ │ └── UniqueBinarySearchTrees2.java │ │ │ │ ├── recursion2 │ │ │ │ ├── BinaryTreeInorderTraversal.java │ │ │ │ ├── BinaryTreeLevelOrderTraversal.java │ │ │ │ ├── Combinations.java │ │ │ │ ├── GenerateParentheses.java │ │ │ │ ├── LargestRectangleInHistogram.java │ │ │ │ ├── LetterCombinationsOfAPhoneNumber.java │ │ │ │ ├── NQueens2.java │ │ │ │ ├── Permutations.java │ │ │ │ ├── SameTree.java │ │ │ │ ├── SearchATwoDMatrix2.java │ │ │ │ ├── SortAnArray.java │ │ │ │ ├── SudokuSolver.java │ │ │ │ ├── TheSkylineProblem.java │ │ │ │ ├── TreeNode.java │ │ │ │ └── ValidateBinarySearchTree.java │ │ │ │ └── trie │ │ │ │ ├── AddAndSearchWordDataStructureDesign.java │ │ │ │ ├── ImplementTriePrefixTree.java │ │ │ │ ├── MapSumPairs.java │ │ │ │ ├── MaximumXorOfTwoNumbersInAnArray.java │ │ │ │ ├── ReplaceWords.java │ │ │ │ └── WordSearch2.java │ │ ├── poorguy.md │ │ ├── tag │ │ │ └── DFS.java │ │ └── util │ │ │ ├── AbstractLeetcodeRunner.java │ │ │ └── LeetcodeRunner.java │ │ └── wkk │ │ ├── TwoSum.java │ │ ├── everyday │ │ ├── aug │ │ │ ├── AddStrings.java │ │ │ ├── BalancedBinaryTree.java │ │ │ ├── BitwiseANDofNumbersRange.java │ │ │ ├── CloneGraph.java │ │ │ ├── ConvertSortedListToBinarySearchTree.java │ │ │ ├── CountBinarySubstrings.java │ │ │ ├── CourseSchedule.java │ │ │ ├── FlattenBinaryTreetoLinkedList.java │ │ │ ├── FloodFill.java │ │ │ ├── HouseRobberIII.java │ │ │ ├── IncreasingSubsequences.java │ │ │ ├── KeysAndRooms.java │ │ │ ├── LetterCombinationsOfAPhoneNumber.java │ │ │ ├── Minesweeper.java │ │ │ ├── MinimumDepthOfBinaryTree.java │ │ │ ├── MultiplyStrings.java │ │ │ ├── PalindromePairs.java │ │ │ ├── PalindromicSubstrings.java │ │ │ ├── ReconstructItinerary.java │ │ │ ├── RecoverBinarySearchTree.java │ │ │ ├── RemoveBoxes.java │ │ │ ├── RepeatedSubstringPattern.java │ │ │ ├── RestoreIPAddresses.java │ │ │ ├── ReverseWordsInAStringIII.java │ │ │ ├── RobotReturnToOrigin.java │ │ │ ├── SameTree.java │ │ │ ├── ShortestPalindrome.java │ │ │ ├── SmallestRangeCoveringElementsfromKLists.java │ │ │ ├── SurroundedRegions.java │ │ │ ├── TwentyFourGame.java │ │ │ └── ValidParentheses.java │ │ └── junly │ │ │ ├── IntegerBreak_343.java │ │ │ └── MagicIndexLCCI.java │ │ ├── structs │ │ ├── ListNode.java │ │ ├── Node.java │ │ └── TreeNode.java │ │ └── wkk.md │ └── test │ └── java │ └── io │ └── github │ ├── dreamylost │ └── practice │ │ └── test │ │ ├── Brackets_test.java │ │ ├── DifferentNumberOfBinaryBits_Java_test.java │ │ ├── Find_test.java │ │ ├── GetGcd_test.java │ │ ├── GetNumberOfK_test.java │ │ ├── IsThePowerOf2_test.java │ │ ├── NumberOf0_test.java │ │ ├── NumberOf1_test.java │ │ ├── ReOrderArray_test.java │ │ ├── RightShift_test.java │ │ └── StringDisplacement_test.java │ └── poorguy │ └── test │ └── BinarySearchTest.java ├── java-utils ├── build.gradle └── src │ └── main │ └── java │ └── io │ └── github │ └── dreamylost │ └── utils │ ├── ImageUtils.java │ ├── MD5Encrypt.java │ ├── PropertiesConfigUtils.java │ ├── StreamUtils.java │ ├── StringUtils.java │ ├── result │ ├── Result.java │ └── ResultStatus.java │ └── time │ └── TimeUtils.java ├── kotlin-leetcode ├── README.md ├── build.gradle └── src │ └── main │ └── kotlin │ └── io │ └── github │ └── dreamylost │ ├── HelloKotlin.kt │ ├── Leetcode_1026.kt │ ├── Leetcode_1030.kt │ ├── Leetcode_1104.kt │ ├── Leetcode_1110.kt │ ├── Leetcode_1122.kt │ ├── Leetcode_1123.kt │ ├── Leetcode_1130.kt │ ├── Leetcode_1161.kt │ ├── Leetcode_1261.kt │ ├── Leetcode_1305.kt │ ├── Leetcode_1315.kt │ ├── Leetcode_1325.kt │ ├── Leetcode_136.kt │ ├── Leetcode_1372.kt │ ├── Leetcode_1423.kt │ ├── Leetcode_1448.kt │ ├── Leetcode_145.kt │ ├── Leetcode_1457.kt │ ├── Leetcode_1466.kt │ ├── Leetcode_148.kt │ ├── Leetcode_1530.kt │ ├── Leetcode_1579.kt │ ├── Leetcode_1609.kt │ ├── Leetcode_164.kt │ ├── Leetcode_169.kt │ ├── Leetcode_406.kt │ ├── Leetcode_623.kt │ ├── Leetcode_652.kt │ ├── Leetcode_654.kt │ ├── Leetcode_655.kt │ ├── Leetcode_684.kt │ ├── Leetcode_701.kt │ ├── Leetcode_724.kt │ ├── Leetcode_778.kt │ ├── Leetcode_814.kt │ ├── Leetcode_863.kt │ ├── Leetcode_865.kt │ ├── Leetcode_889.kt │ ├── Leetcode_894.kt │ ├── Leetcode_919.kt │ ├── Leetcode_951.kt │ ├── Leetcode_958.kt │ ├── Leetcode_971.kt │ ├── Leetcode_976.kt │ ├── Leetcode_JZ_26.kt │ ├── Leetcode_JZ_28.kt │ ├── Leetcode_JZ_32_1.kt │ ├── Leetcode_JZ_32_3.kt │ ├── Leetcode_JZ_34.kt │ ├── Leetcode_JZ_7.kt │ ├── ListNode.kt │ ├── TreeNode.kt │ ├── TreeNodeData.kt │ ├── UnionFind.kt │ └── dreamylost.md ├── kotlin-study ├── README.md ├── build.gradle └── src │ └── main │ └── kotlin │ └── io │ └── github │ └── dreamylost │ └── syntax │ ├── DefaultValue.kt │ ├── HelloKotlin.kt │ ├── basic │ └── Demo1.kt │ └── idiom │ └── Demo1.kt ├── package-lock.json ├── package.json ├── python-leetcode ├── README.md └── laozhang │ ├── __init__.py │ ├── laozhang.md │ └── tree │ ├── leetcode_1008_.py │ ├── leetcode_100_.py │ ├── leetcode_101_.py │ ├── leetcode_1022_.py │ ├── leetcode_103_.py │ ├── leetcode_105_.py │ ├── leetcode_106_.py │ ├── leetcode_107_.py │ ├── leetcode_110_.py │ ├── leetcode_112_.py │ ├── leetcode_113_.py │ ├── leetcode_114_.py │ ├── leetcode_116_.py │ ├── leetcode_117_.py │ ├── leetcode_129_.py │ ├── leetcode_1302_.py │ ├── leetcode_1305_.py │ ├── leetcode_1315_.py │ ├── leetcode_1325_.py │ ├── leetcode_1379_.py │ ├── leetcode_144_.py │ ├── leetcode_173_.py │ ├── leetcode_199_.py │ ├── leetcode_222_.py │ ├── leetcode_226_.py │ ├── leetcode_230_.py │ ├── leetcode_235_.py │ ├── leetcode_257_.py │ ├── leetcode_27_.py │ ├── leetcode_32_.py │ ├── leetcode_404_.py │ ├── leetcode_429_.py │ ├── leetcode_437_.py │ ├── leetcode_501_.py │ ├── leetcode_513_.py │ ├── leetcode_515_.py │ ├── leetcode_538_.py │ ├── leetcode_543_.py │ ├── leetcode_559_.py │ ├── leetcode_55_.py │ ├── leetcode_563_.py │ ├── leetcode_572_.py │ ├── leetcode_590_.py │ ├── leetcode_637_.py │ ├── leetcode_653_.py │ ├── leetcode_654_.py │ ├── leetcode_671_.py │ ├── leetcode_687_.py │ ├── leetcode_700_.py │ ├── leetcode_701_.py │ ├── leetcode_783_.py │ ├── leetcode_814_.py │ ├── leetcode_872_.py │ ├── leetcode_897_.py │ ├── leetcode_938_.py │ ├── leetcode_94_.py │ ├── leetcode_965_.py │ ├── leetcode_98_.py │ └── leetcode_993_.py ├── ruby-examples ├── README.md └── src │ └── Function.rb ├── rust-datastructure ├── Cargo.lock ├── Cargo.toml ├── datastructure.md └── src │ ├── bad_safe_singly_deque.rs │ ├── bad_singly_stack.rs │ ├── main.rs │ ├── ok_singly_stack.rs │ ├── persistent_singly_stack.rs │ ├── priority_queue.rs │ └── unsafe_singly_queue.rs ├── rust-examples ├── Cargo.lock ├── Cargo.toml ├── README.md ├── a │ ├── b.txt │ └── c │ │ └── b.txt ├── files │ └── starwars.rs ├── hello.txt ├── lorem_ipsum.txt ├── schemas │ ├── book.graphql │ └── starwars.schema.graphql ├── src │ ├── closures.rs │ ├── conversion.rs │ ├── custom_types.rs │ ├── flow_control.rs │ ├── formatted_print.rs │ ├── functions.rs │ ├── generics.rs │ ├── main.rs │ ├── misc.rs │ ├── modules.rs │ ├── primitives.rs │ ├── snapshots │ │ └── rust_examples__test_cynic__snapshot_query_dsl.snap │ ├── test_cynic.rs │ ├── test_cynic_starwars.rs │ ├── traits.rs │ ├── types.rs │ └── variable_bindings.rs └── world.txt ├── rust-exercise ├── Cargo.lock ├── Cargo.toml ├── README.md ├── hello.txt └── src │ ├── closures_syntax.rs │ ├── collection_function.rs │ ├── control_function.rs │ ├── enum_data_type.rs │ ├── example_guessing_game.rs │ ├── front_of_house.rs │ ├── front_of_house │ └── hosting.rs │ ├── generic_traits_lifetimes.rs │ ├── iterator_demonstration.rs │ ├── lib.rs │ ├── macro_syntax.rs │ ├── main.rs │ ├── match_syntax.rs │ ├── method_syntax.rs │ ├── other_function.rs │ ├── panic_function.rs │ ├── point_function.rs │ ├── simple_array_data_type.rs │ ├── smart_point.rs │ ├── struct_data_type.rs │ ├── thread_syntax.rs │ └── variables_function.rs ├── rust-http ├── 404.html ├── Cargo.lock ├── Cargo.toml ├── hello.html └── src │ ├── lib.rs │ └── main.rs ├── rust-leetcode ├── Cargo.lock ├── Cargo.toml ├── README.md └── src │ ├── interview_01_01.rs │ ├── interview_02_02.rs │ ├── interview_03_04.rs │ ├── interview_04_02.rs │ ├── interview_04_03.rs │ ├── interview_04_10.rs │ ├── interview_04_12.rs │ ├── interview_05.rs │ ├── interview_06.rs │ ├── interview_08_01.rs │ ├── interview_09.rs │ ├── interview_10_01.rs │ ├── interview_16_07.rs │ ├── interview_17.rs │ ├── interview_22.rs │ ├── interview_24.rs │ ├── interview_25.rs │ ├── interview_27.rs │ ├── interview_32_2.rs │ ├── interview_54.rs │ ├── interview_55_1.rs │ ├── interview_58_2.rs │ ├── leetcode_100.rs │ ├── leetcode_101.rs │ ├── leetcode_1021.rs │ ├── leetcode_1051.rs │ ├── leetcode_107.rs │ ├── leetcode_108.rs │ ├── leetcode_110.rs │ ├── leetcode_111.rs │ ├── leetcode_112.rs │ ├── leetcode_1145.rs │ ├── leetcode_1160.rs │ ├── leetcode_118.rs │ ├── leetcode_1180.rs │ ├── leetcode_1207.rs │ ├── leetcode_1221.rs │ ├── leetcode_1252.rs │ ├── leetcode_1281.rs │ ├── leetcode_13.rs │ ├── leetcode_1304.rs │ ├── leetcode_1313.rs │ ├── leetcode_1323.rs │ ├── leetcode_1342.rs │ ├── leetcode_1351.rs │ ├── leetcode_1365.rs │ ├── leetcode_1367.rs │ ├── leetcode_1370.rs │ ├── leetcode_1374.rs │ ├── leetcode_1380.rs │ ├── leetcode_1385.rs │ ├── leetcode_14.rs │ ├── leetcode_1403.rs │ ├── leetcode_1413.rs │ ├── leetcode_1443.rs │ ├── leetcode_20.rs │ ├── leetcode_204.rs │ ├── leetcode_257.rs │ ├── leetcode_292.rs │ ├── leetcode_321.rs │ ├── leetcode_35.rs │ ├── leetcode_38.rs │ ├── leetcode_448.rs │ ├── leetcode_461.rs │ ├── leetcode_475.rs │ ├── leetcode_500.rs │ ├── leetcode_513.rs │ ├── leetcode_515.rs │ ├── leetcode_532.rs │ ├── leetcode_557.rs │ ├── leetcode_561.rs │ ├── leetcode_58.rs │ ├── leetcode_581.rs │ ├── leetcode_605.rs │ ├── leetcode_617.rs │ ├── leetcode_633.rs │ ├── leetcode_66.rs │ ├── leetcode_665.rs │ ├── leetcode_67.rs │ ├── leetcode_686.rs │ ├── leetcode_687.rs │ ├── leetcode_703.rs │ ├── leetcode_709.rs │ ├── leetcode_728.rs │ ├── leetcode_738.rs │ ├── leetcode_804.rs │ ├── leetcode_83.rs │ ├── leetcode_832.rs │ ├── leetcode_840.rs │ ├── leetcode_859.rs │ ├── leetcode_860.rs │ ├── leetcode_876.rs │ ├── leetcode_9.rs │ ├── leetcode_905.rs │ ├── leetcode_933.rs │ ├── leetcode_938.rs │ ├── leetcode_942.rs │ ├── leetcode_944.rs │ ├── leetcode_977.rs │ ├── leetcode_999.rs │ ├── main.rs │ ├── old_solutions.rs │ └── pre_structs.rs ├── rust-minigrep ├── Cargo.lock ├── Cargo.toml ├── output.txt ├── poem.txt └── src │ ├── lib.rs │ └── main.rs ├── rust-oop ├── Cargo.lock ├── Cargo.toml └── src │ ├── lib.rs │ ├── main.rs │ ├── oop_blog.rs │ └── rust_blog.rs ├── scala-examples ├── README.md ├── build.gradle └── src │ ├── main │ └── scala │ │ └── io │ │ └── github │ │ └── dreamylost │ │ ├── ConstructionExtendsBasic.scala │ │ ├── FunctionBasic.scala │ │ ├── HashEquals.scala │ │ ├── IOBasic.scala │ │ ├── LoopBasic.scala │ │ ├── MainObject1.scala │ │ ├── MainObject2.scala │ │ ├── ObjectBasic.scala │ │ ├── OutPrivateClass.scala │ │ ├── ParameterAnnotation.scala │ │ ├── PrintMultiTable.scala │ │ ├── ScalaRDD.scala │ │ ├── Singleton.scala │ │ ├── SkipList.scala │ │ ├── Test1.scala │ │ ├── Test2.scala │ │ ├── Test3.scala │ │ ├── Test4.scala │ │ ├── Test5.scala │ │ ├── Test6.scala │ │ ├── Test7.scala │ │ ├── Test8.scala │ │ ├── TestArrayApply.scala │ │ ├── TimePosTest.scala │ │ ├── cakepattern │ │ ├── AppSpec.scala │ │ └── EngineComponent.scala │ │ ├── examples │ │ ├── FuturePromiseTest.scala │ │ ├── ImplicitSeqObjectTest.scala │ │ ├── ImplicitTest.scala │ │ ├── JavaCollectionTest.scala │ │ ├── MapExamples.scala │ │ ├── OptionEqualsTest.scala │ │ ├── OptionTest.scala │ │ ├── SymbolTest.scala │ │ ├── TestFold.scala │ │ ├── fb │ │ │ ├── README.md │ │ │ ├── datastructures.scala │ │ │ ├── errorhandling.scala │ │ │ ├── gettingstarted.scala │ │ │ ├── laziness.scala │ │ │ └── package.scala │ │ ├── reflect │ │ │ ├── JavaReflectDemoTest.scala │ │ │ ├── ReflectDemoTestClass.scala │ │ │ └── ScalaReflectDemoTest.scala │ │ └── utils │ │ │ └── ScalaUtil.md │ │ └── macro │ │ ├── CodeGenerator.scala │ │ ├── Fundeps.scala │ │ ├── ImplicitMacros.scala │ │ ├── Macros.scala │ │ └── Macros2.scala │ └── test │ └── scala │ └── io │ └── github │ └── dreamylost │ └── macrospec │ └── CodeGeneratorSpec.scala ├── scala-icon.png ├── scala-leetcode ├── README.md ├── build.gradle └── src │ └── main │ └── scala │ └── io │ └── github │ ├── dreamylost │ ├── BTT.scala │ ├── CoinChange.scala │ ├── LeetCode_590.scala │ ├── Leetcdoe_198.scala │ ├── Leetcode_1008.scala │ ├── Leetcode_101_Tree.scala │ ├── Leetcode_102.scala │ ├── Leetcode_1022.scala │ ├── Leetcode_103.scala │ ├── Leetcode_104_Tree.scala │ ├── Leetcode_105.scala │ ├── Leetcode_106.scala │ ├── Leetcode_1108.scala │ ├── Leetcode_110_Tree.scala │ ├── Leetcode_111_Tree.scala │ ├── Leetcode_1128.scala │ ├── Leetcode_112_Tree.scala │ ├── Leetcode_113.scala │ ├── Leetcode_114.scala │ ├── Leetcode_116.scala │ ├── Leetcode_117.scala │ ├── Leetcode_119.scala │ ├── Leetcode_121.scala │ ├── Leetcode_1266.scala │ ├── Leetcode_1281.scala │ ├── Leetcode_129.scala │ ├── Leetcode_1295.scala │ ├── Leetcode_1299.scala │ ├── Leetcode_1302.scala │ ├── Leetcode_1339.scala │ ├── Leetcode_1351.scala │ ├── Leetcode_144.scala │ ├── Leetcode_155.scala │ ├── Leetcode_160.scala │ ├── Leetcode_17.scala │ ├── Leetcode_173.scala │ ├── Leetcode_199.scala │ ├── Leetcode_1_Array.scala │ ├── Leetcode_200.scala │ ├── Leetcode_206_ListNode.scala │ ├── Leetcode_21_ListNode.scala │ ├── Leetcode_22.scala │ ├── Leetcode_222.scala │ ├── Leetcode_226_Tree.scala │ ├── Leetcode_230.scala │ ├── Leetcode_235.scala │ ├── Leetcode_236.scala │ ├── Leetcode_240_Array.scala │ ├── Leetcode_287_Array.scala │ ├── Leetcode_2_ListNode.scala │ ├── Leetcode_3.scala │ ├── Leetcode_303.scala │ ├── Leetcode_337_Tree.scala │ ├── Leetcode_338.scala │ ├── Leetcode_347.scala │ ├── Leetcode_378_Array.scala │ ├── Leetcode_39.scala │ ├── Leetcode_404_Tree.scala │ ├── Leetcode_429.scala │ ├── Leetcode_437_Tree.scala │ ├── Leetcode_449.scala │ ├── Leetcode_450.scala │ ├── Leetcode_46.scala │ ├── Leetcode_501.scala │ ├── Leetcode_508.scala │ ├── Leetcode_53.scala │ ├── Leetcode_530.scala │ ├── Leetcode_538.scala │ ├── Leetcode_543_Tree.scala │ ├── Leetcode_559.scala │ ├── Leetcode_563.scala │ ├── Leetcode_565_Array.scala │ ├── Leetcode_572_Tree.scala │ ├── Leetcode_589.scala │ ├── Leetcode_606.scala │ ├── Leetcode_617_Tree.scala │ ├── Leetcode_637_Tree.scala │ ├── Leetcode_64.scala │ ├── Leetcode_647.scala │ ├── Leetcode_653.scala │ ├── Leetcode_657.scala │ ├── Leetcode_662.scala │ ├── Leetcode_667_Array.scala │ ├── Leetcode_669.scala │ ├── Leetcode_671_Tree.scala │ ├── Leetcode_697_Array.scala │ ├── Leetcode_7.scala │ ├── Leetcode_70.scala │ ├── Leetcode_700.scala │ ├── Leetcode_739.scala │ ├── Leetcode_746.scala │ ├── Leetcode_766_Array.scala │ ├── Leetcode_769_Array.scala │ ├── Leetcode_771.scala │ ├── Leetcode_78.scala │ ├── Leetcode_783.scala │ ├── Leetcode_79.scala │ ├── Leetcode_80_Array.scala │ ├── Leetcode_83_ListNode.scala │ ├── Leetcode_872.scala │ ├── Leetcode_897.scala │ ├── Leetcode_94.scala │ ├── Leetcode_95.scala │ ├── Leetcode_96.scala │ ├── Leetcode_965.scala │ ├── Leetcode_979.scala │ ├── Leetcode_98.scala │ ├── Leetcode_987.scala │ ├── Leetcode_988.scala │ ├── Leetcode_993.scala │ ├── Leetcode_998.scala │ ├── Leetcode_Interview_0404.scala │ ├── Leetcode_Interview_0405.scala │ ├── Leetcode_Interview_17_12.scala │ ├── Leetcode_Interview_27.scala │ ├── Leetcode_JZ_28.scala │ ├── Leetcode_JZ_55_2.scala │ ├── Leetcode_LCP_1.scala │ ├── ListNode.java │ ├── ListNodeConstants.java │ ├── Node.scala │ ├── PrintlnSupport.scala │ ├── SecondLargeNumbers.scala │ ├── TreeNode.java │ ├── TreeNodeData.scala │ └── dreamylost.md │ └── sweeneycai │ ├── BinaryTreeUtils.scala │ ├── Didi_interview_1.scala │ ├── Lcof_41.scala │ ├── Leetcode_11.scala │ ├── Leetcode_130.scala │ ├── Leetcode_146.scala │ ├── Leetcode_15.scala │ ├── Leetcode_1502.scala │ ├── Leetcode_17.scala │ ├── Leetcode_19.scala │ ├── Leetcode_198.scala │ ├── Leetcode_201.scala │ ├── Leetcode_22.scala │ ├── Leetcode_31.scala │ ├── Leetcode_32.scala │ ├── Leetcode_322.scala │ ├── Leetcode_332.scala │ ├── Leetcode_34.scala │ ├── Leetcode_365.scala │ ├── Leetcode_39.scala │ ├── Leetcode_415.scala │ ├── Leetcode_42.scala │ ├── Leetcode_43.scala │ ├── Leetcode_448.scala │ ├── Leetcode_459.scala │ ├── Leetcode_49.scala │ ├── Leetcode_491.scala │ ├── Leetcode_5.scala │ ├── Leetcode_538.scala │ ├── Leetcode_55.scala │ ├── Leetcode_62.scala │ ├── Leetcode_64.scala │ ├── Leetcode_647.scala │ ├── Leetcode_696.scala │ ├── Leetcode_707.scala │ ├── Leetcode_72.scala │ ├── Leetcode_78.scala │ ├── Leetcode_841.scala │ ├── Leetcode_93.scala │ ├── Leetcode_96.scala │ ├── Leetcode_98.scala │ ├── Leetcode_99.scala │ ├── QuickSort.scala │ └── sweeneycai.md ├── scala-utils ├── README.md ├── build.gradle └── src │ └── main │ └── scala │ └── io │ └── github │ └── dreamylost │ ├── cache │ └── LRUCache.scala │ ├── concurrent │ ├── Executable.scala │ ├── Executions.scala │ ├── RateLimiter.scala │ └── TokenBucket.scala │ ├── config │ └── ConfigConversions.scala │ ├── doobie │ └── DoobieMeta.scala │ ├── guice │ └── AbstractModuleSupport.scala │ ├── http │ ├── Routes.scala │ └── RoutingHandler.scala │ ├── json │ ├── CaseClassDeserializer.scala │ ├── JacksonScalaSupport.scala │ └── JacksonScalaSupportSpec.scala │ ├── other │ ├── AtomicLogic.scala │ ├── ByteUtils.java │ ├── CollectionUtils.scala │ ├── ConditionUtils.scala │ ├── CustomConversions.scala │ ├── DateUtils.scala │ ├── EncrypUtils.scala │ ├── EnumUtils.scala │ ├── FileUtils.scala │ ├── JdkDynamicProxyUtils.scala │ ├── JsonUtils.scala │ ├── LiftedUtil.scala │ ├── ObjectUtils.scala │ ├── SignUtilities.scala │ └── StringUtils.scala │ ├── play │ ├── CaseClassJsonSupport.scala │ ├── CommonResult.scala │ ├── ImplicitUtils.scala │ ├── PerformanceRecord.scala │ └── TimeoutAction.scala │ ├── scalikejdbc │ ├── RepositoryHelper.scala │ └── RepositorySupport.scala │ └── undertow │ ├── AccessLogHandler.scala │ ├── ExceptionHandler.scala │ └── RestfulHandler.scala ├── scala3-examples ├── README.md ├── build.sbt ├── project │ ├── build.properties │ └── plugins.sbt └── src │ ├── main │ └── scala │ │ ├── Main.scala │ │ └── io │ │ └── github │ │ └── dreamylost │ │ └── scala3 │ │ ├── ContextFunctions.scala │ │ ├── Conversion.scala │ │ ├── EnumTypes.scala │ │ ├── GivenInstances.scala │ │ ├── IntersectionTypes.scala │ │ ├── Main.scala │ │ ├── MultiversalEquality.scala │ │ ├── ParameterUntupling.scala │ │ ├── PatternMatching.scala │ │ ├── StructuralTypes.scala │ │ ├── TraitParams.scala │ │ ├── TypeLambdas.scala │ │ └── UnionTypes.scala │ └── test │ └── scala │ └── Test1.scala └── settings.gradle /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: "gradle" 9 | directory: "/" 10 | schedule: 11 | interval: "daily" 12 | - package-ecosystem: "cargo" 13 | directory: "/" 14 | schedule: 15 | interval: "daily" 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | bin/ 2 | target/ 3 | .settings/ 4 | .classpath 5 | .project 6 | .cache-main 7 | out/ 8 | .idea/ 9 | .gradle/ 10 | build/ 11 | *.iml 12 | gradlew 13 | gradlew.bat 14 | tmp/ 15 | *.log 16 | node_modules/ 17 | *.lock 18 | *.bak 19 | sbt.json 20 | -------------------------------------------------------------------------------- /.scalafmt.conf: -------------------------------------------------------------------------------- 1 | # For idea format 2 | version = 2.7.5 3 | align.preset = more 4 | docstrings.style = Asterisk 5 | maxColumn = 100 6 | continuationIndent.defnSite = 4 7 | continuationIndent.callSite = 2 8 | continuationIndent.ctorSite = 4 9 | align.preset = none 10 | align.openParenCallSite = false 11 | align.openParenDefnSite = false 12 | rewrite.rules = [ExpandImportSelectors] 13 | spaces.inImportCurlyBraces = true 14 | rewriteTokens = { 15 | "⇒": "=>" 16 | "→": "->" 17 | "←": "<-" 18 | } -------------------------------------------------------------------------------- /.sonarcloud.properties: -------------------------------------------------------------------------------- 1 | # Path to sources 2 | #sonar.sources=. 3 | sonar.exclusions=**/*.html, **/*.css, **/*.xml 4 | #sonar.inclusions= -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: scala 2 | scala: 3 | - 2.12.2 4 | after_success: 5 | - bash <(curl -s https://codecov.io/bash) 6 | jdk: 7 | - openjdk8 -------------------------------------------------------------------------------- /TechnologyStack-2020-11-16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/TechnologyStack-2020-11-16.png -------------------------------------------------------------------------------- /buildSrc/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'groovy-gradle-plugin' //In order for precompiled script plugins to be discovered 3 | } 4 | //// add dependencies for custom.*.gradle 5 | dependencies { 6 | implementation('org.jetbrains.kotlin.jvm:org.jetbrains.kotlin.jvm.gradle.plugin:1.4.31') 7 | } -------------------------------------------------------------------------------- /buildSrc/settings.gradle: -------------------------------------------------------------------------------- 1 | dependencyResolutionManagement { 2 | repositories { 3 | mavenLocal() 4 | maven { 5 | name "aliyunmaven" 6 | url "https://maven.aliyun.com/nexus/content/groups/public/" 7 | } 8 | maven { 9 | name "spring" 10 | url "https://repo.spring.io/libs-milestone/" 11 | } 12 | mavenCentral() 13 | gradlePluginPortal() 14 | google() 15 | } 16 | } 17 | rootProject.name = 'buildSrc' 18 | -------------------------------------------------------------------------------- /buildSrc/src/main/groovy/custom.java.gradle: -------------------------------------------------------------------------------- 1 | spotless { 2 | java { 3 | removeUnusedImports() 4 | googleJavaFormat('1.6').aosp() 5 | licenseHeader '/* All Contributors (C) $YEAR */' 6 | } 7 | } 8 | 9 | dependencies { 10 | implementation group: 'junit', name: 'junit', version: '4.12' 11 | implementation group: 'org.hamcrest', name: 'hamcrest-core', version: '1.3' 12 | } 13 | -------------------------------------------------------------------------------- /buildSrc/src/main/groovy/custom.kotlin.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'org.jetbrains.kotlin.jvm' 3 | } 4 | 5 | dependencies { 6 | implementation('org.jetbrains.kotlin:kotlin-stdlib') 7 | } 8 | spotless { 9 | kotlin { 10 | ktlint('0.37.2').userData(['indent_size': '4', 'continuation_indent_size': '2', 'disabled_rules': 'no-wildcard-imports']) 11 | licenseHeader '/* Licensed under Apache-2.0 @梦境迷离 */' 12 | } 13 | } -------------------------------------------------------------------------------- /buildSrc/src/main/groovy/custom.scala.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'scala' 3 | } 4 | 5 | sourceSets { 6 | main.scala.srcDirs = ['src/main/scala', 'src/main/java'] 7 | //测试的执行可能有问题,可以用IDEA rebuild单独的子模块,再执行脚本build 8 | test { 9 | scala.srcDirs = ['src/test/java', 'src/test/scala'] 10 | } 11 | } 12 | 13 | dependencies { 14 | implementation group: 'org.scala-lang', name: 'scala-library', version: '2.13.5' 15 | implementation group: 'org.scala-lang', name: 'scala-reflect', version: '2.13.5' 16 | } 17 | 18 | [compileScala, compileTestScala]*.options*.encoding = 'UTF-8' 19 | 20 | spotless { 21 | scala { 22 | scalafmt('2.7.5').configFile("${rootProject.rootDir}/.scalafmt.conf") 23 | licenseHeader '/* Licensed under Apache-2.0 (C) All Contributors */', 'package ' 24 | } 25 | } -------------------------------------------------------------------------------- /countByAuthors.md: -------------------------------------------------------------------------------- 1 | #### Order by Join time 2 | #### 1.Scala 3 | dreamylost:=> 114 4 | 5 | sweeneycai:=> 39 6 | 7 | #### 2.Java (match *.Java) 8 | dreamylost:=> 325 9 | 10 | ccccmaster:=> 3 11 | 12 | poorguy:=> 145 13 | 14 | wkk:=> 37 15 | 16 | #### 3.Rust 17 | dreamylost:=> 100 18 | 19 | #### 4.Kotlin 20 | dreamylost:=> 50 21 | 22 | #### 5.Python 23 | laozhang:=> 59 24 | 25 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Fri Jan 17 14:58:17 CST 2020 2 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-all.zip 3 | distributionBase=GRADLE_USER_HOME 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /java-examples/README.md: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | 早期学习时写的examples code -------------------------------------------------------------------------------- /java-examples/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'custom.java' 3 | } 4 | 5 | dependencies { 6 | implementation 'org.apache.commons:commons-csv:1.9.0' 7 | implementation 'org.javatuples:javatuples:1.2' 8 | implementation 'redis.clients:jedis:3.6.3' 9 | implementation 'commons-codec:commons-codec:1.15' 10 | implementation 'org.ow2.asm:asm-all:6.0_BETA' 11 | implementation 'com.alibaba:fastjson:1.2.78' 12 | implementation 'io.reactivex.rxjava2:rxjava:2.2.21' 13 | } 14 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/concurrent/MyThread.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.concurrent; 3 | 4 | /** 继承 Thread 类创建线程对象 */ 5 | public class MyThread extends Thread { 6 | 7 | @Override // 可以省略 8 | public void run() { 9 | System.out.println("MyThread 的线程对象正在执行任务"); 10 | } 11 | 12 | public static void main(String[] args) { 13 | for (int i = 0; i < 10; i++) { 14 | MyThread thread = new MyThread(); 15 | thread.start(); 16 | 17 | System.out.println("MyThread 的线程对象 " + thread.getId()); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/concurrent/MyThreadBrother.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.concurrent; 3 | 4 | /** 实现 Runnable 接口类创建线程对象 */ 5 | public class MyThreadBrother implements Runnable { 6 | 7 | @Override // 可以省略 8 | public void run() { 9 | System.out.println("MyThreadBrother 的线程对象正在执行任务"); 10 | } 11 | 12 | public static void main(String[] args) { 13 | for (int i = 0; i < 10; i++) { 14 | Thread thread = new Thread(new MyThreadBrother()); 15 | thread.start(); 16 | 17 | System.out.println("MyThreadBrother 的线程对象 " + thread.getId()); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/concurrent/package-info.java: -------------------------------------------------------------------------------- 1 | package cn.edu.jxnu.examples.concurrent; 2 | 3 | /** 4 | * /* Copyright [2018] [Jeff Lee @ bysocket.com] 5 | * 6 | *

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 7 | * except in compliance with the License. You may obtain a copy of the License at 8 | * 9 | *

http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | *

Unless required by applicable law or agreed to in writing, software distributed under the 12 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 13 | * express or implied. See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | *

部分源码版权 17 | */ 18 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/AbstractFactoryMethod.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design; 3 | 4 | /** 5 | * Copyright © 2018 梦境迷离. All rights reserved. 6 | * 7 | * @description:抽象工厂:为创建一组相关或相互依赖的对象提供一个接口,而且无须指定它们的具体类 @Package: cn.jxnu.edu.designpattern 8 | * @author: 梦境迷离 9 | * @date: 2018年3月20日 上午9:54:19 10 | */ 11 | /** 使用场景: 一个对象族(或是一组没有任何关系的对象)都有相同的约束。 涉及不同操作系统的时候,都可以考虑使用抽象工厂模式 */ 12 | public abstract class AbstractFactoryMethod { 13 | // 创建A产品家族 14 | public abstract AbstractProductA createProductA(); 15 | 16 | // 创建B产品家族 17 | public abstract AbstractProductB createProductB(); 18 | } 19 | 20 | class AbstractProductA {} 21 | 22 | class AbstractProductB {} 23 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/Facade.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/Facade.jpg -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/adapter/Adaptee.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.adapter; 3 | 4 | public class Adaptee { 5 | public void specificApiForCurrentSystem() { 6 | System.out.println("源角色:需要被适配的类,具有特殊功能。。。"); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/adapter/Adapter.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.adapter; 3 | 4 | public class Adapter extends Adaptee implements TargetInterface { 5 | 6 | @Override 7 | public void standardApiForCurrentSystem() { 8 | super.specificApiForCurrentSystem(); // 使用源类的特殊功能,加以包装 9 | } 10 | //// 适配器类,继承了被适配类,同时实现标准接口 11 | 12 | } 13 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/adapter/DemoTest.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.adapter; 3 | 4 | public class DemoTest { 5 | /** 当前client 可以通过TargetInterface使用Adaptee (Adapter实现了TargetInterface ,并在自己内部使用了特殊方法) */ 6 | public static void main(String[] args) { 7 | TargetInterface adapter = new Adapter(); 8 | adapter.standardApiForCurrentSystem(); // 使用标准api 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/adapter/TargetInterface.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.adapter; 3 | 4 | public interface TargetInterface { 5 | public void standardApiForCurrentSystem(); // 为当前系统提供标准的api 6 | } 7 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/adapter/object/Adapter.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.adapter.object; 3 | 4 | import cn.edu.jxnu.examples.design.adapter.Adaptee; 5 | 6 | // 不再使用继承,使用关联关系,避免多继承 7 | public class Adapter implements TargetInterface { 8 | Adaptee adaptee; // 直接关联被适配类 9 | 10 | public Adapter(Adaptee adaptee) { // / 可以通过构造函数传入具体需要适配的被适配类对象 11 | this.adaptee = adaptee; 12 | } 13 | 14 | public void standardApiForCurrentSystem() { // 这里是使用委托的方式完成特殊功能 15 | this.adaptee.specificApiForCurrentSystem(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/adapter/object/DemoTest.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.adapter.object; 3 | 4 | import cn.edu.jxnu.examples.design.adapter.Adaptee; 5 | 6 | public class DemoTest { 7 | /** 当前client 可以通过TargetInterface使用Adaptee (Adapter实现了TargetInterface ,并在自己内部使用了特殊方法) */ 8 | public static void main(String[] args) { 9 | TargetInterface adapter = new Adapter(new Adaptee()); 10 | adapter.standardApiForCurrentSystem(); // 使用标准api 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/adapter/object/TargetInterface.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.adapter.object; 3 | 4 | public interface TargetInterface { 5 | public void standardApiForCurrentSystem(); // 为当前系统提供标准的api 6 | } 7 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/adapter/适配器.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/adapter/适配器.jpg -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/decorator/Component.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.decorator; 3 | 4 | public interface Component { 5 | public void methodA(); // 需要被装饰的组件的抽象接口 6 | } 7 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/decorator/ConcreteComponent.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.decorator; 3 | 4 | public class ConcreteComponent implements Component { 5 | 6 | @Override 7 | public void methodA() { 8 | System.out.println("被装饰类,实现了抽象组件的功能"); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/decorator/Decorator.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.decorator; 3 | 4 | public abstract class Decorator implements Component { 5 | private Component instance; // 持有Component的引用,通过构造器注入 6 | 7 | public Decorator() {} 8 | 9 | public Decorator(Component inst) { 10 | this.instance = inst; 11 | } 12 | 13 | public Component getInstance() { 14 | return this.instance; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/decorator/TestDecorator.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.decorator; 3 | 4 | public class TestDecorator { 5 | 6 | public static void main(String[] args) { 7 | Component inst = new ConcreteComponent(); // 实例化一个具体组件 8 | inst.methodA(); // 使用原本的功能 9 | System.out.println("装饰后的功能。。。。。"); 10 | // 仍是Component,所以接口没有改变 11 | Component instDecorator = new ConcreteDecorator(inst); // 经过装饰之后的功能 12 | instDecorator.methodA(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/decorator/装饰器.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/decorator/装饰器.jpeg -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/observer/Concrete2Observer.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.observer; 3 | 4 | // 具体的观察者2 5 | class Concrete2Observer extends Observer { 6 | 7 | public Concrete2Observer(Subject subject) { 8 | this.subject = subject; 9 | this.subject.addObserver(this); 10 | } 11 | 12 | @Override 13 | public void update() { 14 | System.out.println("Concrete2Observer println world"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/observer/ConcreteObserver.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.observer; 3 | 4 | // 具体的观察者 5 | class ConcreteObserver extends Observer { 6 | 7 | public ConcreteObserver(Subject subject) { 8 | this.subject = subject; 9 | this.subject.addObserver(this); 10 | } 11 | 12 | @Override 13 | public void update() { 14 | System.out.println("ConcreteObserver println hello"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/observer/ConcreteSubject.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package cn.edu.jxnu.examples.design.observer; 3 | 4 | /** 5 | * @author 梦境迷离 6 | * @version 1.0 7 | * @since 2021/3/14 8 | */ 9 | public class ConcreteSubject extends Subject {} 10 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/observer/ObserverPatternSpec.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.observer; 3 | 4 | public class ObserverPatternSpec { 5 | 6 | public static void main(String[] args) { 7 | Subject subject = new ConcreteSubject(); 8 | 9 | new ConcreteObserver(subject); 10 | new Concrete2Observer(subject); 11 | 12 | // 实际应该是subject的状态变更时再通知观察者们,这里省略了状态变更步骤 13 | subject.notifyObservers(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/observer/Subject.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.observer; 3 | 4 | import java.util.Vector; 5 | 6 | // 被观察者 7 | abstract class Subject { 8 | // 定义一个观察者数组(顺序 重复性?) 9 | private Vector obsVector = new Vector(); 10 | 11 | // 增加一个观察者 12 | public void addObserver(Observer o) { 13 | this.obsVector.add(o); 14 | } 15 | 16 | // 删除一个观察者 17 | public void delObserver(Observer o) { 18 | this.obsVector.remove(o); 19 | } 20 | 21 | // 通知所有观察者 22 | public void notifyObservers() { 23 | for (Observer o : this.obsVector) { 24 | o.update(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/proxy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/proxy.jpg -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/strategy/Context.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.strategy; 3 | 4 | import java.math.BigDecimal; 5 | 6 | /** 策略环境 */ 7 | public class Context { 8 | 9 | // 持有一个具体策略的对象 10 | private Strategy strategy; 11 | 12 | /** 构造函数,传入一个具体策略对象 3 传入 具体策略对象 */ 13 | public Context(Strategy strategy) { 14 | this.strategy = strategy; 15 | } 16 | 17 | /** 策略方法 */ 18 | public void contextInterface() { 19 | 20 | // 多态,调用实现了该接口的类的该方法 21 | // 传入原价 22 | strategy.strategyInterface(new BigDecimal(55), false); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/strategy/Main.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/strategy/Main.jpg -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/strategy/Strategy.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.strategy; 3 | 4 | import java.math.BigDecimal; 5 | 6 | /** 7 | * 通用逻辑接口 8 | * 9 | *

有必要时,可以对接口进行抽象进一步分层实现 10 | * 11 | * @author 梦境迷离. 12 | * @time 2018年6月13日 13 | * @version v1.0 14 | */ 15 | public interface Strategy { 16 | 17 | /** 定义买票流程 策略方法 价格使用BigDecimal定义 此方法在子类中默认实现为原价购票 */ 18 | public BigDecimal strategyInterface(BigDecimal price); 19 | 20 | /** 21 | * 是否符合条件,默认使用该方法,即需要提供验证 22 | * 23 | *

isCan 是否需要在本接口中判断来的人是否需要购票要求验证 24 | */ 25 | public BigDecimal strategyInterface(BigDecimal price, boolean isCan); 26 | } 27 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/strategy/package-info.java: -------------------------------------------------------------------------------- 1 | package cn.edu.jxnu.examples.design.strategy; 2 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/strategy/策略.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/strategy/策略.jpg -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/visitor/ConcreteElement.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.visitor; 3 | 4 | /** 具体的元素类,它提供接受访问方法的具体实现,而这个具体的实现,通常情况下是使用访问者提供的访问该元素类的方法 */ 5 | public class ConcreteElement implements ElementNode { 6 | 7 | @Override 8 | public void doSomeThings() { 9 | System.out.println("this is an element1 ....."); 10 | } 11 | 12 | @Override 13 | public void accept(Visitor visitor) { 14 | visitor.visit(this); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/visitor/ConcreteElement2.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.visitor; 3 | 4 | /** 具体的元素类,它提供接受访问方法的具体实现,而这个具体的实现,通常情况下是使用访问者提供的访问该元素类的方法 */ 5 | public class ConcreteElement2 implements ElementNode { 6 | 7 | @Override 8 | public void doSomeThings() { 9 | System.out.println("this is an element2........"); 10 | } 11 | 12 | @Override 13 | public void accept(Visitor visitor) { 14 | visitor.visit(this); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/visitor/ConcreteVisitor.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.visitor; 3 | 4 | /** 根据传来的参数判断使用哪一个方法,重载 */ 5 | public class ConcreteVisitor implements Visitor { 6 | 7 | @Override 8 | public void visit(ConcreteElement concreteElement) { 9 | concreteElement.doSomeThings(); 10 | } 11 | 12 | @Override 13 | public void visit(ConcreteElement2 concreteElement2) { 14 | concreteElement2.doSomeThings(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/visitor/ElementNode.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.visitor; 3 | 4 | /** 元素接口或者抽象类,它定义了一个接受访问者的方法(Accept),其意义是指每一个元素都要可以被访问者访问。 */ 5 | public interface ElementNode { 6 | public void doSomeThings(); // 做的事情 7 | 8 | public void accept(Visitor visitor); // 接受访问者作为参数传进来 9 | } 10 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/visitor/Visitor.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.design.visitor; 3 | 4 | /** 5 | * 声明访问者所需要的接口 6 | * 7 | *

它定义了对每一个元素(Element)访问的行为,它的参数就是可以访问的元素,它的方法数理论上来讲与元素个数是一样的 8 | * 因此,访问者模式要求元素的类族要稳定,如果经常添加、移除元素类,必然会导致频繁地修改Visitor接口,如果这样则不适合使用访问者模式。 9 | */ 10 | public interface Visitor { 11 | 12 | public void visit(ConcreteElement concreteElement); 13 | 14 | public void visit(ConcreteElement2 concreteElement2); 15 | } 16 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/visitor/访问者.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/visitor/访问者.png -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/中介者.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/中介者.jpg -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/享元.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/享元.jpg -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/原型.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/原型.jpg -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/备忘录_多重检查点.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/备忘录_多重检查点.jpg -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/工厂方法.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/工厂方法.jpg -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/建造者.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/建造者.gif -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/抽象工厂.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/抽象工厂.jpg -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/白箱备忘录.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/白箱备忘录.jpg -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/组合.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/组合.jpg -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/观察者.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/观察者.jpg -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/访问者模式.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/访问者模式.png -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/责任链.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/责任链.jpg -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/适配器模式.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/适配器模式.jpg -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/design/黑箱备忘录.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/design/黑箱备忘录.jpg -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/lambda/IAnimal.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.lambda; 3 | 4 | /** 动物 */ 5 | public interface IAnimal { 6 | default void breath() { 7 | System.out.println("breath"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/lambda/Ihorse.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.lambda; 3 | 4 | /** 马 接口可以拥有默认方法, 可以包含若干个实例方法 */ 5 | public interface Ihorse { 6 | void eat(); 7 | 8 | default void run() { 9 | System.out.println("horse run"); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/lambda/Mule.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.lambda; 3 | 4 | /** 骡,可以实现为Ihorse,同时骡也是动物 多个接口拥有相同的默认实例方法,则会报错 可以显示调用 IHorse.super.run(); */ 5 | public class Mule implements Ihorse, IAnimal { 6 | 7 | public static void main(String[] args) { 8 | Mule mule = new Mule(); 9 | mule.run(); 10 | mule.breath(); 11 | mule.eat(); 12 | } 13 | 14 | @Override 15 | public void eat() { 16 | System.out.println("Mule eat"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/lambda/NonFunc.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.lambda; 3 | 4 | /** 在Object中实现的方法都不能视为抽象方法 */ 5 | @FunctionalInterface 6 | public interface NonFunc { 7 | boolean equals(Object object); // 不是抽象 8 | 9 | void handle(int i); 10 | } 11 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/other/TestJava.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.other; 3 | 4 | /** 5 | * @author 梦境迷离 6 | * @time 2018-09-08 7 | */ 8 | public class TestJava { 9 | 10 | public static void main(String[] args) { 11 | String a = "A"; 12 | String b = "B"; 13 | StringBuffer sa = new StringBuffer(); 14 | sa.append(a); 15 | change(sa, b); 16 | System.out.println(sa.toString()); // AB 17 | } 18 | 19 | public static void change(StringBuffer sa, String b) { 20 | sa.append(b); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/other/TestJava1.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.other; 3 | 4 | /** 5 | * 语法测试 6 | * 7 | * @author 梦境迷离 8 | * @time 2018-09-28 9 | */ 10 | public class TestJava1 { 11 | 12 | public static void main(String[] args) { 13 | BB B = new BB(); 14 | // 输出 15 | // static A 16 | // static B 17 | // A 18 | // B 19 | } 20 | } 21 | 22 | class AA { 23 | static { 24 | System.out.println("static A"); 25 | } 26 | 27 | AA() { 28 | System.out.println("A"); 29 | } 30 | } 31 | 32 | class BB extends AA { 33 | 34 | static { 35 | System.out.println("static B"); 36 | } 37 | 38 | BB() { 39 | System.out.println("B"); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/other/TestJava3.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.other; 3 | 4 | /** 5 | * 测试语法 6 | * 7 | * @author 梦境迷离 8 | * @time 2018-09-08 9 | */ 10 | public class TestJava3 { 11 | 12 | public static void main(String[] args) { 13 | A a = new A(); 14 | System.out.println(a instanceof B); // true a与父类 lgtm [java/useless-type-test] 15 | System.out.println(a instanceof C); // true a与父接口 lgtm [java/useless-type-test] 16 | System.out.println(a instanceof D); // false a与子类 lgtm [java/useless-type-test] 17 | } 18 | } 19 | 20 | interface C {} 21 | 22 | abstract class B implements C {} 23 | 24 | class A extends B {} 25 | 26 | class D extends A {} 27 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/other/TestJava5.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.other; 3 | 4 | /** 5 | * 测试 6 | * 7 | * @author 梦境迷离 8 | * @time 2018-09-08 9 | */ 10 | public class TestJava5 { 11 | 12 | public static void main(String[] args) { 13 | int s = ~10; 14 | System.out.println(s); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/other/TestJava6.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.other; 3 | 4 | import java.util.List; 5 | 6 | public class TestJava6 { 7 | 8 | public static void main(String[] args) {} 9 | 10 | public void inspect(List list) { 11 | for (Object object : list) { 12 | System.out.println(object); 13 | } 14 | // list.add(1); 15 | // 向未知集合中添加对象不是类型安全的,这会导致编译错误,唯一例外的是null jdk1.5以后可以使用指定索引的添加 16 | } 17 | // public void test() { 18 | // List strs = new ArrayList(); 19 | // inspect(strs);//编译错误 20 | // } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/other/TestJava7.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.other; 3 | 4 | /** 5 | * 测试 6 | * 7 | * @author 梦境迷离 8 | * @time 2018-08-15 9 | */ 10 | public class TestJava7 extends Thread { 11 | 12 | public static void main(String[] args) { 13 | TestJava7 t = new TestJava7(); 14 | TestJava7 s = new TestJava7(); 15 | t.start(); 16 | System.out.println("one."); 17 | s.start(); 18 | System.out.println("two."); 19 | } 20 | 21 | public void run() { 22 | System.out.println("Thread"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/other/TestStaticVar.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.other; 3 | 4 | /** 5 | * @author 梦境迷离 6 | * @description 7 | * @time 2018年3月28日 8 | */ 9 | public class TestStaticVar { 10 | 11 | @SuppressWarnings("unused") 12 | private static int count = 10; 13 | 14 | public static void main(String[] args) { 15 | // 笔试题,优先使用局部 16 | int count = 0; 17 | System.out.println(count); 18 | // 笔试题,Date不是lang包下的,因为需要导包 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/other/接口.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/other/接口.png -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/proxy/Interceptor.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.proxy; 3 | 4 | /** 5 | * 拦截接口 6 | * 7 | * @author 梦境迷离 8 | */ 9 | public interface Interceptor { 10 | Object intercept(HandlerInvocation invocation) throws Exception; 11 | } 12 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/proxy/LoggerInterceptor.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.proxy; 3 | 4 | /** 5 | * 拦截接口的简单实现 6 | * 7 | * @author 梦境迷离 8 | */ 9 | public class LoggerInterceptor implements Interceptor { 10 | public Object intercept(HandlerInvocation invocation) throws Exception { 11 | System.out.println("pre handle"); 12 | Object result = invocation.invoke(); 13 | System.out.println("post handle"); 14 | return result; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/reflect/Expression.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.reflect; 3 | 4 | /** 5 | * 至多两个变量的整数或布尔表达式。 6 | * 7 | * @author Eric Bruneton 8 | */ 9 | public interface Expression { 10 | 11 | /** 12 | * Evaluates this expression. 13 | * 14 | * @param i the value of the first variable. 15 | * @param j the value of the second variable. 16 | * @return the value of this expression for the given variable values. 17 | */ 18 | int eval(int i, int j); 19 | } 20 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/reflect/NotNull.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.examples.reflect; 3 | 4 | import java.lang.annotation.Retention; 5 | import java.lang.annotation.RetentionPolicy; 6 | 7 | @Retention(RetentionPolicy.CLASS) 8 | public @interface NotNull { 9 | String value() default ""; 10 | } 11 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/examples/sql/SQL执行过程简介.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/java-examples/src/main/java/cn/edu/jxnu/examples/sql/SQL执行过程简介.jpg -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/functional/README.md: -------------------------------------------------------------------------------- 1 | # Java的函数式编程 2 | ## 参考资料 3 | 1. [函数式编程思想概论](https://developer.ibm.com/zh/articles/j-understanding-functional-programming-1/) 4 | 2. [用Ruby简单模拟Lambda演算](https://www.jianshu.com/p/f1e5280f8e0b) 5 | ## 简介 6 | 函数式将计算当作数学函数的求值。对于同一个输出函数返回一致的返回值,在函数式编程中就叫做无状态或引用透明性。 -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/reactive/JavaStreamExamples.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.reactive; 3 | 4 | import java.util.Arrays; 5 | import java.util.List; 6 | import java.util.stream.Collectors; 7 | 8 | /** 9 | * Java流式编程的实例 10 | * 11 | * @author 梦境迷离 12 | * @version v1.0 13 | * @since 2019-12-07 14 | */ 15 | public class JavaStreamExamples { 16 | 17 | static final List numbers = Arrays.asList(1, 2, 3); 18 | static final List numbersPlusOne = 19 | numbers.stream().map(number -> number + 1).collect(Collectors.toList()); 20 | 21 | public static void main(String[] args) { 22 | numbersPlusOne.forEach(System.out::println); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/reactive/RxJavaExample.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.reactive; 3 | 4 | import io.reactivex.Observable; 5 | 6 | /** 7 | * rxJava 订阅消息 8 | * 9 | * @author 梦境迷离 10 | * @version v1.0 11 | * @since 2019-12-08 12 | */ 13 | public class RxJavaExample { 14 | 15 | // 订阅事件并在收到时打印 16 | public void observe(String[] strings) { 17 | Observable.fromArray(strings).subscribe(s -> System.out.println("Reactived: " + s)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java-examples/src/main/java/cn/edu/jxnu/reactive/RxJavaExampleDriver.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package cn.edu.jxnu.reactive; 3 | 4 | /** 5 | * rxJava 生产消息 6 | * 7 | * @author 梦境迷离 8 | * @version v1.0 9 | * @since 2019-12-08 10 | */ 11 | public class RxJavaExampleDriver { 12 | 13 | private static final RxJavaExample rx_java_example = new RxJavaExample(); 14 | 15 | public static void main(String[] args) { 16 | // 生产事件 17 | String[] strings = {"a", "b", "c"}; 18 | rx_java_example.observe(strings); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java-examples/src/main/resources/HelloWorld.java.tpl: -------------------------------------------------------------------------------- 1 | package io.github.dreamylost.compiler; 2 | 3 | public class HelloWorld { 4 | 5 | public HelloWorld() { 6 | } 7 | 8 | public void print() { 9 | System.out.println("hello world!"); 10 | } 11 | 12 | @Override 13 | public String toString() { 14 | return "Hello world compiler"; 15 | } 16 | } -------------------------------------------------------------------------------- /java-leetcode/README.md: -------------------------------------------------------------------------------- 1 | # 算法-Java 2 | 3 | 1. 提交前务必执行 **gradle -s spotlessApply** 格式化代码(只处理JVM语言,Java格式使用googleJavaFormat) 4 | 2. Java 8 以上,14以下 5 | 6 | ## author 7 | 8 | [dreamylost](./src/main/java/io/github/dreamylost/dreamylost.md) 9 | 10 | [ccccmaster](./src/main/java/io/github/ccccmaster/ccccmaster.md) 11 | 12 | [poorguy](./src/main/java/io/github/poorguy/poorguy.md) 13 | 14 | [wkk](./src/main/java/io/github/wkk/wkk.md) 15 | 16 | -------------------------------------------------------------------------------- /java-leetcode/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'custom.java' 3 | } 4 | 5 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/ccccmaster/Leetcode_674.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.ccccmaster; 3 | 4 | /** 5 | * 最长连续递增序列 简单 练手 6 | * 7 | * @author chenyu 8 | * @date 2020-07-05. 9 | */ 10 | public class Leetcode_674 { 11 | 12 | public int findLengthOfLCIS(int[] nums) { 13 | if (nums == null || nums.length == 0) { 14 | return 0; 15 | } 16 | int maxLen = 1; 17 | int curLen = 1; 18 | for (int i = 1; i < nums.length; i++) { 19 | if (nums[i] > nums[i - 1]) { 20 | curLen++; 21 | maxLen = curLen > maxLen ? curLen : maxLen; 22 | } else { 23 | curLen = 1; 24 | } 25 | } 26 | return maxLen; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/ccccmaster/Leetcode_JZ_53.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.ccccmaster; 3 | 4 | /** 5 | * 0~n-1中缺失的数字 6 | * 7 | * @author chenyu 8 | * @date 2020-07-05. 9 | */ 10 | public class Leetcode_JZ_53 { 11 | 12 | public int missingNumber(int[] nums) { 13 | for (int i = 0; i < nums.length; i++) { 14 | if (i != nums[i]) { 15 | return i; 16 | } 17 | } 18 | return nums.length; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/ccccmaster/ccccmaster.md: -------------------------------------------------------------------------------- 1 | 1. [最长连续递增序列](./Leetcode_674.java) 2 | 2. [拼写单词](./Leetcode_1160.java) 3 | 3. [0~n-1中缺失的数字](./Leetcode_JZ_53.java) -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/Interview_02_03.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost; 3 | 4 | /** 5 | * 删除中间节点 6 | * 7 | * @author 梦境迷离 8 | * @version v1.0 9 | * @since 2020-03-19 10 | */ 11 | public class Interview_02_03 { 12 | // 将node后面的节点提前一个 13 | public void deleteNode(ListNode node) { 14 | node.value = node.next.value; 15 | node.next = node.next.next; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/LeetCode_237.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost; 3 | 4 | /** 5 | * 删除链表中的节点 6 | * 7 | * @author 梦境迷离 8 | * @version v1.0 9 | * @since 2020-03-14 10 | */ 11 | public class LeetCode_237 { 12 | public void deleteNode(ListNode node) { 13 | node.value = node.next.value; 14 | node.next = node.next.next; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/Leetcode_141.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost; 3 | 4 | /** 5 | * @author 梦境迷离 6 | * @description 判断链表是否存在环 使用双指针,一个指针每次移动一个节点,一个指针每次移动两个节点,如果存在环,那么这两个指针一定会相遇。 7 | * @time 2018年4月7日 8 | */ 9 | public class Leetcode_141 { 10 | 11 | public boolean hasCycle(ListNode head) { 12 | if (head == null) return false; 13 | ListNode l1 = head, l2 = head.next; 14 | while (l1 != null && l2 != null) { 15 | if (l1 == l2) return true; 16 | l1 = l1.next; 17 | if (l2.next == null) break; 18 | l2 = l2.next.next; 19 | } 20 | return false; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/Leetcode_217_Hash.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost; 3 | 4 | import java.util.HashSet; 5 | import java.util.Set; 6 | 7 | /** 8 | * 判断数组是否含有重复元素 9 | * 10 | *

217. Contains Duplicate (Easy) 11 | * 12 | * @author 梦境迷离. 13 | * @time 2018年7月2日 14 | * @version v1.0 15 | */ 16 | public class Leetcode_217_Hash { 17 | 18 | public boolean containsDuplicate(int[] nums) { 19 | Set set = new HashSet<>(); 20 | for (int num : nums) { 21 | set.add(num); 22 | } 23 | return set.size() < nums.length; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/Leetcode_26_Double_Pointer.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost; 3 | 4 | /** 5 | * 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。 6 | * 7 | *

不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。 8 | * 9 | * @author 梦境迷离 10 | * @time 2018-09-20 11 | */ 12 | public class Leetcode_26_Double_Pointer { 13 | 14 | public int removeDuplicates(int[] nums) { 15 | int i = 0; 16 | for (int n : nums) { 17 | if (i == 0 || n > nums[i - 1]) { 18 | nums[i++] = n; 19 | } 20 | } 21 | return i; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/Leetcode_27.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost; 3 | 4 | /** 5 | * 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。 6 | * 7 | *

不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。 8 | * 9 | *

元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。 10 | * 11 | * @author 梦境迷离 12 | * @time 2018-09-20 13 | */ 14 | public class Leetcode_27 { 15 | 16 | public int removeElement(int[] nums, int val) { 17 | int idx = 0; 18 | for (int i = 0; i < nums.length; i++) { 19 | if (nums[i] != val) { 20 | nums[idx++] = nums[i]; 21 | } 22 | } 23 | return idx; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/Leetcode_283_ArrayMatrix.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost; 3 | 4 | /** 5 | * 把数组中的 0 移到末尾 6 | * 7 | *

283. Move Zeroes (Easy) 8 | * 9 | *

For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 10 | * 12, 0, 0]. 11 | * 12 | * @author 梦境迷离. 13 | * @time 2018年7月11日 14 | * @version v1.0 15 | */ 16 | public class Leetcode_283_ArrayMatrix { 17 | 18 | public void moveZeroes(int[] nums) { 19 | int idx = 0; 20 | for (int num : nums) { 21 | if (num != 0) { 22 | nums[idx++] = num; 23 | } 24 | } 25 | while (idx < nums.length) { 26 | nums[idx++] = 0; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/Leetcode_344_Double_Pointer.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost; 3 | 4 | /** 5 | * 编写一个函数,其作用是将输入的字符串反转过来。 6 | * 7 | * @author 梦境迷离 8 | * @time 2018-09-20 9 | */ 10 | public class Leetcode_344_Double_Pointer { 11 | 12 | public String reverseString(String s) { 13 | char[] word = s.toCharArray(); 14 | int i = 0; 15 | int j = s.length() - 1; 16 | while (i < j) { 17 | char temp = word[i]; 18 | word[i] = word[j]; 19 | word[j] = temp; 20 | i++; 21 | j--; 22 | } 23 | return new String(word); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/Leetcode_392.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost; 3 | 4 | /** 5 | * @author 梦境迷离 6 | * @description 判断是否为子串 s = "abc", t = "ahbgdc" Return true. 7 | * @time 2018年4月1日 8 | */ 9 | public class Leetcode_392 { 10 | public boolean isSubsequence(String s, String t) { 11 | for (int i = 0, pos = 0; i < s.length(); i++, pos++) { 12 | // 从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。 13 | pos = t.indexOf(s.charAt(i), pos); 14 | if (pos == -1) return false; 15 | } 16 | return true; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/Leetcode_485_Array.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost; 3 | 4 | /** 5 | * 找出数组中最长的连续 1 6 | * 7 | *

485. Max Consecutive Ones (Easy) 8 | * 9 | * @author 梦境迷离. 10 | * @time 2018年7月14日 11 | * @version v1.0 12 | */ 13 | public class Leetcode_485_Array { 14 | 15 | public int findMaxConsecutiveOnes(int[] nums) { 16 | int max = 0, cur = 0; 17 | for (int x : nums) { 18 | cur = x == 0 ? 0 : cur + 1; 19 | max = Math.max(max, cur); 20 | } 21 | return max; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/Leetcode_540.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost; 3 | 4 | /** 5 | * @author 梦境迷离 6 | * @description 题目描述:一个有序数组只有一个数不出现两次,找出这个数。 7 | * @time 2018年3月30日 8 | */ 9 | public class Leetcode_540 { 10 | 11 | public int singleNonDuplicate(int[] nums) { 12 | int l = 0, h = nums.length - 1; 13 | while (l < h) { 14 | int m = l + (h - l) / 2; 15 | // 这个数必须在偶数上 如: 11 22 33 x 44 55 16 | if (m % 2 == 1) m--; // 保证 l/h/m 都在偶数位,使得查找区间大小一直都是 奇数 17 | if (nums[m] == nums[m + 1]) l = m + 2; 18 | else h = m; 19 | } 20 | return nums[l]; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/Leetcode_633_Double_Pointer.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost; 3 | 4 | /** 5 | * @author 梦境迷离 6 | * @description 判断一个数是否为两个数的平方和,例如 5 = 1^2 + 2^2。 双指针 7 | * @time 2018年4月7日 8 | */ 9 | public class Leetcode_633_Double_Pointer { 10 | public boolean judgeSquareSum(int c) { 11 | // 最大不超过根号c 12 | int left = 0, right = (int) Math.sqrt(c); 13 | while (left <= right) { 14 | int powSum = left * left + right * right; 15 | if (powSum == c) return true; 16 | else if (powSum > c) right--; 17 | else left++; 18 | } 19 | return false; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/Leetcode_69.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost; 3 | 4 | /** 5 | * @author 梦境迷离 6 | * @description 一个数 x 的开方 sqrt 一定在 0 ~ x 之间,并且满足 sqrt == x / sqrt 。可以利用二分查找在 0 ~ x 之间查找 sqrt。 7 | * @time 2018年3月30日 8 | */ 9 | public class Leetcode_69 { 10 | 11 | public int mySqrt(int x) { 12 | if (x <= 1) return x; 13 | int l = 1, h = x; 14 | while (l <= h) { 15 | int mid = l + (h - l) / 2; 16 | int sqrt = x / mid; 17 | if (sqrt == mid) return mid; 18 | else if (sqrt < mid) h = mid - 1; 19 | else l = mid + 1; 20 | } 21 | return h; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/Leetcode_88_Double_Pointer.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost; 3 | 4 | /** 5 | * @author 梦境迷离 6 | * @description 归并两个有序数组,把归并结果存到第一个数组上。 7 | * @time 2018年4月7日 8 | */ 9 | public class Leetcode_88_Double_Pointer { 10 | 11 | public void merge(int[] nums1, int m, int[] nums2, int n) { 12 | int i = m - 1, j = n - 1; // 需要从尾开始遍历,否则在 nums1 上归并得到的值会覆盖还未进行归并比较的值 13 | int idx = m + n - 1; 14 | while (i >= 0 || j >= 0) { 15 | if (i < 0) nums1[idx] = nums2[j--]; 16 | else if (j < 0) nums1[idx] = nums1[i--]; 17 | else if (nums1[i] > nums2[j]) nums1[idx] = nums1[i--]; 18 | else nums1[idx] = nums2[j--]; 19 | idx--; 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/Leetcode_Interview_15.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost; 3 | 4 | /** 5 | * 面试题15. 二进制中1的个数 6 | * 7 | * @author 梦境迷离 8 | * @version v1.0 9 | * @since 2020-04-04 10 | */ 11 | public class Leetcode_Interview_15 { 12 | 13 | public static void main(String[] args) { 14 | int ret = new Leetcode_Interview_15().hammingWeight(9); 15 | System.out.println(ret); 16 | } 17 | 18 | public int hammingWeight(int n) { 19 | int ret = 0; 20 | while (n != 0) { 21 | ret += n & 1; 22 | n >>>= 1; // >>=会超时 23 | } 24 | return ret; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/Leetcode_LCP_1.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost; 3 | 4 | /** 5 | * LCP 1. 猜数字 此种题 不支持rust Scala 6 | * 7 | * @author 梦境迷离 8 | * @version v1.0 9 | * @since 2020-03-13 10 | */ 11 | public class Leetcode_LCP_1 { 12 | 13 | public static void main(String[] args) { 14 | int count = game(new int[] {1, 2, 3}, new int[] {1, 2, 3}); 15 | System.out.println(count); 16 | } 17 | 18 | public static int game(int[] guess, int[] answer) { 19 | int flag = 0; 20 | for (int j = 0; j < answer.length; j++) { 21 | if (guess[j] == answer[j]) { 22 | flag++; 23 | } 24 | } 25 | return flag; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/TreeNode.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost; 3 | 4 | /** 5 | * @description 模拟树的结点 6 | * @author Mr.Li 7 | */ 8 | public class TreeNode { 9 | 10 | public int val = 0; 11 | public TreeNode left = null; 12 | public TreeNode right = null; 13 | 14 | public TreeNode(int val) { 15 | this.val = val; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/dp/package-info.java: -------------------------------------------------------------------------------- 1 | package io.github.dreamylost.dp; 2 | 3 | /** dp */ 4 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/dreamylost.md: -------------------------------------------------------------------------------- 1 | ### 树 2 | 3 | 1. [输出二叉树中所有从根到叶子的路径](./Leetcode_257_DFS.java) Easy 4 | 2. [BiNode](./Leetcode_Interview_17_12.java) Easy 5 | 3. [二叉搜索树的最近公共祖先](./Leetcode_JZ_68_1.java) Easy 6 | 4. [二叉树的最近公共祖先](./Leetcode_JZ_68_2.java) Easy 7 | 5. [首个共同祖先](./Interview_04_08.java) Medium 8 | 6. [后继者](./Interview_04_06.java) Medium 9 | 6. [找出克隆二叉树中的相同节点](./Leetcdoe_1379.java) Medium -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/note.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost; 3 | 4 | /** Java版参考 CyC2018大神,部分参考自Leetcode英文版的国外大佬 */ 5 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/practice/DifferentNumberOfBinaryBits_Java.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.practice; 3 | 4 | /** 5 | * 两个整数的二进制有多少不同位 6 | * 7 | * @author 梦境迷离 8 | * @time 2018年8月10日 9 | * @version v1.0 10 | */ 11 | public class DifferentNumberOfBinaryBits_Java { 12 | 13 | public static int bitSwapRequired(int a, int b) { 14 | int count = 0; 15 | for (int c = a ^ b; c != 0; c = c >>> 1) { 16 | count += c & 1; 17 | } 18 | return count; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/practice/FibonacciDemo2.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.practice; 3 | 4 | /** 5 | * @description 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。 请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 6 | * @author Mr.Li 7 | */ 8 | /** 9 | * @description 本质还是斐波那契 n={0,1,2}是边界 10 | * @author Mr.Li 11 | */ 12 | public class FibonacciDemo2 { 13 | public int RectCover(int target) { 14 | if (target < 1) { 15 | return 0; 16 | } else if (target == 1 || target == 2) { 17 | return target; 18 | } else { 19 | return RectCover(target - 1) + RectCover(target - 2); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/practice/ListNode.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.practice; 3 | 4 | /** 5 | * @description 模拟链表节点 6 | * @author Mr.Li 7 | */ 8 | public class ListNode { 9 | int val; 10 | ListNode next = null; 11 | 12 | ListNode(int val) { 13 | this.val = val; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/practice/O1checkPowerOf2.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.practice; 3 | 4 | /** 5 | * 用 O(1) 时间检测整数 n 是否是 2 的幂次。 6 | * 7 | * @author 梦境迷离 8 | * @time 2018年8月10日 9 | * @version v1.0 10 | */ 11 | public class O1checkPowerOf2 { 12 | 13 | public boolean checkPowerOf2(int n) { 14 | if (n <= 0) { 15 | return false; 16 | } 17 | return (n & (n - 1)) == 0; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/practice/RandomListNode.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.practice; 3 | 4 | public class RandomListNode { 5 | int label; 6 | RandomListNode next = null; 7 | RandomListNode random = null; 8 | 9 | RandomListNode(int label) { 10 | this.label = label; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/practice/Sum.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.practice; 3 | 4 | /** 5 | * @description 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、 case等关键字及条件判断语句(A?B:C)。 6 | * @author Mr.Li 7 | */ 8 | public class Sum { 9 | public static void main(String[] args) { 10 | System.out.println(new Sum().sum_Solution(10)); 11 | } 12 | 13 | public int sum_Solution(int n) { 14 | int sum = (int) (Math.pow(n, 2) + n); 15 | return sum >> 1; 16 | } 17 | 18 | public int Sum_Solution2(int n) { 19 | int ans = n; 20 | @SuppressWarnings("unused") 21 | boolean flag = (n > 0) && ((ans += sum_Solution(n - 1)) > 0); 22 | return ans; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/practice/ToMirror.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.practice; 3 | 4 | /** 5 | * @description 操作给定的二叉树,将其变换为源二叉树的镜像。 6 | * @author Mr.Li 7 | */ 8 | public class ToMirror { 9 | /** 10 | * @description 使用递归 11 | * @param root 12 | */ 13 | public void mirror(TreeNode root) { 14 | if (root == null) return; 15 | if (root.left != null || root.right != null) { 16 | TreeNode temp = root.left; 17 | root.left = root.right; 18 | root.right = temp; 19 | mirror(root.left); 20 | mirror(root.right); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/practice/TreeLinkNode.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.practice; 3 | 4 | public class TreeLinkNode { 5 | int val; 6 | TreeLinkNode left = null; 7 | TreeLinkNode right = null; 8 | TreeLinkNode next = null; 9 | 10 | TreeLinkNode(int val) { 11 | this.val = val; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/practice/TreeNode.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.practice; 3 | 4 | /** 5 | * @description 模拟树的结点 6 | * @author Mr.Li 7 | */ 8 | public class TreeNode { 9 | int val = 0; 10 | TreeNode left = null; 11 | TreeNode right = null; 12 | 13 | public TreeNode(int val) { 14 | this.val = val; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/tooffer/ListNode.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.tooffer; 3 | 4 | public class ListNode { 5 | int val; 6 | ListNode next = null; 7 | 8 | ListNode(int val) { 9 | this.val = val; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/tooffer/RandomListNode.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.tooffer; 3 | 4 | public class RandomListNode { 5 | int label; 6 | RandomListNode next = null; 7 | RandomListNode random = null; 8 | 9 | RandomListNode(int label) { 10 | this.label = label; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/tooffer/T10.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.tooffer; 3 | 4 | /** 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? */ 5 | public class T10 { 6 | 7 | /** 8 | * 依旧是斐波那契数列 2*n的大矩形,和n个2*1的小矩形 其中target*2为大矩阵的大小 9 | * 10 | * @param target 11 | * @return 12 | */ 13 | public int RectCover(int target) { 14 | if (target <= 0) { 15 | return 0; 16 | } 17 | if (target == 1 || target == 2) { 18 | return target; 19 | } 20 | return this.RectCover(target - 1) + this.RectCover(target - 2); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/tooffer/T15.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.tooffer; 3 | 4 | /** 输入一个链表,反转链表后,输出新链表的表头。 */ 5 | public class T15 { 6 | 7 | public ListNode ReverseList(ListNode head) { 8 | if (head == null) { 9 | return head; 10 | } 11 | ListNode newHead = null; 12 | ListNode pNode = head; 13 | ListNode pPrev = null; 14 | while (pNode != null) { 15 | ListNode pNext = pNode.next; 16 | if (pNext == null) { 17 | newHead = pNode; 18 | } 19 | pNode.next = pPrev; 20 | pPrev = pNode; 21 | pNode = pNext; 22 | } 23 | return newHead; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/tooffer/T18.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.tooffer; 3 | 4 | /** 操作给定的二叉树,将其变换为源二叉树的镜像。 */ 5 | public class T18 { 6 | 7 | public void Mirror(TreeNode root) { 8 | if (root == null) { 9 | return; 10 | } 11 | if (root.left == null && root.right == null) { 12 | return; 13 | } 14 | TreeNode temp = root.left; 15 | root.left = root.right; 16 | root.right = temp; 17 | Mirror(root.left); 18 | Mirror(root.right); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/tooffer/T30.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.tooffer; 3 | 4 | /** 5 | * HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学。今天测试组开完会后,他又发话了: 在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决。 6 | * 但是,如果向量中包含负数,是否应该包含某个负数,并期望旁边的正数会弥补它呢? 例如:{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。 7 | * 给一个数组,返回它的最大连续子序列的和,你会不会被他忽悠住?(子向量的长度至少是1) 8 | */ 9 | public class T30 { 10 | 11 | public int FindGreatestSumOfSubArray(int[] array) { 12 | 13 | int res = array[0]; 14 | int max = array[0]; 15 | for (int i = 1; i < array.length; i++) { 16 | max = Math.max(max + array[i], array[i]); 17 | res = Math.max(res, max); 18 | } 19 | return res; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/tooffer/T31.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.tooffer; 3 | 4 | /** 5 | * 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数? 为此他特别数了一下1~13中包含1的数字有1、10、11、12、13因此共出现6次, 6 | * 但是对于后面问题他就没辙了。ACMer希望你们帮帮他,并把问题更加普遍化, 可以很快的求出任意非负整数区间中1出现的次数(从1 到 n 中1出现的次数)。 7 | */ 8 | public class T31 { 9 | 10 | public int NumberOf1Between1AndN_Solution(int n) { 11 | int count = 0; 12 | for (int i = 0; i <= n; i++) { 13 | String num = String.valueOf(i); 14 | for (int j = 0; j < num.length(); j++) { 15 | if ('1' == num.charAt(j)) { 16 | count++; 17 | } 18 | } 19 | } 20 | return count; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/tooffer/T36.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.tooffer; 3 | 4 | import java.util.HashMap; 5 | 6 | /** 输入两个链表,找出它们的第一个公共结点。 */ 7 | public class T36 { 8 | 9 | public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) { 10 | ListNode cur1 = pHead1; 11 | ListNode cur2 = pHead2; 12 | HashMap map = new HashMap<>(); 13 | while (cur1 != null) { 14 | map.put(cur1, 1); 15 | cur1 = cur1.next; 16 | } 17 | while (cur2 != null) { 18 | if (map.containsKey(cur2)) { 19 | return cur2; 20 | } 21 | cur2 = cur2.next; 22 | } 23 | return null; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/tooffer/T37.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.tooffer; 3 | 4 | /** 统计一个数字在排序数组中出现的次数。 */ 5 | public class T37 { 6 | 7 | public int GetNumberOfK(int[] array, int k) { 8 | int count = 0; 9 | for (int i = 0; i < array.length; i++) { 10 | if (array[i] == k) { 11 | count++; 12 | } 13 | } 14 | return count; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/tooffer/T38.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.tooffer; 3 | 4 | /** 输入一棵二叉树,求该树的深度。 从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径, 最长路径的长度为树的深度。 */ 5 | public class T38 { 6 | 7 | public int TreeDepth(TreeNode root) { 8 | if (root == null) { 9 | return 0; 10 | } 11 | int left = TreeDepth(root.left); 12 | int right = TreeDepth(root.right); 13 | return left > right ? (left + 1) : (right + 1); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/tooffer/T39.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.tooffer; 3 | 4 | /** 输入一棵二叉树,判断该二叉树是否是平衡二叉树。 */ 5 | public class T39 { 6 | 7 | public boolean IsBalanced_Solution(TreeNode root) { 8 | if (root == null) { 9 | return true; 10 | } 11 | return Math.abs(maxDepth(root.left) - maxDepth(root.right)) <= 1 12 | && IsBalanced_Solution(root.left) 13 | && IsBalanced_Solution(root.right); 14 | } 15 | 16 | private int maxDepth(TreeNode root) { 17 | if (root == null) { 18 | return 0; 19 | } 20 | return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/tooffer/T47.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.tooffer; 3 | 4 | /** 求1+2+3+...+n,要求不能使用乘除法、 for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。 */ 5 | public class T47 { 6 | 7 | public int Sum_Solution(int n) { 8 | int result = n; 9 | boolean t = ((result != 0) && ((result += Sum_Solution(n - 1)) != 0)); 10 | return result; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/tooffer/T48.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.tooffer; 3 | 4 | import java.math.BigInteger; 5 | 6 | /** 写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。 */ 7 | public class T48 { 8 | 9 | public int Add(int num1, int num2) { 10 | BigInteger integer1 = new BigInteger(String.valueOf(num1)); 11 | BigInteger integer2 = new BigInteger(String.valueOf(num2)); 12 | return integer1.add(integer2).intValue(); 13 | } 14 | 15 | public int Add1(int num1, int num2) { 16 | while (num2 != 0) { 17 | int temp = num1 ^ num2; 18 | num2 = (num1 & num2) << 1; 19 | num1 = temp; 20 | } 21 | return num1; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/tooffer/T62.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.tooffer; 3 | 4 | /** 给定一棵二叉搜索树,请找出其中的第k小的结点。 例如, (5,3,7,2,4,6,8)中, 按结点数值大小顺序第三小结点的值为4。 */ 5 | public class T62 { 6 | 7 | int index = 0; 8 | 9 | TreeNode KthNode(TreeNode pRoot, int k) { 10 | if (null != pRoot) { 11 | TreeNode node = KthNode(pRoot.left, k); 12 | if (null != node) { 13 | return node; 14 | } 15 | index++; 16 | if (index == k) { 17 | return pRoot; 18 | } 19 | node = KthNode(pRoot.right, k); 20 | if (null != node) { 21 | return node; 22 | } 23 | } 24 | return null; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/tooffer/TreeLinkNode.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.tooffer; 3 | 4 | public class TreeLinkNode { 5 | int val; 6 | TreeLinkNode left = null; 7 | TreeLinkNode right = null; 8 | TreeLinkNode next = null; 9 | 10 | TreeLinkNode(int val) { 11 | this.val = val; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/tooffer/TreeNode.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.tooffer; 3 | 4 | public class TreeNode { 5 | int val; 6 | TreeNode left; 7 | TreeNode right; 8 | 9 | TreeNode(int x) { 10 | val = x; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/dreamylost/tooffer/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author 梦境迷离 3 | * @version v1.0 4 | * @since 2020-05-16 5 | */ 6 | package io.github.dreamylost.tooffer; 7 | 8 | /** 9 | * 剑指offer 牛客网 66题 10 | * 11 | *

md派样样式参考 https://github.com/liuenci/GoOffer 12 | * 13 | *

非本人敲的代码,只是收集,并整理。本人敲的在practice包中(此包还有一些以Main开头的是笔试题,和一些编程之美的题目) 14 | */ 15 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/annotation/NotNull.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.annotation; 3 | 4 | /** Parameter should not be null */ 5 | public @interface NotNull {} 6 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/annotation/Nullable.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.annotation; 3 | 4 | public @interface Nullable {} 5 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/arrays/CheckIfNAndItsDoubleExist.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.arrays; 3 | 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | class CheckIfNAndItsDoubleExist { 8 | public boolean checkIfExist(int[] arr) { 9 | Map map = new HashMap<>(); 10 | for (int i = 0; i < arr.length; i++) { 11 | map.put(arr[i], i); 12 | } 13 | for (int i = 0; i < arr.length; i++) { 14 | if (map.containsKey(arr[i] * 2) && map.get(arr[i] * 2) != i) { 15 | return true; 16 | } 17 | } 18 | return false; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/arrays/FindAllNumbersDisappearedInAnArray.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.arrays; 3 | 4 | import java.util.ArrayList; 5 | import java.util.HashMap; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | class FindAllNumbersDisappearedInAnArray { 10 | public List findDisappearedNumbers(int[] nums) { 11 | Map map = new HashMap<>(); 12 | for (int i = 0; i < nums.length; i++) { 13 | map.put(i + 1, 1); 14 | } 15 | for (int i = 0; i < nums.length; i++) { 16 | if (map.containsKey(nums[i])) { 17 | map.remove(nums[i]); 18 | } 19 | } 20 | return new ArrayList(map.keySet()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/arrays/FindNumbersWithEvenNumberOfDigits.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.arrays; 3 | 4 | class FindNumbersWithEvenNumberOfDigits { 5 | public int findNumbers(int[] nums) { 6 | int counter = 0; 7 | for (int num : nums) { 8 | int digitCount = String.valueOf(num).split("").length; 9 | if (digitCount % 2 == 0) { 10 | counter++; 11 | } 12 | } 13 | return counter; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/arrays/HeightChecker.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.arrays; 3 | 4 | import java.util.Arrays; 5 | 6 | class HeightChecker { 7 | public int heightChecker(int[] heights) { 8 | int[] sorted = new int[heights.length]; 9 | for (int i = 0; i < heights.length; i++) { 10 | sorted[i] = heights[i]; 11 | } 12 | Arrays.sort(sorted); 13 | int counter = 0; 14 | for (int i = 0; i < heights.length; i++) { 15 | if (sorted[i] != heights[i]) { 16 | counter++; 17 | } 18 | } 19 | return counter; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/arrays/MaxConsecutiveOnes.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.arrays; 3 | 4 | class MaxConsecutiveOnes { 5 | public int findMaxConsecutiveOnes(int[] nums) { 6 | int counter = 0; 7 | int max = 0; 8 | for (int i : nums) { 9 | if (i == 1) { 10 | counter++; 11 | if (counter > max) { 12 | max = counter; 13 | } 14 | } else { 15 | if (counter > max) { 16 | max = counter; 17 | } 18 | counter = 0; 19 | } 20 | } 21 | return max; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/arrays/MoveZeroes.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.arrays; 3 | 4 | class MoveZeroes { 5 | public void moveZeroes(int[] nums) { 6 | int j = 0; 7 | for (int i = 0; i < nums.length; i++) { 8 | if (nums[i] != 0) { 9 | nums[j] = nums[i]; 10 | j++; 11 | } 12 | } 13 | for (int i = 0; i < nums.length - j; i++) { 14 | nums[nums.length - 1 - i] = 0; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/arrays/RemoveElement.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.arrays; 3 | 4 | class RemoveElement { 5 | public int removeElement(int[] nums, int val) { 6 | int moveCount = 0; 7 | for (int i = 0; i < nums.length; i++) { 8 | if (moveCount + i == nums.length) { 9 | break; 10 | } 11 | 12 | nums[i] = nums[i + moveCount]; 13 | 14 | if (nums[i] == val) { 15 | moveCount++; 16 | i--; 17 | } 18 | } 19 | return nums.length - moveCount; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/arrays/ReplaceElementsWithGreatestElementOnRightSide.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.arrays; 3 | 4 | class ReplaceElementsWithGreatestElementOnRightSide { 5 | public int[] replaceElements(int[] arr) { 6 | int largest = arr[arr.length - 1]; 7 | for (int i = arr.length - 1; i > -1; i--) { 8 | int oldValue = arr[i]; 9 | arr[i] = largest; 10 | if (oldValue > largest) { 11 | largest = oldValue; 12 | } 13 | } 14 | arr[arr.length - 1] = -1; 15 | return arr; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/binarysearch/FindMinimumInRotatedSortedArray.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.binarysearch; 3 | 4 | class FindMinimumInRotatedSortedArray { 5 | public int findMin(int[] nums) { 6 | int l = 0; 7 | int r = nums.length - 1; 8 | int last = nums[r]; 9 | while (l < r) { 10 | int mid = l + (r - l) / 2; 11 | if (nums[mid] > last) { 12 | l = mid + 1; 13 | } else if (nums[mid] < last) { 14 | r = mid; 15 | } 16 | } 17 | return nums[r]; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/binarysearch/FindMinimumInRotatedSortedArray2.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.binarysearch; 3 | 4 | class FindMinimumInRotatedSortedArray2 { 5 | /** bad solution. should use binary search */ 6 | public int findMin(int[] nums) { 7 | if (nums == null || nums.length == 0) { 8 | return Integer.MIN_VALUE; 9 | } 10 | int min = nums[0]; 11 | for (int i = 0; i < nums.length; i++) { 12 | if (nums[i] < min) { 13 | min = nums[i]; 14 | } 15 | } 16 | return min; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/binarysearch/FindPeakElement.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.binarysearch; 3 | 4 | class FindPeakElement { 5 | public int findPeakElement(int[] nums) { 6 | int start = 0; 7 | int end = nums.length - 1; 8 | while (start < end) { 9 | if (start == end) { 10 | return start; 11 | } 12 | int mid = start + (end - start) / 2; 13 | if (nums[mid] < nums[mid + 1]) { 14 | start = mid + 1; 15 | } else { 16 | end = mid; 17 | } 18 | } 19 | return end; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/binarysearch/FindSmallestLetterGreaterThanTarget.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.binarysearch; 3 | 4 | class FindSmallestLetterGreaterThanTarget { 5 | public char nextGreatestLetter(char[] letters, char target) { 6 | int l = 0; 7 | int r = letters.length - 1; 8 | while (l < r) { 9 | int mid = l + (r - l) / 2; 10 | if (letters[mid] - target > 0) { 11 | r = mid; 12 | } else { 13 | l = mid + 1; 14 | } 15 | } 16 | if (r == letters.length - 1 && letters[r] <= target) { 17 | return letters[0]; 18 | } 19 | return letters[r]; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/binarysearch/GuessGame.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.binarysearch; 3 | 4 | public class GuessGame { 5 | int guess(int values) { 6 | return 0; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/binarysearch/ValidPerfectSquare.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.binarysearch; 3 | 4 | class ValidPerfectSquare { 5 | public boolean isPerfectSquare(int num) { 6 | long l = 0L; 7 | long r = num; 8 | while (l <= r) { 9 | long mid = (l + r) / 2; 10 | // !!!!!!!!!!!!!!!!!!!mid*mid might overflow if it is int, then the calculate of new l,r 11 | // will be wrong 12 | if (mid * mid == num) { 13 | return true; 14 | } else if (mid * mid > num) { 15 | r = mid - 1; 16 | } else { 17 | l = mid + 1; 18 | } 19 | } 20 | return false; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/binarysearch/VersionControl.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.binarysearch; 3 | 4 | public class VersionControl { 5 | boolean isBadVersion(int value) { 6 | return true; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/binarytree/Node.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.binarytree; 3 | 4 | class Node { 5 | public int val; 6 | public Node left; 7 | public Node right; 8 | public Node next; 9 | 10 | public Node() {} 11 | 12 | public Node(int _val) { 13 | val = _val; 14 | } 15 | 16 | public Node(int _val, Node _left, Node _right, Node _next) { 17 | val = _val; 18 | left = _left; 19 | right = _right; 20 | next = _next; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/binarytree/TreeNode.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.binarytree; 3 | 4 | public class TreeNode { 5 | int val; 6 | TreeNode left; 7 | TreeNode right; 8 | 9 | TreeNode() {} 10 | 11 | TreeNode(int val) { 12 | this.val = val; 13 | } 14 | 15 | TreeNode(int val, TreeNode left, TreeNode right) { 16 | this.val = val; 17 | this.left = left; 18 | this.right = right; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/dynamicprogramming/MinCostClimbingStairs.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.dynamicprogramming; 3 | 4 | class MinCostClimbingStairs { 5 | // cost.length>=2 6 | public int minCostClimbingStairs(int[] cost) { 7 | int[] dp = new int[cost.length + 3]; 8 | dp[0] = 0; 9 | dp[1] = 0; 10 | System.arraycopy(cost, 0, dp, 2, cost.length); 11 | dp[dp.length - 1] = 0; 12 | 13 | for (int i = 2; i < dp.length; i++) { 14 | dp[i] = Math.min(dp[i - 1], dp[i - 2]) + dp[i]; 15 | } 16 | 17 | return dp[dp.length - 1]; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/dynamicprogramming/README.md: -------------------------------------------------------------------------------- 1 | ## resource 2 | https://leetcode.com/discuss/general-discussion/458695/dynamic-programming-patterns -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/hashtable/ContainsDuplicate.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.hashtable; 3 | 4 | import java.util.HashSet; 5 | import java.util.Set; 6 | 7 | class ContainsDuplicate { 8 | public boolean containsDuplicate(int[] nums) { 9 | Set hashSet = new HashSet<>(); 10 | for (int num : nums) { 11 | if (hashSet.contains(num)) { 12 | return true; 13 | } 14 | hashSet.add(num); 15 | } 16 | return false; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/hashtable/SingleNumber.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.hashtable; 3 | 4 | class SingleNumber { 5 | public int singleNumber(int[] nums) { 6 | int result = 0; 7 | for (int num : nums) { 8 | result ^= num; 9 | } 10 | return result; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/hashtable/TreeNode.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.hashtable; 3 | 4 | public class TreeNode { 5 | int val; 6 | TreeNode left; 7 | TreeNode right; 8 | 9 | TreeNode() {} 10 | 11 | TreeNode(int val) { 12 | this.val = val; 13 | } 14 | 15 | TreeNode(int val, TreeNode left, TreeNode right) { 16 | this.val = val; 17 | this.left = left; 18 | this.right = right; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/hashtable/TwoSum.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.hashtable; 3 | 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | class TwoSum { 8 | public int[] twoSum(int[] nums, int target) { 9 | Map map = new HashMap<>(); 10 | int[] result = new int[2]; 11 | for (int i = 0; i < nums.length; i++) { 12 | int num = nums[i]; 13 | if (map.containsKey(target - num)) { 14 | result[0] = map.get(target - num); 15 | result[1] = i; 16 | return result; 17 | } 18 | map.put(num, i); 19 | } 20 | 21 | return null; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/linkedlist/LinkedListCycle.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.linkedlist; 3 | 4 | /** quick slow pointer */ 5 | public class LinkedListCycle { 6 | public boolean hasCycle(ListNode head) { 7 | if (head == null) { 8 | return false; 9 | } 10 | ListNode slow = head; 11 | ListNode quick = head.next; 12 | while (quick != null && quick.next != null) { 13 | if (slow == quick) { 14 | return true; 15 | } else { 16 | slow = slow.next; 17 | quick = quick.next.next; 18 | } 19 | } 20 | return false; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/linkedlist/LinkedListCycleII.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.linkedlist; 3 | 4 | import java.util.HashSet; 5 | 6 | public class LinkedListCycleII { 7 | public ListNode detectCycle(ListNode head) { 8 | HashSet set = new HashSet(); 9 | while (head != null) { 10 | if (set.contains(head)) { 11 | return head; 12 | } else { 13 | set.add(head); 14 | head = head.next; 15 | } 16 | } 17 | return null; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/linkedlist/ListNode.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.linkedlist; 3 | 4 | class ListNode { 5 | int val; 6 | ListNode next; 7 | 8 | public ListNode() {} 9 | 10 | ListNode(int x) { 11 | val = x; 12 | next = null; 13 | } 14 | 15 | ListNode(int x, ListNode next) { 16 | val = x; 17 | this.next = next; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/linkedlist/Node.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.linkedlist; 3 | 4 | class Node { 5 | public int val; 6 | public Node prev; 7 | public Node next; 8 | public Node child; 9 | Node random; 10 | 11 | public Node() {} 12 | 13 | public Node(int val) { 14 | this.val = val; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/linkedlist/ReverseLinkedList.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.linkedlist; 3 | 4 | /** 5 | * Definition for singly-linked list. public class ListNode { int val; ListNode next; ListNode() {} 6 | * ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; 7 | * this.next = next; } } 8 | */ 9 | class ReverseLinkedList { 10 | public ListNode reverseList(ListNode head) { 11 | ListNode prev = null; 12 | ListNode curr = head; 13 | while (curr != null) { 14 | ListNode t = curr.next; 15 | curr.next = prev; 16 | prev = curr; 17 | curr = t; 18 | } 19 | return prev; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/queuestack/Node.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.queuestack; 3 | 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | class Node { 8 | public int val; 9 | public List neighbors; 10 | 11 | public Node() { 12 | val = 0; 13 | neighbors = new ArrayList(); 14 | } 15 | 16 | public Node(int _val) { 17 | val = _val; 18 | neighbors = new ArrayList(); 19 | } 20 | 21 | public Node(int _val, ArrayList _neighbors) { 22 | val = _val; 23 | neighbors = _neighbors; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/queuestack/TargetSum.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.queuestack; 3 | 4 | class TargetSum { 5 | public int findTargetSumWays(int[] nums, int target) { 6 | return dfs(nums, 0, 0, target); 7 | } 8 | 9 | private int dfs(int[] nums, int index, int sum, int target) { 10 | if (index == nums.length - 1) { 11 | int minus = -nums[index] + sum == target ? 1 : 0; 12 | int add = nums[index] + sum == target ? 1 : 0; 13 | return minus + add; 14 | } 15 | 16 | int minus = dfs(nums, index + 1, sum - nums[index], target); 17 | int add = dfs(nums, index + 1, sum + nums[index], target); 18 | return minus + add; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/recursion/ClimbingStairs.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.recursion; 3 | 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | class ClimbingStairs { 8 | public static Map cache = new HashMap<>(); 9 | 10 | public int climbStairs(int n) { 11 | cache.put(0, 1); 12 | cache.put(1, 1); 13 | 14 | if (n == 0) { 15 | return 1; 16 | } 17 | if (n == 1) { 18 | return 1; 19 | } 20 | if (!cache.containsKey(n - 1)) { 21 | cache.put(n - 1, climbStairs(n - 1)); 22 | } 23 | return cache.get(n - 1) + cache.get(n - 2); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/recursion/ListNode.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.recursion; 3 | 4 | public class ListNode { 5 | int val; 6 | ListNode next; 7 | 8 | ListNode() {} 9 | 10 | ListNode(int val) { 11 | this.val = val; 12 | } 13 | 14 | ListNode(int val, ListNode next) { 15 | this.val = val; 16 | this.next = next; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/recursion/ReverseString.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.recursion; 3 | 4 | class ReverseString { 5 | public void reverseString(char[] s) { 6 | old(s); 7 | } 8 | 9 | public void old(char[] s) { 10 | if (s.length == 0 || s.length == 1) { 11 | return; 12 | } 13 | 14 | int left = 0; 15 | int right = s.length - 1; 16 | char t; 17 | while (left < right) { 18 | t = s[left]; 19 | s[left] = s[right]; 20 | s[right] = t; 21 | left++; 22 | right--; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/recursion/TreeNode.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.recursion; 3 | 4 | public class TreeNode { 5 | int val; 6 | TreeNode left; 7 | TreeNode right; 8 | 9 | public TreeNode() {} 10 | 11 | public TreeNode(int val) { 12 | this.val = val; 13 | } 14 | 15 | public TreeNode(int val, TreeNode left, TreeNode right) { 16 | this.val = val; 17 | this.left = left; 18 | this.right = right; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/recursion2/TreeNode.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.recursion2; 3 | 4 | public class TreeNode { 5 | int val; 6 | TreeNode left; 7 | TreeNode right; 8 | 9 | public TreeNode() {} 10 | 11 | public TreeNode(int val) { 12 | this.val = val; 13 | } 14 | 15 | public TreeNode(int val, TreeNode left, TreeNode right) { 16 | this.val = val; 17 | this.left = left; 18 | this.right = right; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/explore/learn/trie/MaximumXorOfTwoNumbersInAnArray.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.explore.learn.trie; 3 | 4 | class MaximumXorOfTwoNumbersInAnArray { 5 | public int findMaximumXOR(int[] nums) { 6 | if (nums.length == 1) { 7 | return 0; 8 | } 9 | int max = 0; 10 | for (int i = 0; i < nums.length - 1; i++) { 11 | for (int j = i + 1; j < nums.length; j++) { 12 | int xor = nums[i] ^ nums[j]; 13 | if (xor > max) { 14 | max = xor; 15 | } 16 | } 17 | } 18 | return max; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/poorguy.md: -------------------------------------------------------------------------------- 1 | # README 2 | I am not using leetcode-cn.com, but the leetcode.com 3 | 4 | ## code style 5 | 6 | ## explore/learn 7 | 1. Arrays 8 | 2. Linked List 9 | 3. Binary Tree 10 | 4. Recursion I 11 | 5. Recursion II 12 | 6. Binary Search 13 | 7. N-ary Tree 14 | 8. Binary Search Tree 15 | 9. Array and String 16 | 10. Queue & Stack -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/tag/DFS.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.tag; 3 | 4 | public interface DFS {} 5 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/poorguy/util/AbstractLeetcodeRunner.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2021 */ 2 | package io.github.poorguy.util; 3 | 4 | /** 5 | * @author 梦境迷离 6 | * @version 1.0, 2021/3/16 7 | */ 8 | public interface AbstractLeetcodeRunner {} 9 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/wkk/TwoSum.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.wkk; 3 | 4 | import java.util.HashMap; 5 | 6 | /** 7 | * @since 2020/7/30上午9:18 8 | * @author kongwiki kongwiki@163.com 9 | */ 10 | public class TwoSum { 11 | public int[] twoSum(int[] nums, int target) { 12 | HashMap hashMap = new HashMap<>(); 13 | int[] res = new int[2]; 14 | for (int i = 0; i < nums.length; i++) { 15 | if (hashMap.containsKey(target - nums[i])) { 16 | res[0] = i; 17 | res[1] = hashMap.get(target - nums[i]); 18 | break; 19 | } 20 | hashMap.put(nums[i], i); 21 | } 22 | return res; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/wkk/everyday/aug/RepeatedSubstringPattern.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.wkk.everyday.aug; 3 | 4 | /** 5 | * 459. 重复的子字符串 6 | * 7 | * @author kongwiki@163.com 8 | * @since 2020/8/24下午9:14 9 | */ 10 | public class RepeatedSubstringPattern { 11 | public static boolean repeatedSubstringPattern(String s) { 12 | return (s + s).indexOf(s, 1) != s.length(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/wkk/everyday/aug/SameTree.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.wkk.everyday.aug; 3 | 4 | import io.github.wkk.structs.TreeNode; 5 | 6 | /** 7 | * @author kongwiki@163.com 8 | * @since 2020/8/7上午8:58 9 | */ 10 | public class SameTree { 11 | public boolean isSameTree(TreeNode p, TreeNode q) { 12 | if (p == null && q == null) { 13 | return true; 14 | } else if (p == null || q == null) { 15 | return false; 16 | } else if (p.val != q.val) { 17 | return false; 18 | } else { 19 | return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/wkk/structs/ListNode.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.wkk.structs; 3 | 4 | /** 5 | * @author kongwiki@163.com 6 | * @since 2020/8/3上午9:29 7 | */ 8 | public class ListNode { 9 | public int val; 10 | public ListNode left; 11 | public ListNode right; 12 | public ListNode next; 13 | 14 | public ListNode() {} 15 | 16 | public ListNode(int _val) { 17 | val = _val; 18 | } 19 | 20 | public ListNode(int _val, ListNode _left, ListNode _right, ListNode _next) { 21 | val = _val; 22 | left = _left; 23 | right = _right; 24 | next = _next; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/wkk/structs/Node.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.wkk.structs; 3 | 4 | import java.util.ArrayList; 5 | import java.util.List; 6 | 7 | /** 8 | * @author kongwiki@163.com 9 | * @since 2020/8/12上午10:19 10 | */ 11 | public class Node { 12 | public int val; 13 | public List neighbors; 14 | 15 | public Node() { 16 | val = 0; 17 | neighbors = new ArrayList(); 18 | } 19 | 20 | public Node(int _val) { 21 | val = _val; 22 | neighbors = new ArrayList(); 23 | } 24 | 25 | public Node(int _val, ArrayList _neighbors) { 26 | val = _val; 27 | neighbors = _neighbors; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /java-leetcode/src/main/java/io/github/wkk/structs/TreeNode.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.wkk.structs; 3 | 4 | /** 5 | * @author kongwiki@163.com 6 | * @since 2020/8/3上午9:29 7 | */ 8 | public class TreeNode { 9 | public int val; 10 | public int count; 11 | public TreeNode left, right; 12 | 13 | public TreeNode(int val) { 14 | this.val = val; 15 | this.count = 0; 16 | } 17 | 18 | public TreeNode() {} 19 | } 20 | -------------------------------------------------------------------------------- /java-leetcode/src/test/java/io/github/dreamylost/practice/test/IsThePowerOf2_test.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.practice.test; 3 | 4 | import io.github.dreamylost.practice.IsThePowerOf2; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | /** 9 | * 迁移代码,原提交者Perkins 10 | * 11 | * @author Perkins 12 | * @version v1.0 13 | * @time 2019-06-19 14 | */ 15 | public class IsThePowerOf2_test { 16 | 17 | @Test 18 | public void testPowerOf2() { 19 | Assert.assertEquals(-1, IsThePowerOf2.powerOf2(5L)); 20 | Assert.assertEquals(0, IsThePowerOf2.powerOf2(1L)); 21 | Assert.assertEquals(1, IsThePowerOf2.powerOf2(2L)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java-leetcode/src/test/java/io/github/poorguy/test/BinarySearchTest.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.poorguy.test; 3 | 4 | import io.github.poorguy.explore.learn.binarysearch.BinarySearch; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | /** 9 | * Binary Search Test 10 | * 11 | * @author 王一凡 12 | * @date 7/22/20 13 | */ 14 | public class BinarySearchTest { 15 | @Test 16 | public void search() { 17 | int[] nums_1 = new int[] {-1, 0, 3, 5, 9, 12}; 18 | int target_1 = 9; 19 | Assert.assertEquals(4, BinarySearch.search(nums_1, target_1)); 20 | int target_2 = 2; 21 | Assert.assertEquals(-1, BinarySearch.search(nums_1, target_2)); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java-utils/build.gradle: -------------------------------------------------------------------------------- 1 | plugins{ 2 | id 'custom.java' 3 | } 4 | 5 | dependencies { 6 | implementation 'org.apache.commons:commons-lang3:3.12.0' 7 | implementation 'org.projectlombok:lombok:1.18.22' 8 | annotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.22' 9 | implementation 'org.springframework:spring-web:5.3.14' 10 | } 11 | -------------------------------------------------------------------------------- /java-utils/src/main/java/io/github/dreamylost/utils/StreamUtils.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost.utils; 3 | 4 | import java.util.Optional; 5 | import java.util.stream.Stream; 6 | 7 | public final class StreamUtils { 8 | 9 | private StreamUtils() {} 10 | 11 | /** 12 | * Turns an Optional into a Stream of length zero or one depending upon whether a value is 13 | * present. 14 | */ 15 | public static Stream fromOptional(Optional opt) { 16 | return opt.map(Stream::of).orElseGet(Stream::empty); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /kotlin-leetcode/README.md: -------------------------------------------------------------------------------- 1 | # Leetcode Kotlin 实现 2 | 3 | ## author 4 | 5 | [dreamylost](./src/main/kotlin/io/github/dreamylost/dreamylost.md) 6 | -------------------------------------------------------------------------------- /kotlin-leetcode/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'custom.kotlin' 3 | } -------------------------------------------------------------------------------- /kotlin-leetcode/src/main/kotlin/io/github/dreamylost/HelloKotlin.kt: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 @梦境迷离 */ 2 | package io.github.dreamylost 3 | 4 | /** 5 | * 6 | * @author 梦境迷离 7 | * @version 1.0,2020/6/22 8 | */ 9 | class HelloKotlin { 10 | companion object { 11 | @JvmStatic 12 | fun main(args: Array) { 13 | println("hello world") 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /kotlin-leetcode/src/main/kotlin/io/github/dreamylost/ListNode.kt: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 @梦境迷离 */ 2 | package io.github.dreamylost 3 | 4 | /** 5 | * 6 | * @see [[https://github.com/jxnu-liguobin]] 7 | * @author 梦境迷离 8 | * @since 2020-11-21 9 | * @version 1.0 10 | */ 11 | class ListNode(var `val`: Int) { 12 | var next: ListNode? = null 13 | } 14 | -------------------------------------------------------------------------------- /kotlin-leetcode/src/main/kotlin/io/github/dreamylost/TreeNode.kt: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 @梦境迷离 */ 2 | package io.github.dreamylost 3 | 4 | /** 5 | * kotlin 普通类,可空的二叉树结构 6 | * @author 梦境迷离 7 | * @version 1.0,2020/6/22 8 | */ 9 | class TreeNode(var `val`: Int) { 10 | // data class 的 copy不是增量拷贝,非构造函数中的属性会丢失,这里left和right会变成null,实际就是调用构造函数 11 | var left: TreeNode? = null 12 | var right: TreeNode? = null 13 | } 14 | -------------------------------------------------------------------------------- /kotlin-study/README.md: -------------------------------------------------------------------------------- 1 | # 学习 Kotlin 2 | 3 | 顺便比较Scala Java Rust Kotlin之间异同,加深对不同语言的理解。 4 | 5 | 所有代码来自Kotlin官网。 6 | 7 | [基本语法](./src/main/kotlin/io/github/dreamylost/syntax/basic/Demo1.kt) 8 | 9 | [习惯用法](./src/main/kotlin/io/github/dreamylost/syntax/idiom/Demo1.kt) 10 | -------------------------------------------------------------------------------- /kotlin-study/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'custom.kotlin' 3 | } -------------------------------------------------------------------------------- /kotlin-study/src/main/kotlin/io/github/dreamylost/syntax/DefaultValue.kt: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 @梦境迷离 */ 2 | package io.github.dreamylost.syntax 3 | 4 | /** 5 | * 6 | * @author 梦境迷离 7 | * @since 2021/3/20 8 | * @version 1.0 9 | */ 10 | object DefaultValue { 11 | 12 | val f: Float = 0.0F 13 | val d: Double = 0.0 // 没有D 14 | val c: Char = 0.toChar() 15 | val b: Boolean = false 16 | val i: Int = 0 17 | val l: Long = 0 18 | val s: Short = 0 19 | } 20 | -------------------------------------------------------------------------------- /kotlin-study/src/main/kotlin/io/github/dreamylost/syntax/HelloKotlin.kt: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 @梦境迷离 */ 2 | package io.github.dreamylost.syntax 3 | 4 | /** 5 | * 6 | * @author 梦境迷离 7 | * @version 1.0,2020/6/22 8 | */ 9 | fun main() { 10 | println("hello world 2") 11 | } 12 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "scripts": { 3 | "contributors:add": "all-contributors add", 4 | "contributors:generate": "all-contributors generate" 5 | }, 6 | "devDependencies": { 7 | "all-contributors-cli": "^6.17.0" 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /python-leetcode/README.md: -------------------------------------------------------------------------------- 1 | # 算法 Python 2 | 3 | 1. 提交Python代码参考 **leetcode_27.py** 格式,通用数据结构放在 **\_\_init\_\_.py** 中 4 | 2. 可选择性增加main方法和test case 或 ac结果 5 | 3. 提交前使用IDEA的format code格式化代码 6 | 4. 禁止使用Python2 7 | 8 | ## author 9 | 10 | [laozhang](./laozhang/laozhang.md) 11 | 12 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | 所有专题的预备代码 3 | """ 4 | 5 | 6 | class TreeNode: 7 | def __init__(self, x): 8 | self.val = x 9 | self.left = None 10 | self.right = None 11 | 12 | 13 | class Node: 14 | def __init__(self, val=None, children=None): 15 | self.val = val 16 | self.children = children 17 | 18 | 19 | class Node: 20 | def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None): 21 | self.val = val 22 | self.left = left 23 | self.right = right 24 | self.next = next 25 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_1008_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 1008. 先序遍历构造二叉树 6 | """ 7 | from typing import List 8 | 9 | from laozhang import TreeNode 10 | 11 | 12 | class Solution: 13 | def bstFromPreorder(self, preorder: List[int]) -> TreeNode: 14 | def helper(root: TreeNode, val: int) -> TreeNode: 15 | if not root: 16 | return TreeNode(val) 17 | 18 | if val > root.val: 19 | root.right = helper(root.right, val) 20 | else: 21 | root.left = helper(root.left, val) 22 | return root 23 | 24 | root = TreeNode(preorder[0]) 25 | for v in preorder[1:]: 26 | helper(root, v) 27 | return root 28 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_100_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 100. 相同的树 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | def isSameTree(self, p: TreeNode, q: TreeNode) -> bool: 12 | res = True 13 | 14 | def helper(p: TreeNode, q: TreeNode): 15 | nonlocal res 16 | if not p and not q: 17 | return 18 | if not p or not q: 19 | res = False 20 | return 21 | if p.val != q.val: 22 | res = False 23 | helper(p.left, q.left) 24 | helper(p.right, q.right) 25 | 26 | helper(p, q) 27 | return res 28 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_101_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 101. 对称的二叉树 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | def isSymmetric(self, root: TreeNode) -> bool: 12 | def helper(p: TreeNode, q: TreeNode): 13 | if not p and not q: 14 | return True 15 | if not p or not q: 16 | return False 17 | if p.val == q.val: 18 | return helper(p.left, q.right) and helper(p.right, q.left) 19 | return False 20 | 21 | return helper(root, root) 22 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_106_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 106. 从中序与后序遍历序列构造二叉树 6 | """ 7 | from typing import List 8 | 9 | from laozhang import TreeNode 10 | 11 | 12 | class Solution: 13 | def buildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode: 14 | if not postorder: 15 | return None 16 | # 从后续遍历中获取根节点 17 | v = postorder[-1] 18 | # 从中序遍历中获取左右子树的分割点 19 | n = inorder.index(v) 20 | # 创建根节点 21 | root = TreeNode(v) 22 | # 根据n来构建二叉树进行分割递归 23 | root.left = self.buildTree(inorder[:n], postorder[:n]) 24 | root.right = self.buildTree(inorder[n + 1:], postorder[n:-1]) 25 | return root 26 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_107_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 107. 二叉树的层次遍历II 6 | """ 7 | from typing import List 8 | 9 | from laozhang import TreeNode 10 | 11 | 12 | class Solution: 13 | def levelOrderBottom(self, root: TreeNode) -> List[List[int]]: 14 | res = [] 15 | 16 | def helper(root: TreeNode, k: int): 17 | if not root: 18 | return 19 | if len(res) <= k: 20 | res.append([]) 21 | res[k].append(root.val) 22 | helper(root.left, k + 1) 23 | helper(root.right, k + 1) 24 | 25 | helper(root, 0) 26 | return res[::-1] 27 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_110_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 110. 平衡二叉树 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | res = True 12 | 13 | def isBalanced(self, root: TreeNode) -> bool: 14 | def helper(root: TreeNode): 15 | if not root: 16 | return 0 17 | 18 | a = helper(root.left) + 1 19 | b = helper(root.right) + 1 20 | if abs(a - b) > 1: 21 | self.res = False 22 | return max(a, b) 23 | 24 | helper(root) 25 | return self.res 26 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_112_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 112. 路径总和 6 | """ 7 | 8 | from laozhang import TreeNode 9 | 10 | 11 | class Solution: 12 | def hasPathSum(self, root: TreeNode, sum: int) -> bool: 13 | if not root: 14 | return False 15 | sum = sum - root.val 16 | if sum == 0 and not root.left and not root.right: 17 | return True 18 | a = self.hasPathSum(root.left, sum) 19 | if not a: 20 | return self.hasPathSum(root.right, sum) 21 | return a 22 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_114_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 114. 二叉树展开为链表 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | pre = None 12 | 13 | def flatten(self, root: TreeNode) -> None: 14 | """ 15 | Do not return anything, modify root in-place instead. 16 | """ 17 | if not root: 18 | return None 19 | self.flatten(root.right) 20 | self.flatten(root.left) 21 | root.right = self.pre 22 | root.left = None 23 | self.pre = root 24 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_129_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 129. 求根到叶子节点数字之和 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | class Solution: 12 | def sumNumbers(self, root: TreeNode) -> int: 13 | res = 0 14 | 15 | def dfs(root: TreeNode, sum: int) -> int: 16 | nonlocal res 17 | if root: 18 | sum = sum * 10 + root.val 19 | if not root.left and not root.right: 20 | res += sum 21 | dfs(root.left, sum) 22 | dfs(root.right, sum) 23 | 24 | dfs(root, 0) 25 | return res 26 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_1325_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 1325. 删除给定值的叶子节点 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | # Definition for a binary tree node. 11 | # class TreeNode: 12 | # def __init__(self, x): 13 | # self.val = x 14 | # self.left = None 15 | # self.right = None 16 | 17 | class Solution: 18 | def removeLeafNodes(self, root: TreeNode, target: int) -> TreeNode: 19 | if not root: 20 | return None 21 | 22 | root.left = self.removeLeafNodes(root.left, target) 23 | root.right = self.removeLeafNodes(root.right, target) 24 | if not root.left and not root.right and root.val == target: 25 | return None 26 | else: 27 | return root 28 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_1379_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 1379. 找出克隆二叉树中的相同节点 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode: 12 | if original and cloned: 13 | if original.val == target.val: 14 | return cloned 15 | a = self.getTargetCopy(original.left, cloned.left, target) 16 | if not a: 17 | b = self.getTargetCopy(original.right, cloned.right, target) 18 | return b 19 | else: 20 | return a 21 | 22 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_144_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 144. 二叉树的前序遍历 6 | """ 7 | from typing import List 8 | 9 | from laozhang import TreeNode 10 | 11 | 12 | class Solution: 13 | def preorderTraversal(self, root: TreeNode) -> List[int]: 14 | 15 | stack = [] 16 | res = [] 17 | 18 | def dfs(root: TreeNode) -> List[int]: 19 | nonlocal stack, res 20 | while stack or root: 21 | while root: 22 | res.append(root.val) 23 | stack.append(root) 24 | root = root.left 25 | 26 | node = stack.pop() 27 | root = node.right 28 | 29 | dfs(root) 30 | return res 31 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_222_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 222. 完全二叉树的节点个数 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | def countNodes(self, root: TreeNode) -> int: 12 | if not root: 13 | return 0 14 | 15 | a = self.countNodes(root.left) 16 | b = self.countNodes(root.right) 17 | 18 | return a+b+1 19 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_226_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 226. 反转二叉树 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | def invertTree(self, root: TreeNode) -> TreeNode: 12 | if root is None: 13 | return None 14 | temp = root.right 15 | root.right = self.invertTree(root.left) 16 | root.left = self.invertTree(temp) 17 | return root 18 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_230_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 230. 二叉搜索树中第K小的元素 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | def kthSmallest(self, root: TreeNode, k: int) -> int: 12 | count = 0 13 | min = 0 14 | 15 | def helper(root: TreeNode, k: int): 16 | nonlocal count, min 17 | if root: 18 | helper(root.left, k) 19 | count += 1 20 | if count == k: 21 | min = root.val 22 | helper(root.right, k) 23 | 24 | helper(root, k) 25 | return min 26 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_257_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 257. 二叉树的所有路径 6 | """ 7 | from typing import List 8 | 9 | from laozhang import TreeNode 10 | 11 | 12 | class Solution: 13 | def binaryTreePaths(self, root: TreeNode) -> List[str]: 14 | 15 | res = [] 16 | 17 | def helper(root: TreeNode, s: str): 18 | if root: 19 | if not root.left and not root.right: 20 | s += str(root.val) 21 | res.append(s) 22 | else: 23 | s += str(root.val) 24 | s += '->' 25 | helper(root.left, s) 26 | helper(root.right, s) 27 | 28 | helper(root, '') 29 | return res 30 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_27_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 27. 二叉树的镜像 6 | 7 | 所以其他Python代码参考本例格式,通用数据结构放在init中,禁止使用Python2 8 | 9 | 可增加main方法和测速case 或 ac结果 10 | 11 | 提交前使用IDEA的format code格式化代码 12 | """ 13 | from laozhang import TreeNode 14 | 15 | 16 | class Solution: 17 | def mirrorTree(self, root: TreeNode) -> TreeNode: 18 | # 为空返回 19 | if not root: return 20 | # 保存左节点,就像a,b互换值需要一个临时变量 21 | temp = root.left 22 | root.left = self.mirrorTree(root.right) 23 | root.right = self.mirrorTree(temp) 24 | return root 25 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_32_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 32. 二叉树的层次遍历 6 | """ 7 | from typing import List 8 | 9 | from laozhang import TreeNode 10 | 11 | 12 | class Solution: 13 | res = [] 14 | 15 | def levelOrder(self, root: TreeNode) -> List[List[int]]: 16 | 17 | if not root: 18 | return [] 19 | 20 | def helper(root: TreeNode, k): 21 | if not root: 22 | return 23 | if k >= len(self.res): 24 | l = [] 25 | self.res.append(l) 26 | self.res[k].append(root.val) 27 | helper(root.left, k + 1) 28 | helper(root.right, k + 1) 29 | 30 | helper(root, k=0) 31 | return self.res 32 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_404_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 404. 左叶子之和 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | sum = 0 12 | 13 | def sumOfLeftLeaves(self, root: TreeNode) -> int: 14 | if root: 15 | self.sumOfLeftLeaves(root.left) 16 | if root.left and not root.left.left and not root.left.right: 17 | self.sum += root.left.val 18 | self.sumOfLeftLeaves(root.right) 19 | return self.sum 20 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_429_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 429. N叉树的层序遍历 6 | """ 7 | from typing import List 8 | 9 | from laozhang import Node 10 | 11 | 12 | class Solution: 13 | def levelOrder(self, root: 'Node') -> List[List[int]]: 14 | res = [] 15 | 16 | def dfs(root: Node, k: int): 17 | nonlocal res 18 | if not root: 19 | return None 20 | if len(res) <= k: 21 | res.append([]) 22 | res[k].append(root.val) 23 | for chi in root.children: 24 | dfs(chi, k + 1) 25 | 26 | dfs(root, 0) 27 | return res 28 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_513_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 513. 找树左下角的值 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | def findBottomLeftValue(self, root: TreeNode) -> int: 12 | list=[] 13 | 14 | def helper(root:TreeNode,k:int): 15 | nonlocal list 16 | if root: 17 | if k>len(list): 18 | temp=[] 19 | temp.append(root.val) 20 | list.append(temp) 21 | else: 22 | list[k-1].append(root.val) 23 | helper(root.left,k+1) 24 | helper(root.right,k+1) 25 | helper(root,1) 26 | return list[-1][0] -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_515_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 515. 在每个树行中找最大值 6 | """ 7 | from typing import List 8 | 9 | from laozhang import TreeNode 10 | 11 | 12 | class Solution: 13 | def largestValues(self, root: TreeNode) -> List[int]: 14 | 15 | res = [] 16 | 17 | def dfs(root: TreeNode, k: int): 18 | nonlocal res 19 | if not root: 20 | return None 21 | 22 | if k >= len(res): 23 | res.append(root.val) 24 | else: 25 | if res[k] < root.val: 26 | res[k] = root.val 27 | dfs(root.left, k + 1) 28 | dfs(root.right, k + 1) 29 | 30 | dfs(root, 0) 31 | return res 32 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_538_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 538. 二叉搜索树转换为累加树 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | num=0 12 | def convertBST(self, root: TreeNode) -> TreeNode: 13 | if root: 14 | self.convertBST(root.right) 15 | root.val+=self.num 16 | self.num=root.val 17 | self.convertBST(root.left) 18 | return root 19 | 20 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_543_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 543. 二叉树的直径 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | max = 0 12 | 13 | def diameterOfBinaryTree(self, root: TreeNode) -> int: 14 | 15 | if not root or (not root.left and not root.right): 16 | return 0 17 | 18 | def helper(root: TreeNode): 19 | if not root: 20 | return 0 21 | 22 | a = helper(root.left) + 1 23 | b = helper(root.right) + 1 24 | #记录某个节点的最大直径 25 | self.max = self.max if a + b - 2 < self.max else a + b - 2 26 | res = a if a > b else b 27 | return res 28 | 29 | helper(root) 30 | 31 | return self.max 32 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_559_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 559. N叉树的最大深度 6 | """ 7 | from laozhang import Node 8 | 9 | 10 | class Solution: 11 | def maxDepth(self, root: 'Node') -> int: 12 | list = [] 13 | 14 | def dfs(root: Node, k: int) -> int: 15 | nonlocal list 16 | if root: 17 | for c in root.children: 18 | if len(list) < k: 19 | list.append(root) 20 | dfs(c, k + 1) 21 | 22 | if not root: 23 | return 0 24 | dfs(root, 1) 25 | return len(list) + 1 26 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_55_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 55. 二叉树的深度 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | def maxDepth(self, root: TreeNode) -> int: 12 | if root is None: 13 | return 0 14 | leftDepth = self.maxDepth(root.left) + 1 15 | rightDepth = self.maxDepth(root.right) + 1 16 | return leftDepth if leftDepth > rightDepth else rightDepth 17 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_563_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 563. 二叉树的坡度 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | def findTilt(self, root: TreeNode) -> int: 12 | res = 0 13 | 14 | def helper(root: TreeNode): 15 | nonlocal res 16 | if not root: 17 | return 0 18 | left = helper(root.left) 19 | right = helper(root.right) 20 | res += abs(left - right) 21 | return left + right + root.val 22 | 23 | helper(root) 24 | return res 25 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_590_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 590. N叉树的后续遍历 6 | """ 7 | from typing import List 8 | 9 | from laozhang import Node 10 | 11 | 12 | class Solution: 13 | def postorder(self, root: 'Node') -> List[int]: 14 | result = [] 15 | 16 | def postHelper(root): 17 | if not root: 18 | return None 19 | for child in root.children: 20 | postHelper(child) 21 | result.append(child.val) 22 | 23 | if not root: 24 | return result 25 | postHelper(root) 26 | result.append(root.val) 27 | return result 28 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_637_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 637. 二叉树的层平均值 6 | """ 7 | from typing import List 8 | 9 | from laozhang import TreeNode 10 | 11 | 12 | class Solution: 13 | def averageOfLevels(self, root: TreeNode) -> List[float]: 14 | res = [] 15 | average = [] 16 | 17 | def helper(root: TreeNode, k: int): 18 | if not root: 19 | return 20 | if len(res) <= k: 21 | res.append([]) 22 | res[k].append(root.val) 23 | helper(root.left, k + 1) 24 | helper(root.right, k + 1) 25 | 26 | helper(root, 0) 27 | for li in res: 28 | average.append(sum(li) / len(li)) 29 | return average 30 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_700_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 700. 二叉树中的搜索 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | res = None 12 | 13 | def searchBST(self, root: TreeNode, val: int) -> TreeNode: 14 | if root is None: 15 | return 16 | if root.val == val: 17 | self.res = root 18 | return self.res 19 | self.searchBST(root.left, val) 20 | self.searchBST(root.right, val) 21 | return self.res 22 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_701_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 701. 二叉搜索树中的插入操作 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | def insertIntoBST(self, root: TreeNode, val: int) -> TreeNode: 12 | if not root: 13 | return TreeNode(val) 14 | if root.val >= val: 15 | root.left = self.insertIntoBST(root.left, val) 16 | else: 17 | root.right = self.insertIntoBST(root.right, val) 18 | 19 | return root -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_783_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 783. 二叉搜索树节点最小距离 6 | """ 7 | from laozhang import TreeNode 8 | class Solution: 9 | def minDiffInBST(self, root: TreeNode) -> int: 10 | res = 1 << 32 - 1 11 | temp = -1 12 | 13 | def helper(root: TreeNode): 14 | nonlocal res, temp 15 | if root: 16 | helper(root.left) 17 | if res > (root.val - temp) and temp >= 0: 18 | res = root.val - temp 19 | temp = root.val 20 | helper(root.right) 21 | 22 | helper(root) 23 | return res 24 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_814_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 814. 二叉树剪枝 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | def pruneTree(self, root: TreeNode) -> TreeNode: 12 | 13 | def helper(root: TreeNode) -> TreeNode: 14 | if root: 15 | helper(root.left) 16 | helper(root.right) 17 | if root: 18 | if root.left and root.left.val == 0 and not root.left.left and not root.left.right: 19 | root.left = None 20 | if root.right and root.right.val == 0 and not root.right.right and not root.right.left: 21 | root.right = None 22 | helper(root) 23 | return root 24 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_872_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 872. 叶子相似的树 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | def leafSimilar(self, root1: TreeNode, root2: TreeNode) -> bool: 12 | list1 = [] 13 | list2 = [] 14 | 15 | def helper(root: TreeNode, res): 16 | if root: 17 | if not root.left and not root.right: 18 | res.append(root.val) 19 | helper(root.left, res) 20 | helper(root.right, res) 21 | 22 | helper(root1, list1) 23 | helper(root2, list2) 24 | 25 | return list1 == list2 26 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_94_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 94. 二叉树的中序遍历 6 | """ 7 | from typing import List 8 | 9 | from laozhang import TreeNode 10 | 11 | 12 | class Solution: 13 | def inorderTraversal(self, root: TreeNode) -> List[int]: 14 | stack = [] 15 | list = [] 16 | while root or len(stack) > 0: 17 | while root: 18 | stack.append(root) 19 | root = root.left 20 | 21 | node = stack.pop() 22 | list.append(node.val) 23 | root = node.right 24 | 25 | return list 26 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_965_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 965. 单值二叉树 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | # Definition for a binary tree node. 11 | # class TreeNode: 12 | # def __init__(self, x): 13 | # self.val = x 14 | # self.left = None 15 | # self.right = None 16 | 17 | class Solution: 18 | def isUnivalTree(self, root: TreeNode) -> bool: 19 | 20 | def helper(root:TreeNode,value: int): 21 | if not root: 22 | return True 23 | 24 | if root.val != value: 25 | return False 26 | return helper(root.left,value) and helper(root.right,value) 27 | 28 | return helper(root,root.val) 29 | 30 | 31 | -------------------------------------------------------------------------------- /python-leetcode/laozhang/tree/leetcode_98_.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | # -*- coding: utf-8 -*- 3 | # coding=utf-8 4 | """ 5 | 98. 验证二叉搜索树 6 | """ 7 | from laozhang import TreeNode 8 | 9 | 10 | class Solution: 11 | def isValidBST(self, root: TreeNode) -> bool: 12 | temp = -2 ** 32 13 | 14 | def dfs(root: TreeNode) -> bool: 15 | nonlocal temp 16 | if not root: 17 | return True 18 | a = dfs(root.left) 19 | if root.val <= temp: 20 | return False 21 | temp = root.val 22 | if a: 23 | b = dfs(root.right) 24 | return b 25 | return a 26 | 27 | return dfs(root) 28 | -------------------------------------------------------------------------------- /ruby-examples/README.md: -------------------------------------------------------------------------------- 1 | # Ruby实例 2 | 没有用过Ruby,先撸点代码再说 3 | ## 参考 4 | 1. [官方20分钟体验 Ruby](https://www.ruby-lang.org/zh_cn/documentation/quickstart/) -------------------------------------------------------------------------------- /ruby-examples/src/Function.rb: -------------------------------------------------------------------------------- 1 | # 创建匿名函数 2 | -> x { x + 1 } 3 | puts -> x {x+1} #本质上是创建了一个Proc对象 4 | puts -> x {x+1}.call(100) #调用匿名函数,返回101 5 | 6 | # 柯里化 7 | # 把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数,并且返回接受余下的参数而且返回结果的新函数的技术 8 | sum=-> (x,y,z){x+y+z} 9 | puts sum.call(1,2,3) # 6 10 | sum = -> (x) { 11 | -> (y) { 12 | -> (z) { 13 | x + y + z 14 | } 15 | } 16 | } 17 | puts sum.call(1).call(2).call(3) # 6 18 | puts sum[1][2][3] # 6 -------------------------------------------------------------------------------- /rust-datastructure/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust-datastructure" 3 | version = "0.1.0" 4 | authors = ["梦境迷离 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | priority-queue = "1.0.0" -------------------------------------------------------------------------------- /rust-datastructure/src/main.rs: -------------------------------------------------------------------------------- 1 | pub mod bad_safe_singly_deque; 2 | pub mod bad_singly_stack; 3 | pub mod ok_singly_stack; 4 | pub mod persistent_singly_stack; 5 | pub mod priority_queue; 6 | pub mod unsafe_singly_queue; 7 | 8 | pub fn main() { 9 | priority_queue::priority_queue_garro95() 10 | } 11 | -------------------------------------------------------------------------------- /rust-datastructure/src/priority_queue.rs: -------------------------------------------------------------------------------- 1 | use priority_queue::PriorityQueue; 2 | 3 | /// https://github.com/garro95/priority-queue 4 | /// 这个优先级队列使用IndexMap存储元素,并且有每个元素单独的优先级属性 5 | pub(crate) fn priority_queue_garro95() { 6 | let mut pq = PriorityQueue::new(); 7 | 8 | assert!(pq.is_empty()); 9 | pq.push("Apples", 5); 10 | pq.push("Bananas", 8); 11 | pq.push("Strawberries", 23); 12 | 13 | assert_eq!(pq.peek(), Some((&"Strawberries", &23))); 14 | 15 | pq.change_priority("Bananas", 25); 16 | assert_eq!(pq.peek(), Some((&"Bananas", &25))); 17 | 18 | for (item, _) in pq.into_sorted_iter() { 19 | println!("{}", item); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /rust-examples/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust-examples" 3 | version = "0.1.0" 4 | authors = ["梦境迷离 "] 5 | edition = "2018" 6 | #build = "build.rs" 构建前操作 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | cynic = { version = "0.11.1", features = ["surf", "reqwest-blocking"] } 11 | cynic-codegen = { version = "0.11.1" } 12 | serde_json = { version = "1.0", optional = true } 13 | 14 | # Reqwest example requirements 15 | reqwest = { version = "0.10", features = ["json", "blocking"] } 16 | tokio = { version = "1.1", features = ["macros"] } 17 | 18 | # Surf example requirements 19 | surf = "2.0.0" 20 | async-std = "1.6.5" 21 | 22 | 23 | rstest = "0.6.3" 24 | insta = "0.16.0" -------------------------------------------------------------------------------- /rust-examples/README.md: -------------------------------------------------------------------------------- 1 | Rust 学习 - 按照知识点分类的例子 2 | --- 3 | 4 | 参考官方资料:https://doc.rust-lang.org/stable/rust-by-example/index.html -------------------------------------------------------------------------------- /rust-examples/a/b.txt: -------------------------------------------------------------------------------- 1 | hello -------------------------------------------------------------------------------- /rust-examples/a/c/b.txt: -------------------------------------------------------------------------------- 1 | ../b.txt -------------------------------------------------------------------------------- /rust-examples/hello.txt: -------------------------------------------------------------------------------- 1 | hello world 1 -------------------------------------------------------------------------------- /rust-examples/lorem_ipsum.txt: -------------------------------------------------------------------------------- 1 | hello world 3 -------------------------------------------------------------------------------- /rust-examples/world.txt: -------------------------------------------------------------------------------- 1 | hello world 2 -------------------------------------------------------------------------------- /rust-exercise/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust-exercise" 3 | version = "0.1.0" 4 | authors = ["梦境迷离 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | #新增随机数支持依赖,该数字0.5.5实际上是的简写^0.5.5,表示“具有与0.5.5版兼容的公共API的任何版本。” 11 | rand = "0.5.5" 12 | 13 | [profile.release] 14 | panic = 'abort' #发布模式下因恐慌(panic,不可恢复的错误,类似Java的error)而中止,通过将RUST_BACKTRACE环境变量设置为除0以外的任何值来获得错误的确切原因的回溯 15 | 16 | -------------------------------------------------------------------------------- /rust-exercise/README.md: -------------------------------------------------------------------------------- 1 | Rust 学习 2 | --- 3 | 4 | 参考官方资料:https://doc.rust-lang.org/book/ch01-00-getting-started.html -------------------------------------------------------------------------------- /rust-exercise/hello.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/rust-exercise/hello.txt -------------------------------------------------------------------------------- /rust-exercise/src/front_of_house.rs: -------------------------------------------------------------------------------- 1 | //导入子模块的东东 2 | pub mod hosting; 3 | 4 | //Rust会查找 src/hosting.rs(同名文件) 或者 src/hosting/mod.rs,mod不再包含子模块则使用src/hosting.rs,否则使用src/hosting/mod.rs 5 | pub fn add_to_waitlist() { 6 | hosting::add_to_waitlist_host(); 7 | } 8 | -------------------------------------------------------------------------------- /rust-exercise/src/front_of_house/hosting.rs: -------------------------------------------------------------------------------- 1 | //hosting是front_of_house的子模块,hosting在front_of_house的文件夹里面,使用fn公开方法 2 | pub fn add_to_waitlist_host() { 3 | println!("hello world aaaaaaaaaaaa") 4 | } 5 | -------------------------------------------------------------------------------- /rust-http/404.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Hello! 6 | 7 | 8 |

Oops!

9 |

Sorry, I don't know what you're asking for.

10 | 11 | -------------------------------------------------------------------------------- /rust-http/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "rust-http" 5 | version = "0.1.0" 6 | 7 | -------------------------------------------------------------------------------- /rust-http/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust-http" 3 | version = "0.1.0" 4 | authors = ["梦境迷离 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | 11 | [profile.release] 12 | panic = 'abort' 13 | -------------------------------------------------------------------------------- /rust-http/hello.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Hello! 6 | 7 | 8 |

Hello!

9 |

Hi from Rust

10 | 11 | -------------------------------------------------------------------------------- /rust-leetcode/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "rust-leetcode" 5 | version = "0.1.0" 6 | 7 | -------------------------------------------------------------------------------- /rust-leetcode/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust-leetcode" 3 | version = "0.1.0" 4 | authors = ["梦境迷离 <568845948@qq.com>"] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /rust-leetcode/src/interview_05.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///替换空格 4 | impl Solution { 5 | pub fn replace_space(s: String) -> String { 6 | let str = s; 7 | str.replace(" ", "%20") 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /rust-leetcode/src/interview_06.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::{ListNode, Solution}; 2 | 3 | ///从尾到头打印链表 4 | impl Solution { 5 | pub fn reverse_print(head: Option>) -> Vec { 6 | let mut ret = Vec::::new(); 7 | let mut node = head.as_ref(); 8 | loop { 9 | if let Some(root) = node { 10 | ret.push(root.val); 11 | node = root.next.as_ref(); 12 | } else { 13 | break; 14 | } 15 | } 16 | ret.reverse(); 17 | ret 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /rust-leetcode/src/interview_16_07.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///最大数值 4 | impl Solution { 5 | //不能使用if-else 比较运算符 6 | //max(a, b) = ((a + b) + abs(a - b)) / 2。 7 | pub fn maximum(a: i32, b: i32) -> i32 { 8 | let mut a = a as i64; 9 | let mut b = b as i64; 10 | b = a - b; 11 | a -= b & (b >> 32); 12 | a as i32 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /rust-leetcode/src/interview_24.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::{ListNode, Solution}; 2 | 3 | ///反转链表 leetcode 206 4 | impl Solution { 5 | pub fn reverse_list(head: Option>) -> Option> { 6 | let mut pre = None; 7 | let mut tmp = head; 8 | loop { 9 | //每次让head指向的节点指向pre指向的节点 10 | if let Some(mut head) = tmp { 11 | tmp = head.next; 12 | head.next = pre; 13 | pre = Some(head); 14 | } else { 15 | break; 16 | } 17 | } 18 | pre 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /rust-leetcode/src/interview_55_1.rs: -------------------------------------------------------------------------------- 1 | use std::cell::RefCell; 2 | use std::cmp::max; 3 | use std::rc::Rc; 4 | 5 | use crate::pre_structs::{Solution, TreeNode}; 6 | 7 | ///二叉树的深度 = leetcode 104 8 | impl Solution { 9 | //0 ms,100.00% 10 | //2.7 MB,70.37% 11 | pub fn max_depth(root: Option>>) -> i32 { 12 | fn get_depth(root: &Option>>) -> i32 { 13 | if let Some(root) = root { 14 | let node = root.borrow(); 15 | return max(get_depth(&(node.left)), get_depth(&(node.right))) + 1; 16 | } else { 17 | return 0; 18 | } 19 | } 20 | get_depth(&root) 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /rust-leetcode/src/interview_58_2.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///左旋转字符串 4 | impl Solution { 5 | pub fn reverse_left_words(s: String, n: i32) -> String { 6 | let mut s1 = String::from(&s[0..n as usize]); 7 | let s2 = &s[n as usize..s.len()]; 8 | s1.insert_str(0, s2); 9 | s1.to_owned() 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_1051.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///高度检查器 4 | impl Solution { 5 | pub fn height_checker(heights: Vec) -> i32 { 6 | //排序后与原数组的差异个数 7 | let mut c_heights = heights.clone(); 8 | let mut ret = 0; 9 | c_heights.sort(); 10 | for i in 0..heights.len() { 11 | if c_heights[i as usize] != heights[i as usize] { 12 | ret += 1; 13 | } 14 | } 15 | ret 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_1180.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | /// 1480. 一维数组的动态和 4 | /// 给你一个数组 nums 。数组「动态和」的计算公式为:runningSum[i] = sum(nums[0]…nums[i]) 。 5 | impl Solution { 6 | // 0 ms,100.00% 7 | // 2.1 MB,18.07% 8 | pub fn running_sum(nums: Vec) -> Vec { 9 | let mut nums = nums; 10 | let mut tmp_sum = 0; 11 | for i in 0..nums.len() { 12 | tmp_sum += nums[i]; 13 | nums[i] = tmp_sum; 14 | } 15 | 16 | nums 17 | } 18 | } 19 | 20 | #[cfg(test)] 21 | mod test { 22 | use crate::pre_structs::{print_vec, Solution}; 23 | 24 | #[test] 25 | fn running_sum() { 26 | let ret = Solution::running_sum(vec![3, 1, 2, 10, 1].to_owned()); 27 | print_vec(ret); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_1252.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///奇数值单元格的数目 4 | impl Solution { 5 | pub fn odd_cells(n: i32, m: i32, indices: Vec>) -> i32 { 6 | let mut arr = vec![vec![0; m as usize]; n as usize]; 7 | let mut res = 0; 8 | for row in indices { 9 | for i in 0..n { 10 | arr[i as usize][row[1] as usize] += 1; 11 | } 12 | for j in 0..m { 13 | arr[row[0] as usize][j as usize] += 1; 14 | } 15 | } 16 | for i in 0..n { 17 | for j in 0..m { 18 | if arr[i as usize][j as usize] & 1 == 1 { 19 | res += 1; 20 | } 21 | } 22 | } 23 | res 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_1281.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///整数的各位积和之差 4 | impl Solution { 5 | pub fn subtract_product_and_sum(n: i32) -> i32 { 6 | let mut num = n; 7 | let mut muti = 1; 8 | let mut sum = 0; 9 | while num != 0 { 10 | let tmp = num % 10; 11 | muti *= tmp; 12 | sum += tmp; 13 | num /= 10; 14 | } 15 | muti - sum 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_1313.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///解压缩编码列表 4 | impl Solution { 5 | pub fn decompress_rl_elist(nums: Vec) -> Vec { 6 | let mut rets = Vec::new(); 7 | for (index, _e) in nums.iter().enumerate() { 8 | if index & 1 == 0 { 9 | let mut freq = nums[index]; 10 | let value = nums[index + 1]; 11 | while freq != 0 { 12 | rets.push(value); 13 | freq -= 1; 14 | } 15 | } 16 | } 17 | rets 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_1323.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///6 和 9 组成的最大数字 4 | impl Solution { 5 | pub fn maximum69_number(num: i32) -> i32 { 6 | num.to_string().replacen('6', "9", 1).parse().unwrap() 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_1342.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///将数字变成 0 的操作次数 4 | impl Solution { 5 | pub fn number_of_steps(num: i32) -> i32 { 6 | let mut n = num; 7 | let mut i = 0; 8 | while n != 0 { 9 | if n & 1 == 0 { 10 | n /= 2; 11 | } else { 12 | n -= 1; 13 | } 14 | i += 1; 15 | } 16 | i 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_1351.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///统计有序矩阵中的负数 4 | impl Solution { 5 | //应该将矩阵是排序的考虑进去,从右下角或左下角使用标记 6 | pub fn count_negatives(grid: Vec>) -> i32 { 7 | let mut count: i32 = 0; 8 | for r in grid.iter() { 9 | count += r.iter().filter(|&&x| x < 0).count() as i32 10 | } 11 | return count; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_1365.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///有多少小于当前数字的数字 4 | impl Solution { 5 | pub fn smaller_numbers_than_current(nums: Vec) -> Vec { 6 | let mut ret = Vec::with_capacity(nums.len()); 7 | for i in 0..nums.len() { 8 | let mut count = 0; 9 | for j in 0..nums.len() { 10 | if nums[i] > nums[j] { 11 | count += 1; 12 | } 13 | } 14 | ret.push(count); 15 | } 16 | ret 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_1374.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///生成每种字符都是奇数个的字符串 4 | impl Solution { 5 | //偷鸡 6 | pub fn generate_the_string(n: i32) -> String { 7 | let mut ret = vec![]; 8 | if n & 1 == 0 { 9 | ret = vec!['a'; (n - 1) as usize]; 10 | ret.push('b'); 11 | } else { 12 | ret = vec!['a'; n as usize]; 13 | } 14 | let mut rs = String::new(); 15 | ret.iter().for_each(|&c| rs.push(c)); 16 | rs 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_1385.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///两个数组间的距离值 4 | impl Solution { 5 | //暴力解 6 | pub fn find_the_distance_value(arr1: Vec, arr2: Vec, d: i32) -> i32 { 7 | let mut c = 0; 8 | let _ret = arr1.iter().for_each(|&x| { 9 | let mut flag = false; 10 | arr2.iter().for_each(|&y| { 11 | if !flag && (x - y).abs() > d { 12 | } else { 13 | flag = true; 14 | } 15 | }); 16 | if !flag { 17 | c += 1; 18 | } 19 | }); 20 | c 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_1403.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///非递增顺序的最小子序列 4 | impl Solution { 5 | pub fn min_subsequence(nums: Vec) -> Vec { 6 | let mut nums = nums; 7 | nums.sort_by(|a, b| b.cmp(a)); 8 | let size = nums.len(); 9 | let mut f = 0; 10 | let sum: i32 = nums.iter().sum(); 11 | for i in 0..size { 12 | f += nums[i]; 13 | if f > sum - f { 14 | return nums[..i + 1].to_vec(); 15 | } 16 | } 17 | nums 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_1413.rs: -------------------------------------------------------------------------------- 1 | use std::cmp::max; 2 | 3 | use crate::pre_structs::Solution; 4 | 5 | ///逐步求和得到正数的最小值 6 | impl Solution { 7 | pub fn min_start_value(nums: Vec) -> i32 { 8 | let mut start = 1; 9 | let mut min_sum = 0; 10 | for &n in nums.iter() { 11 | min_sum += n; 12 | //累加和的最小值是正数 13 | //min_sum + x >= 1 14 | //x >= 1 - min_sum 15 | //min(x) = 1 - min_sum 16 | start = max(start, 1 - min_sum); 17 | } 18 | start 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_292.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///Nim 游戏 4 | impl Solution { 5 | pub fn can_win_nim(n: i32) -> bool { 6 | if n % 4 == 0 { 7 | false 8 | } else { 9 | true 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_35.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///搜索插入位置 4 | impl Solution { 5 | pub fn search_insert(nums: Vec, target: i32) -> i32 { 6 | let nums = nums; 7 | //找到知己反回索引,没有找到则返回该元素插入后保持数组仍然有序的索引位置,主要用于有序的数组/向量 8 | let ret = match nums.binary_search(&target) { 9 | Ok(found_index) => found_index, 10 | Err(maybe_insert) => maybe_insert, 11 | }; 12 | ret as i32 13 | } 14 | } 15 | 16 | #[cfg(test)] 17 | mod test { 18 | 19 | #[test] 20 | fn search_insert() {} 21 | } 22 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_461.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///汉明距离 4 | impl Solution { 5 | pub fn hamming_distance(x: i32, y: i32) -> i32 { 6 | let mut nums = x ^ y; 7 | //二进制中1的个数 8 | let mut c = 0; 9 | while nums != 0 { 10 | if nums & 1 == 1 { 11 | c += 1; 12 | } 13 | nums = nums >> 1; 14 | } 15 | c 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_557.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///反转字符串中的单词 III 4 | impl Solution { 5 | pub fn reverse_words(s: String) -> String { 6 | let arr: Vec<&str> = s.split(' ').collect(); 7 | let ret: Vec = arr 8 | .iter() 9 | .map(|word| -> String { 10 | let c: String = (*word).chars().rev().collect(); 11 | c 12 | }) 13 | .collect(); 14 | ret.join(" ").to_string() 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_561.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///数组拆分 I 4 | impl Solution { 5 | //尽可能保留最大值 6 | pub fn array_pair_sum(nums: Vec) -> i32 { 7 | let mut nums = nums; 8 | nums.sort(); 9 | let mut sum = 0; 10 | let mut i = 0; 11 | while i < nums.len() { 12 | sum += nums[i as usize]; 13 | i += 2; 14 | } 15 | sum 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_58.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///最后一个单词的长度 4 | impl Solution { 5 | pub fn length_of_last_word(s: String) -> i32 { 6 | let chars: Vec<&str> = s.trim().split(' ').collect(); 7 | chars[chars.len() - 1].len() as i32 8 | } 9 | } 10 | 11 | #[cfg(test)] 12 | mod test { 13 | 14 | #[test] 15 | fn length_of_last_word() {} 16 | } 17 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_709.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///转换成小写字母 4 | impl Solution { 5 | pub fn to_lower_case(str: String) -> String { 6 | str.chars() 7 | .map(|c| { 8 | //说明是大写,+32 9 | if c < 'a' && c >= 'A' { 10 | (c as u8 + 32 as u8) as char 11 | } else { 12 | c 13 | } 14 | }) 15 | .collect() 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_728.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///自除数 4 | impl Solution { 5 | pub fn self_dividing_numbers(left: i32, right: i32) -> Vec { 6 | let mut result = Vec::new(); 7 | for num in left..=right { 8 | if helper(num) { 9 | result.push(num) 10 | } 11 | } 12 | 13 | fn helper(n: i32) -> bool { 14 | for c in n.to_string().chars() { 15 | if ((c as i32) - 48) == 0 || n % ((c as i32) - 48) != 0 { 16 | return false; 17 | } 18 | } 19 | return true; 20 | } 21 | 22 | result 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_832.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///翻转图像 4 | impl Solution { 5 | pub fn flip_and_invert_image(a: Vec>) -> Vec> { 6 | let ret: Vec> = a 7 | .iter() 8 | .map(|row| -> Vec { 9 | let mut new_row: Vec = row 10 | .iter() 11 | .map(|x| -> i32 { 12 | let new_x = if let 0 = x { 1 } else { 0 }; 13 | new_x 14 | }) 15 | .collect(); 16 | new_row.reverse(); 17 | new_row 18 | }) 19 | .collect(); 20 | ret 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_876.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::{ListNode, Solution}; 2 | 3 | ///链表的中间结点 4 | impl Solution { 5 | pub fn middle_node(head: Option>) -> Option> { 6 | let mut q = &head; 7 | let mut s = &head; 8 | while q.is_some() && q.as_ref().unwrap().next.is_some() { 9 | q = &q.as_ref().unwrap().next.as_ref().unwrap().next; 10 | s = &s.as_ref().unwrap().next; 11 | } 12 | s.clone() 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_905.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///按奇偶排序数组 4 | impl Solution { 5 | pub fn sort_array_by_parity(a: Vec) -> Vec { 6 | let (mut even, mut odd): (Vec, Vec) = a.iter().partition(|&n| n % 2 == 0); 7 | even.append(&mut odd); 8 | even 9 | } 10 | } 11 | 12 | #[cfg(test)] 13 | mod test { 14 | 15 | #[test] 16 | fn sort_array_by_parity() {} 17 | } 18 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_933.rs: -------------------------------------------------------------------------------- 1 | use std::collections::VecDeque; 2 | 3 | ///最近的请求次数 4 | struct RecentCounter { 5 | queue: VecDeque, 6 | } 7 | 8 | /** 9 | * `&self` means the method takes an immutable reference. 10 | * If you need a mutable reference, change it to `&mut self` instead. 11 | */ 12 | impl RecentCounter { 13 | fn new() -> Self { 14 | RecentCounter { 15 | queue: VecDeque::new(), 16 | } 17 | } 18 | 19 | fn ping(&mut self, t: i32) -> i32 { 20 | self.queue.push_back(t); 21 | while *self.queue.front().unwrap() < t - 3000 { 22 | self.queue.pop_front(); 23 | } 24 | self.queue.len() as i32 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_942.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///增减字符串匹配 4 | impl Solution { 5 | pub fn di_string_match(s: String) -> Vec { 6 | let n = s.len(); 7 | //'D'代表着倒序,'I'代表升序,那么如果为'D',只要取最大值max,同时最大值减一,作为下一个'D'的最大值;如果为'I',则正好相反,取最小值min,同时加一, 8 | //作为下一个'I'的最小值;剩下最后一个值,max=min,取啥都行 9 | let mut arr = vec![0; n + 1]; 10 | let mut max = n as i32; 11 | let mut min = 0; 12 | for i in 0..n { 13 | if s.get(i..=i).unwrap() == "D" { 14 | arr[i] = max; 15 | max -= 1; 16 | } else { 17 | arr[i] = min; 18 | min += 1; 19 | } 20 | } 21 | arr[n] = max; 22 | arr 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_944.rs: -------------------------------------------------------------------------------- 1 | use std::ops::Index; 2 | 3 | use crate::pre_structs::Solution; 4 | 5 | ///删列造序 6 | impl Solution { 7 | //删除降序的,剩下非降序的 8 | //12ms 9 | pub fn min_deletion_size(a: Vec) -> i32 { 10 | let mut ret = 0; 11 | let row = a.len(); 12 | let col = a[0].len(); 13 | for j in 0..col { 14 | for i in 0..row - 1 { 15 | if a[i].index(j..=j) > a[i + 1].index(j..=j) { 16 | ret += 1; 17 | break; 18 | } 19 | } 20 | } 21 | ret 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /rust-leetcode/src/leetcode_977.rs: -------------------------------------------------------------------------------- 1 | use crate::pre_structs::Solution; 2 | 3 | ///有序数组的平方 4 | impl Solution { 5 | //172ms,2.1MB 6 | pub fn sorted_squares(a: Vec) -> Vec { 7 | let mut a = a; 8 | a[0] = a[0] * a[0]; 9 | for i in 1..a.len() { 10 | let tmp = a[i] * a[i]; 11 | let mut j = i; 12 | while j > 0 && a[j - 1] > tmp { 13 | a[j] = a[j - 1]; 14 | j -= 1 15 | } 16 | a[j] = tmp; 17 | } 18 | a 19 | } 20 | 21 | //12ms,2.2MB 22 | pub fn sorted_squares2(a: Vec) -> Vec { 23 | let mut ret = a; 24 | for (_i, n) in ret.iter_mut().enumerate() { 25 | *n = *n * *n; 26 | } 27 | ret.sort(); 28 | ret 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /rust-minigrep/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "rust-minigrep" 5 | version = "0.1.0" 6 | 7 | -------------------------------------------------------------------------------- /rust-minigrep/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust-minigrep" 3 | version = "0.1.0" 4 | authors = ["梦境迷离 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /rust-minigrep/output.txt: -------------------------------------------------------------------------------- 1 | query result is ggd 2 | query result is dg 3 | query result is d 4 | -------------------------------------------------------------------------------- /rust-minigrep/poem.txt: -------------------------------------------------------------------------------- 1 | 1234567 2 | 3434 3 | ggd 4 | dg 5 | d -------------------------------------------------------------------------------- /rust-oop/Cargo.lock: -------------------------------------------------------------------------------- 1 | # This file is automatically @generated by Cargo. 2 | # It is not intended for manual editing. 3 | [[package]] 4 | name = "rust-oop" 5 | version = "0.1.0" 6 | 7 | -------------------------------------------------------------------------------- /rust-oop/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "rust-oop" 3 | version = "0.1.0" 4 | authors = ["梦境迷离 "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /scala-examples/build.gradle: -------------------------------------------------------------------------------- 1 | plugins{ 2 | id 'custom.scala' 3 | } 4 | dependencies { 5 | implementation 'com.typesafe.play:play_2.13:2.8.11' 6 | } 7 | -------------------------------------------------------------------------------- /scala-examples/src/main/scala/io/github/dreamylost/HashEquals.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | object HashEquals extends App { 5 | 6 | val intVal = new Integer(1) 7 | val longVal = new java.lang.Long(2) //不加包名的话,默认导入scala.Long 8 | 9 | println(intVal == longVal) //输出true,Scala中==等同于equals 10 | 11 | println(intVal.##) //scala hashcode 12 | println(longVal.##) 13 | 14 | println(intVal eq (longVal)) //false,比较引用的相等性,效果等同于Java的引用比较(Ref1==Ref2) 15 | println(intVal ne (longVal)) //true,引用不等 16 | } 17 | -------------------------------------------------------------------------------- /scala-examples/src/main/scala/io/github/dreamylost/MainObject2.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | /** 5 | * 测试 初始化顺序 6 | * 7 | * @author 梦境迷离 8 | * @version 1.0, 2019-06-20 9 | */ 10 | object MainObject2 extends App { 11 | 12 | MainObject1.getData() 13 | 14 | } 15 | -------------------------------------------------------------------------------- /scala-examples/src/main/scala/io/github/dreamylost/ParameterAnnotation.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | import javax.annotation.Nonnull 5 | 6 | /** 7 | * @author 梦境迷离 8 | * @since 2021/3/21 9 | * @version 1.0 10 | */ 11 | object ParameterAnnotation extends App { 12 | 13 | //下面注解均无效 14 | def test(@Nonnull args: String): Unit = { 15 | 16 | println(args) 17 | } 18 | 19 | case class A() 20 | def test1(@Nonnull a: A): Unit = { 21 | println(a) 22 | } 23 | 24 | @Nonnull 25 | def test2(@Nonnull a: A): A = { 26 | a 27 | } 28 | 29 | test(null) 30 | test1(null) 31 | test2(null) 32 | 33 | } 34 | -------------------------------------------------------------------------------- /scala-examples/src/main/scala/io/github/dreamylost/PrintMultiTable.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | /** 5 | * P138 例子 6 | * 7 | * 函数式输出乘法表 8 | */ 9 | object PrintMultiTable extends App { 10 | 11 | val ret = multiTable() 12 | 13 | println { 14 | ret 15 | } 16 | 17 | def makeRowSeq(row: Int) = 18 | for (col <- 1 to 10) yield { 19 | val prod = (row * col).toString 20 | val padding = " " * (4 - prod.length) 21 | padding + prod 22 | } 23 | 24 | def makeRow(row: Int) = makeRowSeq(row).mkString 25 | 26 | def multiTable() = { 27 | val tableSqe = for (row <- 1 to 10) yield makeRow(row) 28 | 29 | tableSqe.mkString("\n") 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /scala-examples/src/main/scala/io/github/dreamylost/Test1.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | object Test1 { 5 | // scala 数组下标用(),泛型用[] 6 | def main(args: Array[String]) = { 7 | println(apply(layout, 10)) 8 | useLambda() 9 | } 10 | 11 | // 函数 f 和 值 v 作为参数,而函数 f 又调用了参数 v 12 | def apply(f: Int => String, v: Int) = f(v) 13 | 14 | def layout[A](x: A) = "[" + x.toString + "]" 15 | 16 | //声明一个lambda 17 | //实现对传入的两个参数进行凭借,lambda可以当做def使用 18 | val lambda = (param1: String, param2: String) => param1 + param2 19 | 20 | def useLambda(): Unit = { 21 | println(lambda("hello", "world")) 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /scala-examples/src/main/scala/io/github/dreamylost/Test2.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | object Test2 extends App { 5 | // 继承App 默认有main方法,自己也可以重写main。或者不继承App,自己写main方法,签名需一致 6 | val string: String = "ABCabc" 7 | val hasUpper = string.exists(_.isUpper) //_.isUpper是函数字面量,其中_占位符(args:type)=> func body 8 | println(hasUpper) 9 | 10 | var i = 0 //scala不能用i++ ++i --i i-- 11 | i += 1 12 | println(i) 13 | 14 | val str = "hello" + 15 | "world"; //+操作符放在末尾,而不是java那样推荐在前面 16 | Console println str 17 | 18 | println { 19 | "hello" 20 | } //只有一个参数的方法调用可以使用花括号 21 | 22 | // string.chars().anyMatch((int ch)-> Character.isUpperCase((char)ch)) //java 8 23 | } 24 | -------------------------------------------------------------------------------- /scala-examples/src/main/scala/io/github/dreamylost/Test3.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | object Test3 extends App { 5 | val arr = new Array[String](3) //这种方式不是函数式编程推荐的 6 | // arr=new Array(2) val不能被重新赋值,但是本身指向的对象可能发生改变比如:改变arr数组内容 7 | arr(0) = "hello1" //实际上,Scala数组赋值也是函数调用,arr(i)底层调用了apply(i)方法,这是与其他方法调用一致的通用规则 8 | arr(1) = "hello2" //arr(i) = "hello" 实际上底层调用了update方法 arr.update(0,"hello") 9 | arr(2) = "hello3" 10 | arr.foreach(println) 11 | } 12 | -------------------------------------------------------------------------------- /scala-examples/src/main/scala/io/github/dreamylost/Test5.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | object Test5 extends App { 5 | val arr = Array("hello", "world") 6 | arr.foreach(print) 7 | } 8 | -------------------------------------------------------------------------------- /scala-examples/src/main/scala/io/github/dreamylost/Test7.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | object Test7 extends App { 5 | val tuple1 = (11, "hello") //可见,类型可以不同。同样元组也是不可变 6 | print(tuple1._1) //打印第一个,从1开始,跟随Haskell、ML等静态类型元组设定的语言传统 7 | print(tuple1._2) //打印第一个,标准库最大22个元组(从概念上可以创建任意长度) 8 | // print(tuple1(1))//不能使用下标,因为apply是只能返回同种类型 9 | } 10 | -------------------------------------------------------------------------------- /scala-examples/src/main/scala/io/github/dreamylost/TimePosTest.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | import java.time.ZonedDateTime 5 | import java.time.ZoneOffset 6 | 7 | /** 8 | * @author 梦境迷离 9 | * @version 1.0,2019/9/30 10 | */ 11 | object TimePosTest extends App { 12 | 13 | def getTimePosOfHour = { 14 | import java.time.format.DateTimeFormatter 15 | val timeStart = ZonedDateTime.now(ZoneOffset.UTC) 16 | val timeEnd = timeStart.plusHours(1) 17 | val formatter = DateTimeFormatter.ofPattern("yyyyMMddHH00") 18 | val startPos = timeStart.format(formatter) 19 | val stopPos = timeEnd.format(formatter) 20 | startPos -> stopPos 21 | } 22 | 23 | println(getTimePosOfHour) 24 | } 25 | -------------------------------------------------------------------------------- /scala-examples/src/main/scala/io/github/dreamylost/cakepattern/AppSpec.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost.cakepattern 3 | 4 | /** 5 | * @author 梦境迷离 6 | * @version 1.0,2020/6/10 7 | */ 8 | object AppSpec extends App { 9 | 10 | APP.car.drive() 11 | APP.car.park() 12 | } 13 | 14 | object APP extends CarComponent with EngineComponent { 15 | override val car: APP.Car = new HondaCar() //实例化时使用了engine 16 | override val engine: APP.Engine = new DieselEngine() 17 | 18 | } 19 | -------------------------------------------------------------------------------- /scala-examples/src/main/scala/io/github/dreamylost/examples/JavaCollectionTest.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost.examples 3 | 4 | import java.util 5 | 6 | import scala.collection.JavaConverters._ 7 | 8 | /** 9 | * Java集合转换为Scala集合 10 | * 11 | * @author 梦境迷离 12 | * @since 2019-09-25 13 | * @version v1.0 14 | */ 15 | object JavaCollectionTest extends App { 16 | 17 | val javaMap = new util.LinkedHashMap[String, String] 18 | javaMap.put("1", "1") 19 | 20 | //使用迭代器遍历 21 | val scalaMap = javaMap.asScala.toMap //return Map(1 -> 1) 22 | //这里toSeq里面实际就是toStream 23 | val scalaStream = javaMap.values().asScala.toSeq //return Stream(1, ?) 24 | 25 | println(scalaMap) 26 | println(scalaStream) 27 | 28 | } 29 | -------------------------------------------------------------------------------- /scala-examples/src/main/scala/io/github/dreamylost/examples/OptionEqualsTest.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost.examples 3 | 4 | /** 5 | * @author 梦境迷离 6 | * @version 1.0,2019/10/15 7 | */ 8 | object OptionEqualsTest extends App { 9 | 10 | val var1 = Option("dsff") 11 | val var2 = Some("dsff") 12 | 13 | println(var1 == var2) 14 | 15 | } 16 | -------------------------------------------------------------------------------- /scala-examples/src/main/scala/io/github/dreamylost/examples/SymbolTest.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost.examples 3 | 4 | /** 5 | * @author 梦境迷离 6 | * @version 1.0,2020/3/25 7 | */ 8 | object SymbolTest { 9 | 10 | //在Scala中,Symbol类型的对象是被拘禁的(interned),任意的同名symbols都指向同一个Symbol对象,避免了因冗余而造成的内存开销。 11 | //而对于String类型,只有编译时确定的字符串是被拘禁的(interned)。字符串的equals方法需要逐个字符比较两个字符串 12 | val s = Symbol("aSymbol") // 'aSymbol 过期 13 | //输出true 14 | println(s == Symbol("aSymbol")) 15 | //输出true 16 | println(s == Symbol("aSymbol")) 17 | } 18 | -------------------------------------------------------------------------------- /scala-examples/src/main/scala/io/github/dreamylost/examples/TestFold.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost.examples 3 | 4 | import scala.concurrent.Await 5 | import scala.concurrent.Future 6 | import scala.concurrent.duration.Duration 7 | 8 | /** 9 | * @author 梦境迷离 10 | * @version 1.0,2020/3/25 11 | */ 12 | object TestFold extends App { 13 | 14 | val d = Option(111) 15 | val empty = None 16 | 17 | //fold[_]是不行的,无法推断类型。 18 | val f = d.fold[Future[Option[_]]](Future.successful(None))(f => Future.successful(Option(f))) 19 | val f2 = empty.fold[Future[Option[_]]](Future.successful(None))(f => Future.successful(Option(f))) 20 | println(Await.result(f, Duration.Inf)) 21 | println(Await.result(f2, Duration.Inf)) 22 | } 23 | -------------------------------------------------------------------------------- /scala-examples/src/main/scala/io/github/dreamylost/examples/fb/README.md: -------------------------------------------------------------------------------- 1 | 《Scala 函数式编程》习题实现 2 | --- 3 | 4 | - 《Scala函数式编程》(小红书)练习题的个人实现,仅供参考 5 | 6 | - 源代码在 fb 包,此处不含包名 7 | 8 | - 目前只含习题部分 9 | 10 | 章节内容 11 | --- 12 | 13 | - [x] Chapter 2: gettingstarted 14 | - [x] Chapter 3: datastructures 15 | - [x] Chapter 4: errorhandling 16 | - [x] Chapter 5: laziness 17 | - [ ] Chapter 6: state 18 | - [ ] Chapter 7: parallelism 19 | - [ ] Chapter 8: testing 20 | - [ ] Chapter 9: parsing 21 | - [ ] Chapter 10: monoids 22 | - [ ] Chapter 11: monads 23 | - [ ] Chapter 12: applicative 24 | - [ ] Chapter 13: iomonad 25 | - [ ] Chapter 14: localeffects 26 | - [ ] Chapter 15: streamingio -------------------------------------------------------------------------------- /scala-examples/src/main/scala/io/github/dreamylost/examples/fb/package.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost.examples 3 | 4 | package object fb { 5 | 6 | /** 7 | * Scala函数式编程 (小红书)练习题 个人实现,仅供参考 8 | * 9 | * 部分较难实现参考https://github.com/fpinscala/fpinscala 10 | */ 11 | 12 | } 13 | -------------------------------------------------------------------------------- /scala-examples/src/main/scala/io/github/dreamylost/examples/reflect/ReflectDemoTestClass.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost.examples.reflect 3 | 4 | /** 5 | * @author 梦境迷离 6 | * @since 2020-03-07 7 | * @version v1.0 8 | */ 9 | class ReflectDemoTestClass { 10 | 11 | def testMethod(name: String) = { 12 | println("println in testMethod = " + name) 13 | name 14 | } 15 | 16 | } 17 | 18 | object ReflectDemoTestClass { 19 | //模拟Java的静态方法 20 | def testStaticMethod(name: String) = { 21 | println("println in testMethod = " + name) 22 | name 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /scala-examples/src/main/scala/io/github/dreamylost/macro/Fundeps.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost.`macro` 3 | 4 | /** 5 | * @see https://docs.scala-lang.org/overviews/macros/implicits.html#fundep-materialization 6 | * @author 梦境迷离 7 | * @version 1.0,2020/3/17 8 | */ 9 | object Fundeps { 10 | 11 | trait Iso[T, U] { 12 | def to(t: T): U 13 | 14 | def from(u: U): T 15 | } 16 | 17 | case class Foo(i: Int, s: String, b: Boolean) 18 | 19 | def conv[C, L](c: C)(implicit iso: Iso[C, L]): L = iso.to(c) 20 | 21 | // val tp = conv(Foo(23, "foo", true))//必须要能推断L类型 22 | // tp: (Int, String, Boolean) 23 | // tp == (23, "foo", true) 24 | } 25 | -------------------------------------------------------------------------------- /scala-icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jxnu-liguobin/cs-summary-reflection/a97159e99fc6592bec5032d18391d18b84708175/scala-icon.png -------------------------------------------------------------------------------- /scala-leetcode/README.md: -------------------------------------------------------------------------------- 1 | Leetcode Scala 实现 2 | --- 3 | 4 | > @unchecked 表示最优解仍不能AC,与极端case有关 5 | 6 | ## author 7 | 8 | [dreamylost](./src/main/scala/io/github/dreamylost/dreamylost.md) 9 | 10 | [sweeneycai](./src/main/scala/io/github/sweeneycai/sweeneycai.md) -------------------------------------------------------------------------------- /scala-leetcode/build.gradle: -------------------------------------------------------------------------------- 1 | plugins{ 2 | id 'custom.scala' 3 | } -------------------------------------------------------------------------------- /scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_104_Tree.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | /** 5 | * 二叉树的最大深度 6 | * 104. Maximum Depth of Binary Tree (Easy) 7 | * 8 | * @time 2018年7月28日 9 | * @version v1.0 10 | */ 11 | object Leetcode_104_Tree extends App { 12 | 13 | def maxDepth(root: TreeNode): Int = { 14 | if (root == null) return 0 15 | math.max(maxDepth(root.left), maxDepth(root.right)) + 1 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_1108.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | /** 5 | * 给你一个有效的 IPv4 地址 address,返回这个 IP 地址的无效化版本。 6 | * 7 | * 所谓无效化 IP 地址,其实就是用 "[.]" 代替了每个 "."。 8 | * 9 | * @author 梦境迷离 10 | * @since 2020-01-06 11 | * @version v1.0 12 | */ 13 | object Leetcode_1108 { 14 | 15 | def defangIPaddr(address: String): String = { 16 | val cs = address.flatMap(c => if (c == '.') List('[', '.', ']') else List(c)) 17 | cs.mkString 18 | // address.replace(".","[.]") //有点丢人,不好意思用 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_110_Tree.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | /** 5 | * 平衡二叉树 6 | * 7 | * 110. Balanced Binary Tree (Easy) 8 | * 9 | * @author 梦境迷离 10 | * @time 2018年7月28日 11 | * @version v1.0 12 | */ 13 | object Leetcode_110_Tree extends App { 14 | 15 | private var result = true 16 | 17 | def isBalanced(root: TreeNode): Boolean = { 18 | maxDepth(root) 19 | result 20 | } 21 | 22 | //@tailrec 尾递归使用注解优化 23 | def maxDepth(root: TreeNode): Int = { 24 | if (root == null) 25 | return 0 26 | val l = maxDepth(root.left) 27 | val r = maxDepth(root.right) 28 | if (Math.abs(l - r) > 1) 29 | result = false 30 | 1 + math.max(l, r) 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_111_Tree.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | /** 5 | * 最小路径 6 | * 7 | * 111. Minimum Depth of Binary Tree (Easy) 8 | * 9 | * 树的根节点到叶子节点的最小路径长度 10 | * 11 | * @author 梦境迷离 12 | * @time 2018年8月9日 13 | * @version v1.0 14 | */ 15 | object Leetcode_111_Tree extends App { 16 | 17 | def minDepth(root: TreeNode): Int = { 18 | if (root == null) return 0 19 | var left = minDepth(root.left) 20 | var right = minDepth(root.right) 21 | if (left == 0 || right == 0) return left + right + 1 22 | return math.min(left, right) + 1 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_1281.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | /** 5 | * @author 梦境迷离 6 | * @version 1.0,2020/3/12 7 | */ 8 | object Leetcode_1281 extends App { 9 | def subtractProductAndSum(n: Int): Int = { 10 | val f = n.toString.toCharArray.map(c => Integer.parseInt(c + "")) 11 | f.product - f.sum 12 | } 13 | 14 | Console println subtractProductAndSum(234) 15 | } 16 | -------------------------------------------------------------------------------- /scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_1295.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | /** 5 | * 给你一个整数数组 nums,请你返回其中位数为 偶数 的数字的个数。 6 | * 7 | * @author 梦境迷离 8 | * @since 2020-01-01 9 | * @version v1.0 10 | */ 11 | object Leetcode_1295 extends App { 12 | 13 | println(findNumbers(Array(12, 345, 2, 6, 7896))) 14 | 15 | def findNumbers(nums: Array[Int]): Int = { 16 | 17 | val isEvenNum = (n: Int) => { 18 | var i = 0 19 | var j = n 20 | while (j > 0) { 21 | i += 1 22 | j = j / 10 23 | } 24 | if (i % 2 == 0) true else false 25 | } 26 | //_.toString.length == 2 (滑稽) 27 | nums.count(isEvenNum) 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_1351.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | /** 5 | * 统计有序矩阵中的负数 6 | * 7 | * @author 梦境迷离 8 | * @since 2020-03-15 9 | * @version v1.0 10 | */ 11 | object Leetcode_1351 extends App { 12 | def countNegatives(grid: Array[Array[Int]]): Int = { 13 | grid.flatten.count(_ < 0) //应该将矩阵是排序的考虑进去,从右下角或左下角使用标记位 14 | } 15 | 16 | Console println countNegatives( 17 | Array(Array(4, 3, 2, -1), Array(3, 2, 1, -1), Array(1, 1, -1, -2), Array(-1, -1, -2, -3)) 18 | ) 19 | } 20 | -------------------------------------------------------------------------------- /scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_226_Tree.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | /** 5 | * 翻转树 6 | * 226. Invert Binary Tree (Easy) 7 | * @author 梦境迷离 8 | * @time 2018年7月29日 9 | * @version v1.0 10 | */ 11 | object Leetcode_226_Tree extends App { 12 | 13 | def invertTree(root: TreeNode): TreeNode = { 14 | if (root == null) 15 | return null 16 | val left = root.left // 后面的操作会改变 left 指针,因此先保存下来 17 | root.left = invertTree(root.right) 18 | root.right = invertTree(left) 19 | root 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_303.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | /** 5 | * 303. 区域和检索 - 数组不可变 6 | * 给定一个整数数组 nums,求出数组从索引 i 到 j (i ≤ j) 范围内元素的总和,包含 i, j 两点。 7 | * 8 | * @author 梦境迷离 9 | * @time 2018年8月13日 10 | * @version v1.0 11 | */ 12 | object Leetcode_303 extends App { 13 | 14 | class NumArray(_nums: Array[Int]) { 15 | 16 | //牺牲空间 17 | for (i <- 1 until _nums.length) 18 | _nums(i) += _nums(i - 1); 19 | var nums = _nums; 20 | 21 | def sumRange(i: Int, j: Int): Int = { 22 | if (i == 0) return nums(j) 23 | nums(j) - nums(i - 1) 24 | } 25 | } 26 | 27 | val ret = new NumArray(Array(-2, 0, 3, -5, 2, -1)).sumRange(0, 2) 28 | print(ret) 29 | } 30 | -------------------------------------------------------------------------------- /scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_7.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | /** 5 | * 反转数字 6 | * 求余得尾数,以除得十位 7 | * 8 | * @author 梦境迷离 9 | * @since 2020-01-01 10 | * @version v1.0 11 | */ 12 | object Leetcode_7 extends App { 13 | 14 | Console println reverse(1534236469) 15 | 16 | def reverse(x: Int): Int = { 17 | var n = x 18 | var m = 0 19 | var tmp = 0 20 | while (n != 0) { 21 | m = m * 10 + n % 10 22 | if (m / 10 != tmp) { 23 | return 0 24 | } 25 | tmp = m 26 | n = n / 10 27 | } 28 | m 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_70.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | /** 5 | * 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 6 | * 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢? 7 | * 注意:给定 n 是一个正整数。 8 | * 9 | * @author 梦境迷离 10 | * @time 2018年8月14日 11 | * @version v1.0 12 | */ 13 | object Leetcode_70 extends App { 14 | 15 | print(climbStairs(2)) 16 | 17 | def climbStairs(number: Int): Int = { 18 | if (number <= 0) 19 | return 0 20 | if (number == 1 || number == 2) 21 | return number 22 | var (first, second, third) = (1, 2, 0) 23 | for (i <- 3 to number) { 24 | third = first + second 25 | first = second 26 | second = third 27 | } 28 | third 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_771.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | /** 5 | * 求S中符合J的元素的个数 6 | * 简单粗暴 7 | * 8 | * @author 梦境迷离 9 | * @since 2020-01-01 10 | * @version v1.0 11 | */ 12 | object Leetcode_771 extends App { 13 | 14 | println(numJewelsInStones("aA", "aAAbbbb")) 15 | 16 | def numJewelsInStones(J: String, S: String): Int = { 17 | S.toSeq.count(J.toSeq.contains) 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_80_Array.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | /** 5 | * 删除排序数组中的重复项 II 6 | * 7 | * @author 梦境迷离 8 | * @time 2018年7月16日 9 | */ 10 | object Leetcode_80_Array { 11 | 12 | def removeDuplicates(nums: Array[Int]): Int = { 13 | var i = 0 14 | for (n <- nums) { 15 | if (i < 2 || n > nums(i - 2)) { 16 | nums(i) = n 17 | i += 1 18 | } 19 | } 20 | i 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /scala-leetcode/src/main/scala/io/github/dreamylost/Leetcode_Interview_27.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | /** 5 | * 镜像二叉树非递归 6 | * 7 | * @author 梦境迷离 8 | * @version 1.0,2020/3/18 9 | */ 10 | object Leetcode_Interview_27 extends App { 11 | 12 | //层次遍历-QUEUE 13 | def mirrorTree(root: TreeNode): TreeNode = { 14 | import scala.collection.mutable 15 | val queue = mutable.Queue[TreeNode]() 16 | queue.enqueue(root) 17 | while (queue.nonEmpty) { 18 | val tmp = queue.dequeue() 19 | if (tmp != null) { 20 | val left = tmp.left 21 | tmp.left = tmp.right 22 | tmp.right = left 23 | queue.enqueue(tmp.right) 24 | queue.enqueue(tmp.left) 25 | } 26 | } 27 | root 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /scala-leetcode/src/main/scala/io/github/dreamylost/PrintlnSupport.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | import com.google.gson.Gson 5 | import com.google.gson.GsonBuilder 6 | 7 | /** 8 | * @author 梦境迷离 dreamylost 9 | * @since 2020-07-12 10 | * @version v1.0 11 | */ 12 | trait PrintlnSupport { 13 | 14 | private val gson: Gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create 15 | 16 | def prettyPrinting(any: Any) = gson.toJson(any) 17 | 18 | } 19 | -------------------------------------------------------------------------------- /scala-leetcode/src/main/scala/io/github/dreamylost/SecondLargeNumbers.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost 3 | 4 | import scala.io.StdIn 5 | 6 | object SecondLargeNumbers extends App { 7 | 8 | def secondLargeNumbers: Int = { 9 | var max = Integer.MIN_VALUE 10 | var sec = Integer.MIN_VALUE 11 | var n = StdIn.readInt() 12 | while (n > 0) { 13 | n -= 1 14 | var temp = StdIn.readInt() 15 | if (temp >= max) { 16 | sec = max // sec=min,max=1 sec=1,max=2,sec=2,max=3... 17 | max = temp 18 | } else if (temp >= sec) { 19 | sec = temp 20 | } 21 | } 22 | sec 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /scala-leetcode/src/main/scala/io/github/dreamylost/TreeNode.java: -------------------------------------------------------------------------------- 1 | /* All Contributors (C) 2020 */ 2 | package io.github.dreamylost; 3 | 4 | /** 5 | * @description 模拟树的结点 6 | * @author Mr.Li 7 | */ 8 | public class TreeNode { 9 | 10 | public int value = 0; 11 | public TreeNode left = null; 12 | 13 | @Override 14 | public String toString() { 15 | return "TreeNode{" + "value=" + value + ", left=" + left + ", right=" + right + '}'; 16 | } 17 | 18 | public TreeNode right = null; 19 | 20 | public TreeNode(int val) { 21 | this.value = val; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /scala-leetcode/src/main/scala/io/github/sweeneycai/Leetcode_201.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.sweeneycai 3 | 4 | /** 5 | * 201. 数字范围按位与 (Medium) 6 | * 给定范围 [m, n],其中 0 <= m <= n <= 2147483647,返回此范围内所有数字的按位与(包含 m, n 两端点)。 7 | */ 8 | object Leetcode_201 extends App { 9 | def rangeBitwiseAnd(m: Int, n: Int): Int = { 10 | // 问题的关键在于求出二进制的公共前缀 11 | var b = n 12 | while (m < b) { 13 | // 和比自身小 1 的数相与,可以消去二进制表示中最右边的 1 14 | b = b & (b - 1) 15 | } 16 | b 17 | } 18 | println(rangeBitwiseAnd(5, 7)) 19 | } 20 | -------------------------------------------------------------------------------- /scala-leetcode/src/main/scala/io/github/sweeneycai/Leetcode_96.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.sweeneycai 3 | 4 | /** 5 | * 96. 不同的二叉搜索树 (Medium) 6 | * 7 | * 给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 8 | * @see leetcode-cn.com 9 | */ 10 | object Leetcode_96 extends App { 11 | def numTrees(n: Int): Int = { 12 | val array = Array.fill(n + 1)(0) 13 | array(0) = 1 14 | array(1) = 1 15 | for (i <- 2 to n) { 16 | for (j <- 1 to i) { 17 | array(i) += array(j - 1) * array(i - j) 18 | } 19 | } 20 | array.last 21 | } 22 | 23 | println(numTrees(6)) 24 | } 25 | -------------------------------------------------------------------------------- /scala-utils/README.md: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | 学习与工作过程中时写的或收集的Scala工具类。(长期同步) 4 | 5 | 与 Scala 生态相关的 examples code 已统一移到了 [scala-examples](https://github.com/jxnu-liguobin/scala-examples) 项目 6 | 7 | -------------------------------------------------------------------------------- /scala-utils/src/main/scala/io/github/dreamylost/concurrent/Executable.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost.concurrent 3 | 4 | import scala.concurrent.ExecutionContext 5 | import scala.concurrent.Future 6 | import scala.util.control.NonFatal 7 | 8 | /** 9 | * 带错误捕获的线程执行器 10 | * 11 | * @author 梦境迷离 12 | * @time 2019-08-18 13 | * @version v1.0 14 | */ 15 | trait Executable { 16 | 17 | def executeSafely[T](block: => Future[T], exceptionCaught: Throwable => Unit)(implicit 18 | ec: ExecutionContext 19 | ): Unit = { 20 | try { 21 | block.recover { 22 | //非致命错误 23 | case NonFatal(t) => exceptionCaught(t) 24 | } 25 | } catch { 26 | case NonFatal(t) => exceptionCaught(t) 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /scala-utils/src/main/scala/io/github/dreamylost/http/RoutingHandler.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost.http 3 | 4 | /** 5 | * undertow 处理器接口 6 | * 7 | * 一般restful覆盖route和methods即可 8 | * 9 | * @author 梦境迷离 10 | * @time 2019-08-18 11 | * @version v1.0 12 | */ 13 | trait RoutingHandler { 14 | 15 | //uri 16 | def route: String 17 | 18 | //POST GET DELETE PUT 19 | def methods: Set[String] 20 | 21 | def pattern: Option[String] = None 22 | 23 | def single(method: String): Set[String] = Set(method) 24 | 25 | def multi(methods: String*): Set[String] = methods.toSet 26 | 27 | } 28 | -------------------------------------------------------------------------------- /scala-utils/src/main/scala/io/github/dreamylost/other/ConditionUtils.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost.other 3 | 4 | import scala.concurrent.Future 5 | 6 | /** 7 | * 条件处理工具 8 | * 9 | * @author 梦境迷离 10 | * @version 1.0, 2019-07-15 11 | */ 12 | object ConditionUtils { 13 | 14 | /** 15 | * for推断中抛出异常 16 | * 17 | * {{{ 18 | * for{ 19 | * _ <- failCondition(user.isEmpty, InternalErrorException(s"user not found ${p.userId}")) 20 | * } 21 | * }}} 22 | * 23 | * @param condition 24 | * @param PlayException 25 | * @return 26 | */ 27 | def failCondition(condition: Boolean, PlayException: => Exception): Future[Unit] = 28 | if (condition) Future.failed(PlayException) else Future.successful(()) 29 | 30 | } 31 | -------------------------------------------------------------------------------- /scala-utils/src/main/scala/io/github/dreamylost/other/LiftedUtil.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost.other 3 | 4 | import scala.concurrent.ExecutionContext 5 | import scala.concurrent.Future 6 | 7 | /** 8 | * 消除嵌套的Seq[Future] 9 | * 10 | * @author 梦境迷离 11 | * @since 2019-09-04 12 | * @version v1.0 13 | */ 14 | object LiftedUtil { 15 | 16 | /** 17 | * {{{ 18 | * use for Seq[Future[Option[T]]] => Future[Seq[T]] 19 | * }}} 20 | * 21 | * @param futures 22 | * @tparam T 23 | * @return 24 | */ 25 | def futureOptionListToFuture[T]( 26 | futures: Seq[Future[Option[T]]] 27 | )(implicit executionContext: ExecutionContext) = 28 | Future.sequence(futures).map(_.flatten) 29 | } 30 | -------------------------------------------------------------------------------- /scala-utils/src/main/scala/io/github/dreamylost/other/ObjectUtils.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost.other 3 | 4 | /** 5 | * 对象工具 6 | * 7 | * @author 梦境迷离 8 | * @time 2019-08-18 9 | * @version v2.0 10 | */ 11 | object ObjectUtils { 12 | 13 | /** 14 | * 获取对象的所有属性转换为Map 15 | * 16 | * @param obj 17 | * @param excludes 18 | * @return 19 | */ 20 | def properties(obj: Any, excludes: String*): Map[String, Any] = { 21 | val fields = obj.getClass.getDeclaredFields.map { field => 22 | field.setAccessible(true) 23 | field.getName -> field.get(obj) 24 | }.toMap 25 | if (excludes.nonEmpty) { 26 | fields.filterNot(entry => excludes.contains(entry._1)) 27 | } else { 28 | fields 29 | } 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /scala-utils/src/main/scala/io/github/dreamylost/undertow/ExceptionHandler.scala: -------------------------------------------------------------------------------- 1 | /* Licensed under Apache-2.0 (C) All Contributors */ 2 | package io.github.dreamylost.undertow 3 | 4 | import io.undertow.server.HttpServerExchange 5 | 6 | /** 7 | * undertow 异常处理器 8 | * 9 | * @author 梦境迷离 10 | * @time 2019-08-18 11 | * @version v1.0 12 | */ 13 | trait ExceptionHandler { 14 | 15 | def handleException(exchange: HttpServerExchange, cause: Throwable): Unit 16 | 17 | } 18 | -------------------------------------------------------------------------------- /scala3-examples/README.md: -------------------------------------------------------------------------------- 1 | ## sbt project compiled with Dotty 2 | 3 | ### Usage 4 | 5 | This is a normal sbt project, you can compile code with `sbt compile` and run it 6 | with `sbt run`, `sbt console` will start a Dotty REPL. 7 | 8 | For more information on the sbt-dotty plugin, see the 9 | [dotty-example-project](https://github.com/lampepfl/dotty-example-project/blob/master/README.md). 10 | 11 | The codes copy from [scala3-example-project](https://github.com/scala/scala3-example-project). -------------------------------------------------------------------------------- /scala3-examples/build.sbt: -------------------------------------------------------------------------------- 1 | val dottyVersion = "3.0.0-RC2" 2 | 3 | lazy val root = project 4 | .in(file(".")) 5 | .settings( 6 | name := "scala3-examples", 7 | version := "0.1.0", 8 | 9 | scalaVersion := dottyVersion, 10 | 11 | libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % "test" 12 | ) 13 | -------------------------------------------------------------------------------- /scala3-examples/project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=1.5.0-RC2 -------------------------------------------------------------------------------- /scala3-examples/project/plugins.sbt: -------------------------------------------------------------------------------- 1 | addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % "0.4.6") -------------------------------------------------------------------------------- /scala3-examples/src/main/scala/Main.scala: -------------------------------------------------------------------------------- 1 | 2 | object Main { 3 | 4 | def main(args: Array[String]): Unit = { 5 | println("Hello world!") 6 | println(msg) 7 | } 8 | 9 | def msg = "I was compiled by dotty :)" 10 | 11 | } 12 | -------------------------------------------------------------------------------- /scala3-examples/src/main/scala/io/github/dreamylost/scala3/ParameterUntupling.scala: -------------------------------------------------------------------------------- 1 | package io.github.dreamylost.scala3 2 | 3 | /** 4 | * 参数解组: https://dotty.epfl.ch/docs/reference/other-new-features/parameter-untupling.html 5 | */ 6 | object ParameterUntupling: 7 | 8 | def test(): Unit = 9 | val xs: List[String] = List("d", "o", "t", "t", "y") 10 | 11 | /** 12 | * Current behaviour in Scala 2.12.2 : 13 | * error: missing parameter type 14 | * Note: The expected type requires a one-argument function accepting a 2-Tuple. 15 | * Consider a pattern matching anonymous function, `{ case (s, i) => ... }` 16 | */ 17 | xs.zipWithIndex.map((s, i) => println(s"$i: $s")) 18 | 19 | -------------------------------------------------------------------------------- /scala3-examples/src/main/scala/io/github/dreamylost/scala3/TraitParams.scala: -------------------------------------------------------------------------------- 1 | package io.github.dreamylost.scala3 2 | 3 | /** 4 | * 特质参数: https://dotty.epfl.ch/docs/reference/other-new-features/trait-parameters.html 5 | */ 6 | object TraitParams: 7 | 8 | trait Base(val msg: String) 9 | class A extends Base("Hello") 10 | class B extends Base("Dotty!") 11 | 12 | // 联合类型只存在于Scala3中,所以不可能意外地用Scala 2编译。 13 | private def printMessages(msgs: (A | B)*) = println(msgs.map(_.msg).mkString(" ")) 14 | 15 | def test(): Unit = 16 | printMessages(new A, new B) 17 | 18 | // 检查类路径的合理性:如果scala3 jar不存在,则不会运行。 19 | val x: Int => Int = identity 20 | x(1) 21 | 22 | -------------------------------------------------------------------------------- /scala3-examples/src/main/scala/io/github/dreamylost/scala3/TypeLambdas.scala: -------------------------------------------------------------------------------- 1 | package io.github.dreamylost.scala3 2 | 3 | /** 4 | * 类型 Lambdas: https://dotty.epfl.ch/docs/reference/new-types/type-lambdas.html 5 | */ 6 | object TypeLambdas: 7 | 8 | type T[+X, Y] = Map[Y, X] 9 | 10 | type Tuple = [X] =>> (X, X) 11 | 12 | def test(): Unit = 13 | val m: T[String, Int] = Map(1 -> "1") 14 | println(m) 15 | 16 | val tuple: Tuple[String] = ("a", "b") 17 | println(tuple) 18 | 19 | -------------------------------------------------------------------------------- /scala3-examples/src/test/scala/Test1.scala: -------------------------------------------------------------------------------- 1 | import org.junit.Test 2 | import org.junit.Assert._ 3 | 4 | class Test1 { 5 | @Test def t1(): Unit = { 6 | assertEquals("I was compiled by dotty :)", Main.msg) 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'cs-summary-reflection' 2 | include 'java-examples' 3 | include 'scala-utils' 4 | include 'java-leetcode' 5 | include 'scala-leetcode' 6 | include 'scala-examples' 7 | include 'kotlin-leetcode' 8 | include 'kotlin-study' 9 | include 'java-utils' 10 | --------------------------------------------------------------------------------