├── .gitignore ├── CCF ├── Accept │ ├── 201312-3 最大的矩形.cpp │ ├── 201403-3 命令行选项.cpp │ ├── 201403-4 无线网络.cpp │ ├── 201512-3 画图.cpp │ ├── 201512-4 送货.cpp │ ├── 201604-3 路径解析.cpp │ ├── 201604-4 游戏.cpp │ ├── 201609-4 交通规划.cpp │ ├── 201609-4-test.txt │ ├── 201609-4-交通规划 new.cpp │ ├── 201612-1 中间数.cpp │ ├── 201612-2 工资计算.cpp │ ├── 201612-3 权限查询.cpp │ ├── 201612-4 压缩编码.cpp │ ├── 201703-1 分蛋糕.cpp │ ├── 201703-1-test.txt │ ├── 201703-2 学生排队.cpp │ ├── 201703-2-test.txt │ ├── 201703-4 地铁修建.cpp │ ├── 201709-4 通信网络.cpp │ ├── 201709-4-test.txt │ └── 201712-4 行车路线.cpp ├── CCF 阶段总结.docx ├── DevCpp 个人配置.docx └── OJ 刷题总结.docx ├── HiHoCoder ├── 1562 小Hi的钟表.cpp ├── 1563 影院座位.cpp ├── 1564 同步H公司的终端.cpp ├── 1566 皇室成员的名字.cpp ├── 1567 偶树的切分.cpp ├── 1568 不一定合法括号序列.cpp ├── 1572 小Hi与花盆 .cpp ├── 1599 逃离迷宫4.cpp ├── 1601 最大得分.cpp ├── offer24.pptx ├── offer25.pptx ├── offer26.pptx ├── offer45.pptx └── readme.md ├── Leetcode ├── AAA │ ├── BFS.docx │ ├── Backtracking.docx │ ├── Binary Search.docx │ ├── BitManipulation.docx │ ├── Divide and Conquer.docx │ ├── Dynamic Programming.docx │ ├── Error Case.docx │ ├── Greedy.docx │ ├── Hash or Two Pointer.docx │ ├── Heap.docx │ ├── LibFuncation.docx │ ├── Other.docx │ ├── String.docx │ ├── funcation.txt │ ├── ~$namic Programming.docx │ ├── ~WRL2331.tmp │ └── 排列_next_permutation.docx ├── AC Java │ └── LeetcodeSolution │ │ ├── .idea │ │ ├── compiler.xml │ │ ├── description.html │ │ ├── encodings.xml │ │ ├── misc.xml │ │ ├── modules.xml │ │ ├── vcs.xml │ │ └── workspace.xml │ │ ├── 227. Basic Calculator II │ │ └── src │ │ │ └── Main.java │ │ ├── 640. Solve the Equation │ │ └── src │ │ │ └── Solution.java │ │ ├── 647. Palindromic Substrings │ │ └── src │ │ │ └── Main.java │ │ ├── 657. Judge Route Circle │ │ └── src │ │ │ └── Solution.java │ │ ├── 664. Strange Printer │ │ └── src │ │ │ └── Solution.java │ │ ├── 672. Bulb Switcher II │ │ └── src │ │ │ └── Solution.java │ │ ├── 673. Number of Longest Increasing Subsequence │ │ └── src │ │ │ └── Solution.java │ │ ├── 674. Longest Continuous Increasing Subsequence │ │ └── src │ │ │ └── Solution.java │ │ ├── 675. Cut Off Trees for Golf Event │ │ └── src │ │ │ └── Solution.java │ │ ├── 678. Valid Parenthesis String │ │ └── src │ │ │ └── Main.java │ │ ├── 686. Repeated String Match │ │ └── src │ │ │ └── Solution.java │ │ ├── 687. Longest Univalue Path │ │ └── src │ │ │ └── Solution.java │ │ ├── 688. Knight Probability in Chessboard │ │ └── src │ │ │ └── Solution.java │ │ ├── 689. Maximum Sum of 3 Non-Overlapping Subarrays │ │ └── src │ │ │ └── Solution.java │ │ ├── 696. Count Binary Substrings │ │ └── src │ │ │ └── Main.java │ │ ├── 722. Remove Comments │ │ └── src │ │ │ └── Main.java │ │ ├── 767. Reorganize String │ │ └── src │ │ │ └── Main.java │ │ ├── out │ │ └── production │ │ │ ├── 227. Basic Calculator II │ │ │ ├── Main.class │ │ │ ├── Solution.class │ │ │ └── Solution2.class │ │ │ ├── 647. Palindromic Substrings │ │ │ ├── Main.class │ │ │ └── Solution.class │ │ │ ├── 678. Valid Parenthesis String │ │ │ ├── Main.class │ │ │ └── Solution.class │ │ │ ├── 696. Count Binary Substrings │ │ │ ├── Main.class │ │ │ └── Solution.class │ │ │ ├── 722. Remove Comments │ │ │ ├── Main.class │ │ │ └── Solution.class │ │ │ ├── 767. Reorganize String │ │ │ ├── Main.class │ │ │ └── Solution.class │ │ │ └── LeetcodeSolution │ │ │ └── Main.class │ │ └── src │ │ └── Main.java ├── AC Python │ ├── 1. Two Sum.py │ ├── 105. Construct Binary Tree from Preorder and Inorder Traversal.py │ ├── 106. Construct Binary Tree from Inorder and Postorder Traversal.py │ ├── 116. Populating Next Right Pointers in Each Node.py │ ├── 125. Valid Palindrome.py │ ├── 136. Single Number.py │ ├── 144. Binary Tree Preorder Traversal.py │ ├── 151. Reverse Words in a String.py │ ├── 165. Compare Version Numbers.py │ ├── 167. Two Sum II - Input array is sorted.py │ ├── 173. Binary Search Tree Iterator.py │ ├── 18. 4Sum.py │ ├── 187. Repeated DNA Sequences.py │ ├── 202. Happy Number.py │ ├── 205. Isomorphic Strings.py │ ├── 217. Contains Duplicate.py │ ├── 222. Count Complete Tree Nodes.py │ ├── 226. Invert Binary Tree.py │ ├── 230. Kth Smallest Element in a BST.py │ ├── 235. Lowest Common Ancestor of a Binary Search Tree.py │ ├── 257. Binary Tree Paths.py │ ├── 274. H-Index.py │ ├── 278. First Bad Version.py │ ├── 299. Bulls and Cows.py │ ├── 35. Search Insert Position.py │ ├── 36. Valid Sudoku.py │ ├── 43. Multiply Strings.py │ ├── 49. Group Anagrams.py │ ├── 59. Spiral Matrix II.py │ ├── 655. Print Binary Tree.py │ ├── 66. Plus One.py │ ├── 67. Add Binary.py │ ├── 676. Implement Magic Dictionary.py │ ├── 68. Text Justification.py │ ├── 680. Valid Palindrome II.py │ ├── 69. Sqrt(x).py │ ├── 692. Top K Frequent Words.py │ ├── 709. To Lower Case.py │ ├── 71. Simplify Path.py │ ├── 74. Search a 2D Matrix.py │ ├── 791. Custom Sort String.py │ ├── 816. Ambiguous Coordinates.py │ ├── 831. Masking Personal Information.py │ ├── 842. Split Array into Fibonacci Sequence.py │ ├── 848. Shifting Letters.py │ ├── 856. Score of Parentheses.py │ ├── 886. Possible Bipartition.py │ ├── 887. Super Egg Drop.py │ ├── 890. Find and Replace Pattern.py │ ├── 94. Binary Tree Inorder Traversal.py │ ├── 95. Unique Binary Search Trees II.py │ └── 98. Validate Binary Search Tree.py ├── Array │ ├── 396. Rotate Function.cpp │ ├── 41. First Missing Positive.cpp │ ├── 561. Array Partition I.cpp │ ├── 566. Reshape the Matrix.cpp │ ├── 581. Shortest Unsorted Continuous Subarray.cpp │ ├── 605. Can Place Flowers.cpp │ ├── 624. Maximum Distance in Arrays.cpp │ ├── 628. Maximum Product of Three Numbers.cpp │ └── 674. Longest Continuous Increasing Subsequence.cpp ├── BFS │ ├── 101. Symmetric Tree.cpp │ ├── 102. Binary Tree Level Order Traversal.cpp │ ├── 103. Binary Tree Zigzag Level Order Traversal.cpp │ ├── 107. Binary Tree Level Order Traversal II.cpp │ ├── 111. Minimum Depth of Binary Tree.cpp │ ├── 126. Word Ladder II.cpp │ ├── 130. Surrounded Regions.cpp │ ├── 133. Clone Graph.cpp │ ├── 199. Binary Tree Right Side View.cpp │ ├── 200. Number of Islands.cpp │ ├── 207. Course Schedule.cpp │ ├── 210. Course Schedule II.cpp │ ├── 310. Minimum Height Trees.cpp │ ├── 407. Trapping Rain Water II.cpp │ ├── 417. Pacific Atlantic Water Flow.cpp │ ├── 488. Zuma Game.cpp │ ├── 513. Find Bottom Left Tree Value.cpp │ ├── 515. Find Largest Value in Each Tree Row.cpp │ ├── 529. Minesweeper.cpp │ ├── 542. 01 Matrix.cpp │ └── 675. Cut Off Trees for Golf Event.cpp ├── Backtracking │ ├── 131. Palindrome Partitioning.cpp │ ├── 140. Word Break II.cpp │ ├── 17. Letter Combinations of a Phone Number.cpp │ ├── 212. Word Search II.cpp │ ├── 216. Combination Sum III.cpp │ ├── 39. Combination Sum.cpp │ ├── 40. Combination Sum II.cpp │ ├── 401. Binary Watch.cpp │ ├── 46. Permutations.cpp │ ├── 47. Permutations II.cpp │ ├── 51. N-Queens.cpp │ ├── 52. N-Queens II.cpp │ ├── 526. Beautiful Arrangement.cpp │ ├── 60. Permutation Sequence.cpp │ ├── 77. Combinations.cpp │ ├── 78. Subsets.cpp │ ├── 79. Word Search.cpp │ ├── 89. Gray Code.cpp │ ├── 90. Subsets II.cpp │ └── 93. Restore IP Addresses.cpp ├── Binary Search │ ├── 153. Find Minimum in Rotated Sorted Array.cpp │ ├── 154. Find Minimum in Rotated Sorted Array II.cpp │ ├── 162. Find Peak Element.cpp │ ├── 33. Search in Rotated Sorted Array.cpp │ ├── 378. Kth Smallest Element in a Sorted Matrix.cpp │ ├── 57. Insert Interval.cpp │ └── 611. Valid Triangle Number.cpp ├── Bit Manipulation │ ├── 318. Maximum Product of Word Lengths.cpp │ ├── 397. Integer Replacement.cpp │ └── 477. Total Hamming Distance.cpp ├── DFS │ ├── 100. Same Tree.cpp │ ├── 104. Maximum Depth of Binary Tree.cpp │ ├── 108. Convert Sorted Array to Binary Search Tree.cpp │ ├── 110. Balanced Binary Tree.cpp │ ├── 112. Path Sum.cpp │ ├── 113. Path Sum II.cpp │ ├── 117. Populating Next Right Pointers in Each Node II.cpp │ ├── 124. Binary Tree Maximum Path Sum.cpp │ ├── 129. Sum Root to Leaf Numbers.cpp │ ├── 329. Longest Increasing Path in a Matrix.cpp │ ├── 332. Reconstruct Itinerary.cpp │ ├── 337. House Robber III.cpp │ ├── 37. Sudoku Solver.cpp │ ├── 394. Decode String.cpp │ ├── 399. Evaluate Division.cpp │ ├── 473. Matchsticks to Square.cpp │ ├── 491. Increasing Subsequences.cpp │ ├── 514. Freedom Trail.cpp │ ├── 547. Friend Circles.cpp │ ├── 606. Construct String from Binary Tree.cpp │ ├── 623. Add One Row to Tree.cpp │ ├── 637. Average of Levels in Binary Tree.cpp │ ├── 687. Longest Univalue Path.cpp │ ├── 863. All Nodes Distance K in Binary Tree.cpp │ ├── 98. Validate Binary Search Tree.cpp │ └── 99. Recover Binary Search Tree.cpp ├── Design │ ├── 146. LRU Cache.cpp │ ├── 297. Serialize and Deserialize Binary Tree.cpp │ ├── 355. Design Twitter.cpp │ ├── 588. Design In-Memory File System.cpp │ ├── 631. Design Excel Sum Formula.cpp │ ├── 635. Design Log Storage System.cpp │ └── 640. Solve the Equation.cpp ├── Divide and Conquer │ ├── 169. Majority Element.cpp │ ├── 215. Kth Largest Element in an Array.cpp │ ├── 240. Search a 2D Matrix II.cpp │ ├── 241. Different Ways to Add Parentheses.cpp │ ├── 315. Count of Smaller Numbers After Self.cpp │ ├── 327. Count of Range Sum.cpp │ ├── 4. Median of Two Sorted Arrays.cpp │ └── 493. Reverse Pairs.cpp ├── Dynamic Programming │ ├── 10. Regular Expression Matching.cpp │ ├── 114 Flatten Binary Tree to Linked List.cpp │ ├── 115. Distinct Subsequences.cpp │ ├── 120. Triangle.cpp │ ├── 121. Best Time to Buy and Sell Stock.cpp │ ├── 122. Best Time to Buy and Sell Stock II.cpp │ ├── 123. Best Time to Buy and Sell Stock III.cpp │ ├── 132. Palindrome Partitioning II.cpp │ ├── 139. Word Break.cpp │ ├── 152. Maximum Product Subarray.cpp │ ├── 174. Dungeon Game.cpp │ ├── 188. Best Time to Buy and Sell Stock IV.cpp │ ├── 198. House Robber.cpp │ ├── 213. House Robber II.cpp │ ├── 221. Maximal Square.cpp │ ├── 264. Ugly Number II.cpp │ ├── 279. Perfect Squares.cpp │ ├── 300. Longest Increasing Subsequence.cpp │ ├── 303. Range Sum Query - Immutable.cpp │ ├── 304. Range Sum Query 2D - Immutable.cpp │ ├── 309. Best Time to Buy and Sell Stock with Cooldown.cpp │ ├── 312. Burst Balloons.cpp │ ├── 32. Longest Valid Parentheses.cpp │ ├── 321. Create Maximum Number.cpp │ ├── 322 Coin Change.cpp │ ├── 338. Counting Bits.cpp │ ├── 343. Integer Break.cpp │ ├── 354. Russian Doll Envelopes.cpp │ ├── 357. Count Numbers with Unique Digits.cpp │ ├── 363. Max Sum of Rectangle No Larger Than K.cpp │ ├── 368. Largest Divisible Subset.cpp │ ├── 375. Guess Number Higher or Lower II.cpp │ ├── 376. Wiggle Subsequence.cpp │ ├── 377. Combination Sum IV.cpp │ ├── 392. Is Subsequence.cpp │ ├── 403. Frog Jump.cpp │ ├── 410. Split Array Largest Sum.cpp │ ├── 413. Arithmetic Slices.cpp │ ├── 416. Partition Equal Subset Sum.cpp │ ├── 44. Wildcard Matching.cpp │ ├── 446. Arithmetic Slices II - Subsequence.cpp │ ├── 464. Can I Win.cpp │ ├── 466. Count The Repetitions.cpp │ ├── 467. Unique Substrings in Wraparound String.cpp │ ├── 472. Concatenated Words.cpp │ ├── 474. Ones and Zeroes.cpp │ ├── 486. Predict the Winner.cpp │ ├── 494. Target Sum.cpp │ ├── 5. Longest Palindromic Substring.cpp │ ├── 516. Longest Palindromic Subsequence.cpp │ ├── 517. Super Washing Machines.cpp │ ├── 523. Continuous Subarray Sum.cpp │ ├── 53. Maximum Subarray.cpp │ ├── 546. Remove Boxes.cpp │ ├── 552. Student Attendance Record II.cpp │ ├── 562. Longest Line of Consecutive One in Matrix.cpp │ ├── 576. Out of Boundary Paths.cpp │ ├── 583. Delete Operation for Two Strings.cpp │ ├── 600. Non-negative Integers without Consecutive Ones.cpp │ ├── 62. Unique Paths.cpp │ ├── 629. K Inverse Pairs Array.cpp │ ├── 63. Unique Paths II.cpp │ ├── 634. Find the Derangement of An Array.cpp │ ├── 638. Shopping Offers.cpp │ ├── 639. Decode Ways II.cpp │ ├── 64. Minimum Path Sum.cpp │ ├── 664. Strange Printer.cpp │ ├── 673. Number of Longest Increasing Subsequence.cpp │ ├── 688. Knight Probability in Chessboard.cpp │ ├── 689. Maximum Sum of 3 Non-Overlapping Subarrays.cpp │ ├── 70. Climbing Stairs.cpp │ ├── 714. Best Time to Buy and Sell Stock with Transaction Fee.cpp │ ├── 72. Edit Distance.cpp │ ├── 730. Count Different Palindromic Subsequences.cpp │ ├── 741. Cherry Pickup.cpp │ ├── 787. Cheapest Flights Within K Stops.cpp │ ├── 818. Race Car.cpp │ ├── 85. Maximal Rectangle.cpp │ ├── 86 Partition List.cpp │ ├── 87. Scramble String.cpp │ ├── 91. Decode Ways.cpp │ ├── 96. Unique Binary Search Trees.cpp │ └── 97. Interleaving String.cpp ├── Greedy │ ├── 134. Gas Station.cpp │ ├── 135. Candy.cpp │ ├── 316. Remove Duplicate Letters.cpp │ ├── 321. Create Maximum Number.cpp │ ├── 330. Patching Array.cpp │ ├── 402. Remove K Digits.cpp │ ├── 406. Queue Reconstruction by Height.cpp │ ├── 435. Non-overlapping Intervals.cpp │ ├── 45. Jump Game II.cpp │ ├── 452. Minimum Number of Arrows to Burst Balloons.cpp │ ├── 455. Assign Cookies.cpp │ ├── 502. IPO.cpp │ ├── 55. Jump Game.cpp │ ├── 573. Squirrel Simulation.cpp │ ├── 621. Task Scheduler.cpp │ ├── 630. Course Schedule III.cpp │ └── 632. Smallest Range.cpp ├── Hash │ ├── 149. Max Points on a Line.cpp │ ├── 166. Fraction to Recurring Decimal.cpp │ ├── 209. Minimum Size Subarray Sum.cpp │ ├── 3. Longest Substring Without Repeating Characters.cpp │ ├── 30. Substring with Concatenation of All Words.cpp │ ├── 380. Insert Delete GetRandom O(1).cpp │ ├── 381. Insert Delete GetRandom O(1) - Duplicates allowed.cpp │ ├── 438. Find All Anagrams in a String.cpp │ ├── 451. Sort Characters By Frequency.cpp │ ├── 454. 4Sum II.cpp │ ├── 463. Island Perimeter.cpp │ ├── 500. Keyboard Row.cpp │ ├── 508. Most Frequent Subtree Sum.cpp │ ├── 525. Contiguous Array.cpp │ ├── 535. Encode and Decode TinyURL.cpp │ ├── 554. Brick Wall.cpp │ ├── 560. Subarray Sum Equals K.cpp │ ├── 575. Distribute Candies.cpp │ ├── 594. Longest Harmonious Subsequence.cpp │ ├── 599. Minimum Index Sum of Two Lists.cpp │ ├── 609. Find Duplicate File in System.cpp │ ├── 76. Minimum Window Substring.cpp │ └── 895. Maximum Frequency Stack.cpp ├── Heap │ ├── 218. The Skyline Problem.cpp │ ├── 23. Merge k Sorted Lists.cpp │ ├── 239. Sliding Window Maximum.cpp │ ├── 295. Find Median from Data Stream.cpp │ ├── 347. Top K Frequent Elements.cpp │ ├── 378. Kth Smallest Element in a Sorted Matrix.cpp │ └── priority_queue │ │ ├── priority_queue.cpp │ │ ├── test1.cpp │ │ ├── test11.cpp │ │ ├── test2.cpp │ │ └── test3.cpp ├── LinkList │ ├── 109. Convert Sorted List to Binary Search Tree.cpp │ ├── 138. Copy List with Random Pointer.cpp │ ├── 141. Linked List Cycle.cpp │ ├── 142. Linked List Cycle II.cpp │ ├── 143. Reorder List.cpp │ ├── 147. Insertion Sort List.cpp │ ├── 148. Sort List.cpp │ ├── 160. Intersection of Two Linked Lists.cpp │ ├── 19. Remove Nth Node From End of List.cpp │ ├── 21. Merge Two Sorted Lists.cpp │ ├── 234. Palindrome Linked List.cpp │ ├── 237. Delete Node in a Linked List.cpp │ ├── 24. Swap Nodes in Pairs.cpp │ ├── 25. Reverse Nodes in k-Group.cpp │ ├── 445. Add Two Numbers II.cpp │ ├── 61. Rotate List.cpp │ ├── 82. Remove Duplicates from Sorted List II.cpp │ ├── 83. Remove Duplicates from Sorted List.cpp │ └── 92. Reverse Linked List II.cpp ├── Math │ ├── 224. Basic Calculator.cpp │ ├── 233. Number of Digit One.cpp │ ├── 592. Fraction Addition and Subtraction.cpp │ ├── 593. Valid Square.cpp │ ├── 598. Range Addition II.cpp │ ├── 625. Minimum Factorization.cpp │ ├── 633. Sum of Square Numbers.cpp │ ├── 672. Bulb Switcher II.cpp │ ├── 770. Basic Calculator IV.cpp │ └── 858. Mirror Reflection.cpp ├── Other │ ├── 128. Longest Consecutive Sequence.cpp │ ├── 145. Binary Tree Postorder Traversal.cpp │ ├── 164. Maximum Gap.cpp │ ├── 563. Binary Tree Tilt.cpp │ ├── 572. Subtree of Another Tree.cpp │ ├── 582. Kill Process.cpp │ ├── 587. Erect the Fence.cpp │ ├── 617. Merge Two Binary Trees.cpp │ └── 84. Largest Rectangle in Histogram.cpp ├── Segment Tree │ └── 307. Range Sum Query - Mutable.cpp ├── String │ ├── 12. Integer to Roman.cpp │ ├── 13. Roman to Integer.cpp │ ├── 14. Longest Common Prefix.cpp │ ├── 20. Valid Parentheses.cpp │ ├── 214. Shortest Palindrome.cpp │ ├── 22. Generate Parentheses.cpp │ ├── 273. Integer to English Words.cpp │ ├── 28. Implement strStr().cpp │ ├── 344. Reverse String.cpp │ ├── 345. Reverse Vowels of a String.cpp │ ├── 38. Count and Say.cpp │ ├── 383. Ransom Note.cpp │ ├── 385. Mini Parser.cpp │ ├── 434. Number of Segments in a String.cpp │ ├── 459. Repeated Substring Pattern.cpp │ ├── 468. Validate IP Address.cpp │ ├── 520. Detect Capital.cpp │ ├── 537. Complex Number Multiplication.cpp │ ├── 539. Minimum Time Difference.cpp │ ├── 541. Reverse String II.cpp │ ├── 551. Student Attendance Record I.cpp │ ├── 553. Optimal Division.cpp │ ├── 556. Next Greater Element III.cpp │ ├── 557. Reverse Words in a String III.cpp │ ├── 564. Find the Closest Palindrome.cpp │ ├── 58. Length of Last Word.cpp │ ├── 686. Repeated String Match.cpp │ ├── 770. Basic Calculator IV.cpp │ └── 8. String to Integer (atoi).cpp ├── Trie │ ├── 208. Implement Trie (Prefix Tree).cpp │ ├── 211. Add and Search Word - Data structure design.cpp │ ├── 336. Palindrome Pairs.cpp │ └── 648. Replace Words.cpp ├── Two Pointers │ ├── 42. Trapping Rain Water.cpp │ └── 75. Sort Colors.cpp └── Union Find │ └── 685. Redundant Connection II.cpp ├── Nowcoder ├── 2019-校招实习-编程集合 │ ├── 2018-今日头条校招后端第二批-1.cpp │ ├── 2018-今日头条校招后端第二批-2.cpp │ ├── 2018-今日头条校招后端第二批-4.cpp │ ├── 2018-阿里巴巴-编程测评.cpp │ ├── 2019-360实习-1.cpp │ ├── 2019-360实习-2.cpp │ ├── 2019-今日头条实习第三批-1.cpp │ ├── 2019-今日头条实习第三批-2.cpp │ ├── 2019-华为实习-1-easy.cpp │ ├── 2019-华为实习-2-easy.cpp │ ├── 2019-华为实习-3-classic-01背包.cpp │ ├── 2019-网易实习-1-牛牛找工作.cpp │ ├── 2019-网易实习-2-被3整除.cpp │ ├── 2019-网易实习-3-安置路灯.cpp │ ├── 2019-网易实习-4-迷路的牛牛-.cpp │ ├── 2019-网易实习-5-数对.cpp │ ├── 2019-网易实习-6-牛牛的闹钟.cpp │ ├── 2019-网易实习-7-矩形重叠.cpp │ ├── 2019-网易实习-8-牛牛的背包问题.cpp │ ├── 2019-网易校招-1-表达式求值.cpp │ ├── 2019-网易校招-2-瞌睡.cpp │ ├── 2019-网易校招-3-小易的字典.cpp │ ├── 2019-腾讯校招-1-区间LCM-数论.cpp │ ├── 2019-腾讯校招-2-最重要的城市-DFS.cpp │ └── 2019-腾讯校招-3-数论.cpp ├── DS Problem Set -- ZheJiangUniversity │ ├── 4-1 单链表逆转.cpp │ ├── 4-10 二分查找.cpp │ ├── 4-11 先序输出叶节点.cpp │ ├── 4-2 顺序表操作集.cpp │ ├── 4-8 求二叉树高度.cpp │ ├── 4-9 二叉树的遍历.cpp │ ├── 5-10 公路村村通.cpp │ ├── 5-11 关键活动.cpp │ ├── 5-12 排序.cpp │ ├── 5-13 工龄统计.cpp │ ├── 5-14 电话聊天狂人.cpp │ ├── 5-16 一元多项式求导.cpp │ ├── 5-23 修理牧场.cpp │ ├── 5-23 还原二叉树.cpp │ ├── 5-24 树种统计.cpp │ ├── 5-28 搜索树判断.cpp │ ├── 5-3 树的同构.cpp │ ├── 5-31 判断笛卡尔树.cpp │ ├── 5-33 地下迷宫探索.cpp │ ├── 5-34 任务调度的合理性.cpp │ ├── 5-35 城市间紧急救援.cpp │ ├── 5-36 社交网络中结点的“重要性”计算.cpp │ ├── 5-37 模拟Excel排序.cpp │ ├── 5-39 魔法优惠券.cpp │ ├── 5-4 是否同一颗二叉搜索树.cpp │ ├── 5-40 奥运排行榜.cpp │ ├── 5-49 打印学生选课清单.cpp │ ├── 5-5 堆中的路径.cpp │ ├── 5-50 畅通工程之局部最小花费问题.cpp │ ├── 5-51 两个有序链表的合并.cpp │ ├── 5-53 两个有序序列的中位数.cpp │ ├── 5-6 列出连通集.cpp │ ├── 5-7 六度空间.cpp │ ├── 5-8 哈利波特的考试.cpp │ └── 5-9 旅游规划.cpp ├── Easy Problems Set │ ├── BFS 地牢逃脱.cpp │ ├── DP 构造回文.cpp │ ├── Hash 下厨房.cpp │ ├── Math 分苹果.cpp │ ├── Math 星际穿越.cpp │ ├── Math 素数对.cpp │ ├── Recursively+Reverse 魔法币.cpp │ ├── String 相反数.cpp │ ├── String 藏宝图.cpp │ └── note.cpp ├── Fundmental Practice │ ├── A+B and C (64bit).cpp │ ├── A+B in Hogwarts.cpp │ ├── Acute Stroke.cpp │ ├── All Roads Lead to Rome.cpp │ ├── Are They Equal.cpp │ ├── Be Unique.cpp │ ├── Build A Binary Search Tree.cpp │ ├── Complete Binary Search Tree.cpp │ ├── Consecutive Factors.cpp │ ├── Count PAT's.cpp │ ├── Counting Ones.cpp │ ├── Course List for Student.cpp │ ├── Dating.cpp │ ├── Deduplication on a Linked List.cpp │ ├── Favorite Color Stripe.cpp │ ├── Find Coins.cpp │ ├── Find More Coins.cpp │ ├── Forwards on Weibo.cpp │ ├── Gas Station (30).cpp │ ├── Graduate Admission.cpp │ ├── Hashing.cpp │ ├── Head of a Gang.cpp │ ├── Hello World for U.cpp │ ├── Highest Price In Supply Chain.cpp │ ├── Insert or Merge.cpp │ ├── Insertion or Heap Sort.cpp │ ├── Kuchiguse.cpp │ ├── Linked List Sorting.cpp │ ├── Longest Symmetric String.cpp │ ├── Mice and Rice.cpp │ ├── Mooncake.cpp │ ├── PAT Judge.cpp │ ├── Path of Equal Weight.cpp │ ├── Pop Sequence.cpp │ ├── Prime Factors.cpp │ ├── Public Bike Management.cpp │ ├── Recover the Smallest Number.cpp │ ├── Reversing Linked List.cpp │ ├── Root of AVL Tree.cpp │ ├── Set Similarity.cpp │ ├── Sharing.cpp │ ├── Shopping in Mars.cpp │ ├── Shuffling Machine.cpp │ ├── Sort with Swap 0.cpp │ ├── Speech Patterns.cpp │ ├── Stack.cpp │ ├── String Subtraction.cpp │ ├── Student List for Course.cpp │ ├── Table Tennis.cpp │ ├── Talent and Virtue.cpp │ ├── The Black Hole of Numbers.cpp │ ├── The Dominant Color.cpp │ ├── The Largest Generation.cpp │ ├── To Buy or Not to Buy.cpp │ └── To Fill or Not to Fill.cpp └── Middle Problems Set │ ├── BinarySearch 分田地.cpp │ ├── DP or Cursively 放苹果.cpp │ ├── DP 合唱.cpp │ ├── DP 合唱团.cpp │ ├── Tree 游历魔法王国.cpp │ ├── 字典序.cpp │ └── 异或.cpp ├── OJ ├── Google │ ├── Kickstart 2018 Round E A.cpp │ ├── Kickstart 2018 Round E B.cpp │ ├── Kickstart 2018 Round H A.cpp │ └── Kickstart 2018 Round H B.cpp ├── HOJ 4460 最短路径 │ └── HOJ 4460.cpp ├── HRBUST 1050 次短路径 │ └── HRBUST 1050.cpp ├── OJ解题报告.docx ├── POJ 1151 线段树 │ └── POJ 1151.cpp ├── POJ 1182 并查集 │ └── POJ 1182 .cpp ├── POJ 1330 LCA │ └── POJ 1330.cpp ├── POJ 1568 极大极小值剪枝 │ ├── POJ 1568.cpp │ └── input.txt ├── POJ 1611 并查集 │ └── POJ 1611.cpp ├── POJ 2299 树状数组 │ └── POJ 2299 .cpp ├── POJ 3259 spfa最短路径 │ └── POJ 3259.cpp ├── POJ 3468 线段树 │ └── POJ 3468 .cpp ├── TOJ │ ├── 1001 染色 │ │ └── 1001 染色.cpp │ ├── 1002 正方形 │ │ └── 1002 正方形.cpp │ ├── 1003 美人鱼 │ │ └── 1003 美人鱼.cpp │ ├── 1005 多米诺骨牌 │ │ └── 1005 多米诺骨牌.cpp │ ├── 1010 还原二叉树 │ │ └── 1010 还原二叉树.cpp │ ├── 1011 Chessboard │ │ └── 1011 Chessboard.cpp │ ├── 1013 Escape │ │ ├── 1013 Escape.cpp │ │ └── test.txt │ ├── 1015 Height │ │ └── 1015 Height.cpp │ ├── 1018 Sum │ │ └── 1018 Sum.cpp │ ├── 1019 Trouble │ │ └── 1019 Trouble.cpp │ ├── 1021 Appointment │ │ └── 1021 Appointment.cpp │ ├── 1035 最优比赛策略 │ │ └── 1035 最优比赛策略.cpp │ ├── 1049 Parity of Combination I │ │ └── 1049 Parity of Combination I.cpp │ ├── 1050 XOR XOR XOR │ │ └── 1050 XOR XOR XOR....cpp │ ├── 1051 Space Trip │ │ └── 1051 Space Trip.cpp │ ├── 1056 旅游安排 │ │ └── 1056 旅游安排.cpp │ └── TOJ2017 │ │ ├── 1 │ │ └── 1.cpp │ │ ├── 2 │ │ └── 2.cpp │ │ ├── 3 │ │ └── 3.cpp │ │ └── 4 │ │ └── 4.cpp ├── UESTC 92 LCA │ └── UESTC 92.cpp └── ZOJ 3794 最短路径 │ └── ZOJ 3794.cpp ├── README.md └── book ├── acm-book.pdf ├── acm-icpc-template.pdf ├── mine ├── algorithm Template.docx ├── algorithm Template.pdf ├── dijkstra.cpp ├── lca.cpp ├── spfa.cpp ├── spfa1.cpp ├── sspath.cpp ├── treeArray.cpp └── unionSet.cpp ├── 动态规划加速原理之四边形不等式.pdf ├── 北京大学--DP.pdf ├── 最短路径spfa算法.pdf ├── 线段树与树状数组.pdf ├── 背包问题.pdf └── 贪心算法.pdf /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | Algorithm Course/ 3 | Data structure/ 4 | Leetcode/*.iml 5 | Leetcode/*/*.iml 6 | Leetcode/*/*/*.iml 7 | Leetcode/*/*/*/*.iml 8 | Leetcode/Java Solution/LeetcodeSolution/.idea/ 9 | Leetcode/Java Solution/LeetcodeSolution/out 10 | Leetcode/*.exe 11 | Leetcode/*/*.exe 12 | Leetcode/*/*/*.exe 13 | Leetcode/*/*/*/*.exe 14 | Leetcode/.vscode/settings.json 15 | -------------------------------------------------------------------------------- /CCF/Accept/201312-3 最大的矩形.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/CCF/Accept/201312-3 最大的矩形.cpp -------------------------------------------------------------------------------- /CCF/Accept/201403-3 命令行选项.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/CCF/Accept/201403-3 命令行选项.cpp -------------------------------------------------------------------------------- /CCF/Accept/201403-4 无线网络.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/CCF/Accept/201403-4 无线网络.cpp -------------------------------------------------------------------------------- /CCF/Accept/201512-4 送货.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/CCF/Accept/201512-4 送货.cpp -------------------------------------------------------------------------------- /CCF/Accept/201604-3 路径解析.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/CCF/Accept/201604-3 路径解析.cpp -------------------------------------------------------------------------------- /CCF/Accept/201604-4 游戏.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/CCF/Accept/201604-4 游戏.cpp -------------------------------------------------------------------------------- /CCF/Accept/201609-4-test.txt: -------------------------------------------------------------------------------- 1 | 4 5 2 | 1 2 4 3 | 1 3 5 4 | 2 3 2 5 | 2 4 3 6 | 3 4 2 -------------------------------------------------------------------------------- /CCF/Accept/201612-1 中间数.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define NMAX 1000 7 | int main() 8 | { 9 | //freopen("test1.txt", "r", stdin); 10 | int n[NMAX + 1]; 11 | int N; 12 | while (scanf("%d", &N) != EOF) { 13 | int i; 14 | for (i = 1; i <= N; ++i) 15 | scanf("%d", n + i); 16 | sort(n + 1, n + N + 1); 17 | 18 | int ans = -1; 19 | int mid = n[(N + 1) / 2]; 20 | int count = 0; 21 | for (i = (N + 1) / 2; i && n[i] == mid; --i) 22 | ++count; 23 | for (i = (N + 1) / 2 + 1; i <= N && n[i] == mid; ++i) 24 | ++count; 25 | if (count % 2 == N % 2) 26 | ans = mid; 27 | printf("%d\n", ans); 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /CCF/Accept/201612-2 工资计算.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | using namespace std; 4 | 5 | #define startFare 3500 6 | 7 | const int rt[] = {0, 3,10,20,25,30,35,45 }; 8 | const int bt[] = { 0,1500,4500,9000,35000,55000,80000,300000 }; 9 | int at[8] = { 0 }; 10 | 11 | int main() 12 | { 13 | //freopen("test2.txt", "r", stdin); 14 | 15 | int i; 16 | for (i = 1; i <= 7; ++i) 17 | at[i] = at[i - 1] + (bt[i] - bt[i - 1])*(100 - rt[i]) / 100; 18 | 19 | int T; 20 | while (scanf("%d", &T) != EOF) { 21 | if (T <= 3500) { 22 | printf("%d\n", T); 23 | continue; 24 | } 25 | 26 | int t = T - startFare; 27 | for (i = 1; i <= 7 && t >= at[i]; ++i); 28 | 29 | int A = bt[i - 1] + (t - at[i - 1]) * 100 / (100 - rt[i]); 30 | printf("%d\n", A + startFare); 31 | } 32 | return 0; 33 | } -------------------------------------------------------------------------------- /CCF/Accept/201703-1 分蛋糕.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // freopen("201703-1-test.txt", "r", stdin); 6 | int n,k; 7 | int a, sum; 8 | int ans; 9 | while(scanf("%d %d", &n, &k)!=EOF) { 10 | ans=0; 11 | sum=0; 12 | while(n--) { 13 | scanf("%d", &a); 14 | sum+=a; 15 | if(sum>=k) { 16 | ++ans; 17 | sum=0; 18 | } 19 | } 20 | ans+=sum!=0; 21 | cout< 2 | #include 3 | using namespace std; 4 | #define N 1001 5 | 6 | int main() { 7 | // freopen("201703-2-test.txt", "r", stdin); 8 | 9 | int n,m; 10 | int p,q; 11 | int i; 12 | 13 | int que[N]; 14 | while(scanf("%d %d", &n, &m)!=EOF) { 15 | for (i=1; i<=n; ++i) { 16 | que[i]=i; 17 | } 18 | while(m--) { 19 | scanf("%d %d", &p, &q); 20 | for (i=1; i<=n; ++i) { 21 | if(que[i]==p) 22 | break; 23 | } 24 | if(q<0) { 25 | memcpy(que+i+q+1, que+i+q, sizeof(int)*(-q)); 26 | que[i+q]=p; 27 | } else { 28 | memcpy(que+i, que+i+1, sizeof(int)*(q)); 29 | que[i+q]=p; 30 | } 31 | } 32 | for(i=1; i<=n; ++i) { 33 | printf("%d", que[i]); 34 | if(i!=n) 35 | printf(" "); 36 | } 37 | } 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /CCF/Accept/201703-2-test.txt: -------------------------------------------------------------------------------- 1 | 8 2 | 3 3 | 3 2 4 | 8 -3 5 | 3 -2 -------------------------------------------------------------------------------- /CCF/Accept/201709-4-test.txt: -------------------------------------------------------------------------------- 1 | 4 4 2 | 1 2 3 | 1 3 4 | 2 4 5 | 3 4 6 | 7 | 5 4 8 | 1 3 9 | 2 3 10 | 3 4 11 | 3 5 12 | -------------------------------------------------------------------------------- /CCF/Accept/201712-4 行车路线.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/CCF/Accept/201712-4 行车路线.cpp -------------------------------------------------------------------------------- /CCF/CCF 阶段总结.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/CCF/CCF 阶段总结.docx -------------------------------------------------------------------------------- /CCF/DevCpp 个人配置.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/CCF/DevCpp 个人配置.docx -------------------------------------------------------------------------------- /CCF/OJ 刷题总结.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/CCF/OJ 刷题总结.docx -------------------------------------------------------------------------------- /HiHoCoder/1562 小Hi的钟表.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | int T,t; 8 | int h,m,s; 9 | int ss; 10 | 11 | const int cst=12*3600; 12 | cin>>T; 13 | while(T--){ 14 | cin>>h>>m>>s; 15 | cin>>t; 16 | t%=cst; 17 | ss=h*3600+m*60+s; 18 | ss=(ss+t)%cst; 19 | 20 | double a1=(ss*1.0/cst)*360; 21 | double a2=(ss%3600/3600.0)*360; 22 | double a3=abs(a1-a2); 23 | double ans=a3>180?360-a3:a3; 24 | printf("%.4lf\n",ans); 25 | } 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /HiHoCoder/1572 小Hi与花盆 .cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/HiHoCoder/1572 小Hi与花盆 .cpp -------------------------------------------------------------------------------- /HiHoCoder/1599 逃离迷宫4.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/HiHoCoder/1599 逃离迷宫4.cpp -------------------------------------------------------------------------------- /HiHoCoder/1601 最大得分.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/HiHoCoder/1601 最大得分.cpp -------------------------------------------------------------------------------- /HiHoCoder/offer24.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/HiHoCoder/offer24.pptx -------------------------------------------------------------------------------- /HiHoCoder/offer25.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/HiHoCoder/offer25.pptx -------------------------------------------------------------------------------- /HiHoCoder/offer26.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/HiHoCoder/offer26.pptx -------------------------------------------------------------------------------- /HiHoCoder/offer45.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/HiHoCoder/offer45.pptx -------------------------------------------------------------------------------- /HiHoCoder/readme.md: -------------------------------------------------------------------------------- 1 | # hihoCoder problems solving ! 2 | 3 | In this repository, I will push my answer or other's solving of problems in hihoCoder OJ ! 4 | 5 | files included: 6 | # cpp 7 | # pptx 8 | # word or txt -------------------------------------------------------------------------------- /Leetcode/AAA/BFS.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AAA/BFS.docx -------------------------------------------------------------------------------- /Leetcode/AAA/Backtracking.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AAA/Backtracking.docx -------------------------------------------------------------------------------- /Leetcode/AAA/Binary Search.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AAA/Binary Search.docx -------------------------------------------------------------------------------- /Leetcode/AAA/BitManipulation.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AAA/BitManipulation.docx -------------------------------------------------------------------------------- /Leetcode/AAA/Divide and Conquer.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AAA/Divide and Conquer.docx -------------------------------------------------------------------------------- /Leetcode/AAA/Dynamic Programming.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AAA/Dynamic Programming.docx -------------------------------------------------------------------------------- /Leetcode/AAA/Error Case.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AAA/Error Case.docx -------------------------------------------------------------------------------- /Leetcode/AAA/Greedy.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AAA/Greedy.docx -------------------------------------------------------------------------------- /Leetcode/AAA/Hash or Two Pointer.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AAA/Hash or Two Pointer.docx -------------------------------------------------------------------------------- /Leetcode/AAA/Heap.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AAA/Heap.docx -------------------------------------------------------------------------------- /Leetcode/AAA/LibFuncation.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AAA/LibFuncation.docx -------------------------------------------------------------------------------- /Leetcode/AAA/Other.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AAA/Other.docx -------------------------------------------------------------------------------- /Leetcode/AAA/String.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AAA/String.docx -------------------------------------------------------------------------------- /Leetcode/AAA/funcation.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AAA/funcation.txt -------------------------------------------------------------------------------- /Leetcode/AAA/~$namic Programming.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AAA/~$namic Programming.docx -------------------------------------------------------------------------------- /Leetcode/AAA/~WRL2331.tmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AAA/~WRL2331.tmp -------------------------------------------------------------------------------- /Leetcode/AAA/排列_next_permutation.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AAA/排列_next_permutation.docx -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/.idea/description.html: -------------------------------------------------------------------------------- 1 | Simple Java application that includes a class with main() method -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/657. Judge Route Circle/src/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean judgeCircle(String moves) { 3 | int x = 0, y = 0; 4 | for (char move: moves.toCharArray()) { 5 | if (move == 'U') y--; 6 | else if (move == 'D') y++; 7 | else if (move == 'L') x--; 8 | else if (move == 'R') x++; 9 | } 10 | return x == 0 && y == 0; 11 | } 12 | } -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/664. Strange Printer/src/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | int[][] memo; 3 | public int strangePrinter(String s) { 4 | int N = s.length(); 5 | memo = new int[N][N]; 6 | return dp(s, 0, N - 1); 7 | } 8 | public int dp(String s, int i, int j) { 9 | if (i > j) return 0; 10 | if (memo[i][j] == 0) { 11 | int ans = dp(s, i+1, j) + 1; 12 | for (int k = i+1; k <= j; ++k) 13 | if (s.charAt(k) == s.charAt(i)) 14 | ans = Math.min(ans, dp(s, i, k-1) + dp(s, k+1, j)); 15 | memo[i][j] = ans; 16 | } 17 | return memo[i][j]; 18 | } 19 | } -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/672. Bulb Switcher II/src/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int flipLights(int n, int m) { 3 | if(m==0) return 1; 4 | if(n==1) return 2; 5 | if(n==2&&m==1) return 3; 6 | if(n==2) return 4; 7 | if(m==1) return 4; 8 | if(m==2) return 7; 9 | if(m>=3) return 8; 10 | return 8; 11 | } 12 | } -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/674. Longest Continuous Increasing Subsequence/src/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findLengthOfLCIS(int[] nums) { 3 | int ans = 0, anchor = 0; 4 | for (int i = 0; i < nums.length; ++i) { 5 | if (i > 0 && nums[i-1] >= nums[i]) anchor = i; 6 | ans = Math.max(ans, i - anchor + 1); 7 | } 8 | return ans; 9 | } 10 | } -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/678. Valid Parenthesis String/src/Main.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AC Java/LeetcodeSolution/678. Valid Parenthesis String/src/Main.java -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/686. Repeated String Match/src/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int repeatedStringMatch(String A, String B) { 3 | int q = 1; 4 | StringBuilder s = new StringBuilder(A); 5 | for (; s.length() < B.length(); ++q) { 6 | s.append(A); 7 | } 8 | if (s.indexOf(B) >= 0) 9 | return q; 10 | if (s.append(A).indexOf(B) >= 0) 11 | return q + 1; 12 | return -1; 13 | } 14 | } -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/out/production/227. Basic Calculator II/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AC Java/LeetcodeSolution/out/production/227. Basic Calculator II/Main.class -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/out/production/227. Basic Calculator II/Solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AC Java/LeetcodeSolution/out/production/227. Basic Calculator II/Solution.class -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/out/production/227. Basic Calculator II/Solution2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AC Java/LeetcodeSolution/out/production/227. Basic Calculator II/Solution2.class -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/out/production/647. Palindromic Substrings/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AC Java/LeetcodeSolution/out/production/647. Palindromic Substrings/Main.class -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/out/production/647. Palindromic Substrings/Solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AC Java/LeetcodeSolution/out/production/647. Palindromic Substrings/Solution.class -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/out/production/678. Valid Parenthesis String/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AC Java/LeetcodeSolution/out/production/678. Valid Parenthesis String/Main.class -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/out/production/678. Valid Parenthesis String/Solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AC Java/LeetcodeSolution/out/production/678. Valid Parenthesis String/Solution.class -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/out/production/696. Count Binary Substrings/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AC Java/LeetcodeSolution/out/production/696. Count Binary Substrings/Main.class -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/out/production/696. Count Binary Substrings/Solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AC Java/LeetcodeSolution/out/production/696. Count Binary Substrings/Solution.class -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/out/production/722. Remove Comments/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AC Java/LeetcodeSolution/out/production/722. Remove Comments/Main.class -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/out/production/722. Remove Comments/Solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AC Java/LeetcodeSolution/out/production/722. Remove Comments/Solution.class -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/out/production/767. Reorganize String/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AC Java/LeetcodeSolution/out/production/767. Reorganize String/Main.class -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/out/production/767. Reorganize String/Solution.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AC Java/LeetcodeSolution/out/production/767. Reorganize String/Solution.class -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/out/production/LeetcodeSolution/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/AC Java/LeetcodeSolution/out/production/LeetcodeSolution/Main.class -------------------------------------------------------------------------------- /Leetcode/AC Java/LeetcodeSolution/src/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String[] args) { 4 | System.out.println("Hello World!"); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /Leetcode/AC Python/1. Two Sum.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def twoSum(self, nums, target): 3 | """ 4 | :type nums: List[int] 5 | :type target: int 6 | :rtype: List[int] 7 | """ 8 | if len(nums) <= 1: 9 | return False 10 | buff_dict = {} 11 | for i in range(len(nums)): 12 | if nums[i] in buff_dict: 13 | return [buff_dict[nums[i]], i] 14 | else: 15 | buff_dict[target - nums[i]] = i -------------------------------------------------------------------------------- /Leetcode/AC Python/105. Construct Binary Tree from Preorder and Inorder Traversal.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | class TreeNode: 3 | def __init__(self, x): 4 | self.val = x 5 | self.left = None 6 | self.right = None 7 | 8 | class Solution: 9 | def buildTree(self, preorder, inorder): 10 | if inorder: 11 | ind = inorder.index(preorder.pop(0)) 12 | root = TreeNode(inorder[ind]) 13 | root.left = self.buildTree(preorder, inorder[0:ind]) 14 | root.right = self.buildTree(preorder, inorder[ind+1:]) 15 | return root -------------------------------------------------------------------------------- /Leetcode/AC Python/106. Construct Binary Tree from Inorder and Postorder Traversal.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | 8 | class Solution: 9 | # @param inorder, a list of integers 10 | # @param postorder, a list of integers 11 | # @return a tree node 12 | # 12:00 13 | def buildTree(self, inorder, postorder): 14 | if not inorder or not postorder: 15 | return None 16 | 17 | root = TreeNode(postorder.pop()) 18 | inorderIndex = inorder.index(root.val) 19 | 20 | root.right = self.buildTree(inorder[inorderIndex+1:], postorder) 21 | root.left = self.buildTree(inorder[:inorderIndex], postorder) 22 | 23 | return root -------------------------------------------------------------------------------- /Leetcode/AC Python/116. Populating Next Right Pointers in Each Node.py: -------------------------------------------------------------------------------- 1 | # Definition for binary tree with next pointer. 2 | # class TreeLinkNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | # self.next = None 8 | 9 | class Solution: 10 | # @param root, a tree link node 11 | # @return nothing 12 | def connect(self, root): 13 | if not root: return 14 | while root.left: 15 | cur = root.left 16 | prev = None 17 | while root: 18 | if prev: prev.next = root.left 19 | root.left.next = root.right 20 | prev = root.right 21 | root = root.next 22 | root = cur -------------------------------------------------------------------------------- /Leetcode/AC Python/125. Valid Palindrome.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, s): 3 | l, r = 0, len(s)-1 4 | while l < r: 5 | while l < r and not s[l].isalnum(): 6 | l += 1 7 | while l < r and not s[r].isalnum(): 8 | r -= 1 9 | if s[l].lower() != s[r].lower(): 10 | return False 11 | l += 1 12 | r -= 1 13 | return True 14 | 15 | import string 16 | 17 | class Solution1: 18 | def isPalindrome(self, s): 19 | # Better: 20 | translates = str.maketrans("", "", string.punctuation + " ") 21 | s = s.translate(translates).lower() 22 | 23 | return s == s[::-1] 24 | -------------------------------------------------------------------------------- /Leetcode/AC Python/136. Single Number.py: -------------------------------------------------------------------------------- 1 | import operator 2 | 3 | class Solution: 4 | def singleNumber(self, nums): 5 | dict={} 6 | for num in nums: 7 | dict[num]=dict.get(num, 0)+1 8 | for key, val in dict.items(): 9 | if val==1: 10 | return key 11 | 12 | def singleNumber1(self, nums): 13 | return reduce(operator.xor, nums) -------------------------------------------------------------------------------- /Leetcode/AC Python/144. Binary Tree Preorder Traversal.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | class TreeNode: 3 | def __init__(self, x): 4 | self.val = x 5 | self.left = None 6 | self.right = None 7 | 8 | class Solution: 9 | def preorderTraversal(self, root): 10 | """ 11 | :type root: TreeNode 12 | :rtype: List[int] 13 | """ 14 | ret = [] 15 | stack = [root] 16 | while stack: 17 | node = stack.pop() 18 | if node: 19 | ret.append(node.val) 20 | stack.append(node.right) 21 | stack.append(node.left) 22 | return ret -------------------------------------------------------------------------------- /Leetcode/AC Python/151. Reverse Words in a String.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseWords(self, s): 3 | return " ".join(s.strip().split()[::-1]) -------------------------------------------------------------------------------- /Leetcode/AC Python/165. Compare Version Numbers.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def compareVersion(self, version1, version2): 3 | versions1 = [int(v) for v in version1.split(".")] 4 | versions2 = [int(v) for v in version2.split(".")] 5 | for i in range(max(len(versions1),len(versions2))): 6 | v1 = versions1[i] if i < len(versions1) else 0 7 | v2 = versions2[i] if i < len(versions2) else 0 8 | if v1 > v2: 9 | return 1 10 | elif v1 < v2: 11 | return -1; 12 | return 0; -------------------------------------------------------------------------------- /Leetcode/AC Python/167. Two Sum II - Input array is sorted.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoSum(self, numbers, target): 3 | for i in range(len(numbers)): 4 | l, r = i+1, len(numbers)-1 5 | tmp = target - numbers[i] 6 | while l <= r: 7 | mid = l + (r-l)//2 8 | if numbers[mid] == tmp: 9 | return [i+1, mid+1] 10 | elif numbers[mid] < tmp: 11 | l = mid+1 12 | else: 13 | r = mid-1 -------------------------------------------------------------------------------- /Leetcode/AC Python/173. Binary Search Tree Iterator.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node 2 | # class TreeNode(object): 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | 8 | 9 | class BSTIterator(object): 10 | def __init__(self, root): 11 | self.stack = [] 12 | while root: 13 | self.stack.append(root) 14 | root = root.left 15 | 16 | def hasNext(self): 17 | return len(self.stack) > 0 18 | 19 | def next(self): 20 | node = self.stack.pop() 21 | x = node.right 22 | while x: 23 | self.stack.append(x) 24 | x = x.left 25 | return node.val 26 | 27 | 28 | # Your BSTIterator will be called like this: 29 | # i, v = BSTIterator(root), [] 30 | # while i.hasNext(): v.append(i.next()) 31 | -------------------------------------------------------------------------------- /Leetcode/AC Python/187. Repeated DNA Sequences.py: -------------------------------------------------------------------------------- 1 | import collections 2 | 3 | class Solution: 4 | def findRepeatedDnaSequences(self, s): 5 | dict=collections.defaultdict(int) 6 | for i in range(len(s)): 7 | dict[s[i:i+10]]+=1 8 | return [key for key, value in dict.items() if value>1] -------------------------------------------------------------------------------- /Leetcode/AC Python/202. Happy Number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isHappy(self, n): 3 | mem = set() 4 | while n != 1: 5 | n = sum([int(i) ** 2 for i in str(n)]) 6 | if n in mem: 7 | return False 8 | else: 9 | mem.add(n) 10 | else: 11 | return True -------------------------------------------------------------------------------- /Leetcode/AC Python/205. Isomorphic Strings.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | 3 | class Solution: 4 | def isIsomorphic(self, s, t): 5 | d1, d2=defaultdict(int), defaultdict(int) 6 | for i in range(len(s)): 7 | if d1[ord(s[i])]!=d2[ord(t[i])]: 8 | return False 9 | d1[ord(s[i])]=d2[ord(t[i])]=i+1 10 | return True -------------------------------------------------------------------------------- /Leetcode/AC Python/217. Contains Duplicate.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def containsDuplicate(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: bool 6 | """ 7 | return len(nums) != len(set(nums)) -------------------------------------------------------------------------------- /Leetcode/AC Python/226. Invert Binary Tree.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | class TreeNode: 3 | def __init__(self, x): 4 | self.val = x 5 | self.left = None 6 | self.right = None 7 | 8 | class Solution: 9 | def invertTree(self, root): 10 | if root: 11 | root.left, root.right = self.invertTree(root.right), self.invertTree(root.left) 12 | return root -------------------------------------------------------------------------------- /Leetcode/AC Python/230. Kth Smallest Element in a BST.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | class TreeNode: 3 | def __init__(self, x): 4 | self.val = x 5 | self.left = None 6 | self.right = None 7 | 8 | class Solution(object): 9 | def kthSmallest(self, root, k): 10 | """ 11 | :type root: TreeNode 12 | :type k: int 13 | :rtype: int 14 | """ 15 | count = [] 16 | self.helper(root, count) 17 | return count[k-1] 18 | 19 | def helper(self, node, count): 20 | if not node: 21 | return 22 | 23 | self.helper(node.left, count) 24 | count.append(node.val) 25 | self.helper(node.right, count) -------------------------------------------------------------------------------- /Leetcode/AC Python/235. Lowest Common Ancestor of a Binary Search Tree.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode(object): 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | 8 | class Solution(object): 9 | def lowestCommonAncestor(self, root, p, q): 10 | """ 11 | :type root: TreeNode 12 | :type p: TreeNode 13 | :type q: TreeNode 14 | :rtype: TreeNode 15 | """ 16 | if(p.val>q.val): 17 | return self.lowestCommonAncestor(root, q, p) 18 | if root.val>q.val: 19 | return self.lowestCommonAncestor(root.left, p, q) 20 | if root.val'+path 6 | for kid in (root.left, root.right) if kid 7 | for path in self.binaryTreePaths(kid)] or [str(root.val)] -------------------------------------------------------------------------------- /Leetcode/AC Python/274. H-Index.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hIndex(self, citations): 3 | n = len(citations) 4 | citeCount = [0] * (n+1) 5 | for c in citations: 6 | if c >= n: 7 | citeCount[n] += 1 8 | else: 9 | citeCount[c] += 1 10 | 11 | i = n-1 12 | while i >= 0: 13 | citeCount[i] += citeCount[i+1] 14 | if citeCount[i+1] >= i+1: 15 | return i+1 16 | i -= 1 17 | return 0 -------------------------------------------------------------------------------- /Leetcode/AC Python/278. First Bad Version.py: -------------------------------------------------------------------------------- 1 | # The isBadVersion API is already defined for you. 2 | # @param version, an integer 3 | # @return a bool 4 | # def isBadVersion(version): 5 | 6 | class Solution(object): 7 | def firstBadVersion(self, n): 8 | """ 9 | :type n: int 10 | :rtype: int 11 | """ 12 | r = n-1 13 | l = 0 14 | while(l<=r): 15 | mid = l + (r-l)//2 16 | if isBadVersion(mid)==False: 17 | l = mid+1 18 | else: 19 | r = mid-1 20 | return l -------------------------------------------------------------------------------- /Leetcode/AC Python/299. Bulls and Cows.py: -------------------------------------------------------------------------------- 1 | import operator 2 | 3 | class Solution: 4 | def getHint(self, secret, guess): 5 | bulls=sum(map(operator.eq, secret, guess)) 6 | both=sum( min(secret.count(x), guess.count(x)) for x in set(guess)) 7 | return '%dA%dB' % (bulls, both-bulls) -------------------------------------------------------------------------------- /Leetcode/AC Python/35. Search Insert Position.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def searchInsert(self, nums, target): 3 | return sorted(nums + [target]).index(target) -------------------------------------------------------------------------------- /Leetcode/AC Python/36. Valid Sudoku.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isValidSudoku(self, board): 3 | seen=sum(([(c, i), (j, c), (i//3, j//3, c)] 4 | for i in range(9) for j in range(9) 5 | for c in [board[i][j]] if c!='.'), []) 6 | return len(seen)==len(set(seen)) -------------------------------------------------------------------------------- /Leetcode/AC Python/43. Multiply Strings.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def multiply(self, num1, num2): 3 | """ 4 | :type num1: str 5 | :type num2: str 6 | :rtype: str 7 | """ 8 | ans = [0]*(len(num1)+len(num2)) 9 | pos = len(ans)-1 10 | 11 | for n1 in reversed(num1): 12 | tpos=pos 13 | for n2 in reversed(num2): 14 | ans[tpos] += int(n1) * int(n2) 15 | ans[tpos-1]+=ans[tpos]//10 16 | ans[tpos]%=10 17 | tpos-=1 18 | pos-=1 19 | 20 | tpos=0 21 | while tpos x: 9 | r = (r + x//r) // 2 10 | return r -------------------------------------------------------------------------------- /Leetcode/AC Python/692. Top K Frequent Words.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def topKFrequent(self, words, k): 3 | """ 4 | :type words: List[str] 5 | :type k: int 6 | :rtype: List[str] 7 | """ 8 | coll = collections.Counter(words) 9 | heap = [(-freq, word) for word, freq in coll.items()] 10 | heapq.heapify(heap) 11 | return [heapq.heappop(heap)[1] for _ in range(k)] -------------------------------------------------------------------------------- /Leetcode/AC Python/709. To Lower Case.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def toLowerCase(self, str): 3 | return "".join(chr(ord(c) + 32) if "A" <= c <= "Z" else c for c in str) 4 | # class Solution: 5 | # def toLowerCase(self, str): 6 | # return str.lower() -------------------------------------------------------------------------------- /Leetcode/AC Python/71. Simplify Path.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def simplifyPath(self, path): 3 | stack = [] 4 | for token in path.split('/'): 5 | if token in ('', '.'): 6 | pass 7 | elif token == '..': 8 | if stack: 9 | stack.pop() 10 | else: 11 | stack.append(token) 12 | return '/' + '/'.join(stack) 13 | -------------------------------------------------------------------------------- /Leetcode/AC Python/74. Search a 2D Matrix.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @param matrix, a list of lists of integers 3 | # @param target, an integer 4 | # @return a boolean 5 | # 8:21 6 | def searchMatrix(self, matrix, target): 7 | if not matrix or target is None: 8 | return False 9 | 10 | rows, cols = len(matrix), len(matrix[0]) 11 | low, high = 0, rows * cols - 1 12 | 13 | while low <= high: 14 | mid = (low + high) // 2 15 | num = matrix[mid // cols][mid % cols] 16 | 17 | if num == target: 18 | return True 19 | elif num < target: 20 | low = mid + 1 21 | else: 22 | high = mid - 1 23 | 24 | return False -------------------------------------------------------------------------------- /Leetcode/AC Python/791. Custom Sort String.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def customSortString(self, S, T): 3 | """ 4 | :type S: str 5 | :type T: str 6 | :rtype: str 7 | """ 8 | l = [] 9 | for i in S: 10 | l.append(i*T.count(i)) 11 | for i in T: 12 | if i not in S: 13 | l.append(i) 14 | return ''.join(l) 15 | -------------------------------------------------------------------------------- /Leetcode/AC Python/816. Ambiguous Coordinates.py: -------------------------------------------------------------------------------- 1 | import itertools 2 | 3 | class Solution: 4 | def ambiguousCoordinates(self, S): 5 | S = S[1:-1] 6 | 7 | def f(S): 8 | if not S or len(S) > 1 and S[0] == S[-1] == '0': 9 | return [] 10 | if S[-1] == '0': 11 | return [S] 12 | if S[0] == '0': 13 | return [S[0]+'.'+S[1:]] 14 | return [S]+[S[:i]+'.'+S[i:] for i in range(1, len(S))] 15 | return ['(%s, %s)' % (a, b) for i in range(len(S)) for a, b in itertools.product(f(S[:i]), f(S[i:]))] 16 | 17 | s = Solution() 18 | for a in s.ambiguousCoordinates("123") : 19 | print (a) 20 | -------------------------------------------------------------------------------- /Leetcode/AC Python/831. Masking Personal Information.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maskPII(self, S): 3 | at=S.find('@') 4 | if at>=0: 5 | return (S[0]+'*'*5 +S[at-1:]).lower() 6 | S="".join(i for i in S if i.isdigit()) 7 | return ["", "+*-", "+**-", "+***-"][len(S)-10]+"***-***-"+S[-4:] -------------------------------------------------------------------------------- /Leetcode/AC Python/848. Shifting Letters.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def shiftingLetters(self, S, shifts): 3 | for i in range(len(shifts)-1)[::-1]: 4 | shifts[i] += shifts[i+1] 5 | return "".join(chr((ord(c)-97+s) % 26+97) for (c, s) in zip(S, shifts)) 6 | -------------------------------------------------------------------------------- /Leetcode/AC Python/856. Score of Parentheses.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def scoreOfParentheses(self, S): 3 | stack, res = [], 0 4 | for c in S: 5 | if c == "(": 6 | stack.append(0) 7 | else: 8 | last = stack.pop() 9 | add = last and 2 * last or 1 10 | if stack: 11 | stack[-1] += add 12 | else: 13 | res += add 14 | return res -------------------------------------------------------------------------------- /Leetcode/AC Python/886. Possible Bipartition.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def possibleBipartition(self, N, dislikes): 3 | """ 4 | :type N: int 5 | :type dislikes: List[List[int]] 6 | :rtype: bool 7 | """ 8 | graph = collections.defaultdict(list) 9 | for u, v in dislikes: 10 | graph[u].append(v) 11 | graph[v].append(u) 12 | 13 | color = {} 14 | def dfs(node, c=0): 15 | if node in color: 16 | return color[node]==c 17 | color[node]=c 18 | return all(dfs(nei, c^1) for nei in graph[node]) 19 | return all(dfs(node) for node in range(1, N+1) if node not in color) 20 | -------------------------------------------------------------------------------- /Leetcode/AC Python/890. Find and Replace Pattern.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findAndReplacePattern(self, words, pattern): 3 | """ 4 | :type words: List[str] 5 | :type pattern: str 6 | :rtype: List[str] 7 | """ 8 | def fun(w): 9 | d = {} 10 | for c in w: 11 | d[c] = d.get(c, len(d)) 12 | return "".join(chr(d[c]+97) for c in w) 13 | return [w for w in words if fun(w) == fun(pattern)] 14 | -------------------------------------------------------------------------------- /Leetcode/AC Python/94. Binary Tree Inorder Traversal.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | 8 | class Solution: 9 | # recursively 10 | def inorderTraversal(self, root): 11 | res = [] 12 | self.helper(root, res) 13 | return res 14 | 15 | def helper(self, root, res): 16 | if root: 17 | self.helper(root.left, res) 18 | res.append(root.val) 19 | self.helper(root.right, res) -------------------------------------------------------------------------------- /Leetcode/AC Python/98. Validate Binary Search Tree.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | 8 | class Solution(object): 9 | def isValidBST(self, root, lessThan = float('inf'), largerThan = float('-inf')): 10 | if not root: 11 | return True 12 | if root.val <= largerThan or root.val >= lessThan: 13 | return False 14 | return self.isValidBST(root.left, min(lessThan, root.val), largerThan) and \ 15 | self.isValidBST(root.right, lessThan, max(root.val, largerThan)) 16 | -------------------------------------------------------------------------------- /Leetcode/Array/396. Rotate Function.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxRotateFunction(vector& A) { 4 | int F = 0, sum = 0, n = A.size(); 5 | for (int i = 0; i < n; ++i){ 6 | F += i * A[i]; 7 | sum += A[i]; 8 | } 9 | int m = F; 10 | for (int i = n - 1; i >= 0; --i){ 11 | F = F + sum - n * A[i]; 12 | m = max(m, F); 13 | } 14 | return m; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Leetcode/Array/41. First Missing Positive.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Array/41. First Missing Positive.cpp -------------------------------------------------------------------------------- /Leetcode/Array/561. Array Partition I.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int arrayPairSum(vector& nums) { 4 | sort(nums.begin(),nums.end()); 5 | int ans=0; 6 | for(int i=0;i> matrixReshape(vector>& nums, int r, int c) { 4 | int m = nums.size(), n = nums[0].size(), o = m * n; 5 | if (r * c != o) return nums; 6 | vector> res(r, vector(c, 0)); 7 | for (int i = 0; i < o; i++) res[i / c][i % c] = nums[i / n][i % n]; 8 | return res; 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /Leetcode/Array/581. Shortest Unsorted Continuous Subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findUnsortedSubarray(vector& nums) { 4 | if(nums.empty()) 5 | return 0; 6 | vectortnum(nums); 7 | sort(tnum.begin(),tnum.end()); 8 | int begin=0,end=tnum.size()-1; 9 | for(; beginend) 12 | return 0; 13 | for(; end>=0&&tnum[end]==nums[end];) 14 | --end; 15 | return end-begin+1; 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Leetcode/Array/605. Can Place Flowers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canPlaceFlowers(vector& flowerbed, int n) { 4 | for(int i=0; n&&i0&&flowerbed[i-1]||i+1& flowerbed, int n) { 20 | flowerbed.insert(flowerbed.begin(),0); 21 | flowerbed.push_back(0); 22 | for(int i = 1; i < flowerbed.size()-1; ++i) { 23 | if(flowerbed[i-1] + flowerbed[i] + flowerbed[i+1] == 0) { 24 | --n; 25 | ++i; 26 | } 27 | } 28 | return n <=0; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Leetcode/Array/628. Maximum Product of Three Numbers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maximumProduct(vector& nums) { 4 | sort(nums.begin(),nums.end()); 5 | int size=nums.size(); 6 | int ans1=nums[0]*nums[1]*nums[size-1]; 7 | int ans2=nums[size-1]*nums[size-2]*nums[size-3]; 8 | return max(ans1,ans2); 9 | } 10 | }; 11 | -------------------------------------------------------------------------------- /Leetcode/Array/674. Longest Continuous Increasing Subsequence.cpp: -------------------------------------------------------------------------------- 1 | //very easy problem 2 | class Solution { 3 | public: 4 | int findLengthOfLCIS(vector& nums) { 5 | int ans=0; 6 | int tem=0,i; 7 | for(i=0; inums[i-1]) 9 | ans=max(ans, ++tem); 10 | else 11 | tem=1; 12 | } 13 | return ans; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Leetcode/BFS/101. Symmetric Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | bool isSymmetric(TreeNode* root) { 13 | if(!root) 14 | return true; 15 | return isSyme(root->left,root->right); 16 | } 17 | private: 18 | bool isSyme(TreeNode*r1,TreeNode*r2) { 19 | if (r1==NULL&&r2==NULL) 20 | return true; 21 | else if (r1==NULL&&r2||r1&&r2==NULL) 22 | return false; 23 | else { 24 | if(r1->val==r2->val) 25 | if (isSyme(r1->left,r2->right)&&isSyme(r1->right,r2->left)) 26 | return true; 27 | return false; 28 | } 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Leetcode/BFS/111. Minimum Depth of Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | int minDepth(TreeNode *root) { 13 | if(!root) return 0; 14 | if(!root->left) return 1 + minDepth(root->right); 15 | if(!root->right) return 1 + minDepth(root->left); 16 | return 1+min(minDepth(root->left),minDepth(root->right)); 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Leetcode/BFS/126. Word Ladder II.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/BFS/126. Word Ladder II.cpp -------------------------------------------------------------------------------- /Leetcode/BFS/199. Binary Tree Right Side View.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/BFS/199. Binary Tree Right Side View.cpp -------------------------------------------------------------------------------- /Leetcode/BFS/207. Course Schedule.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/BFS/207. Course Schedule.cpp -------------------------------------------------------------------------------- /Leetcode/BFS/210. Course Schedule II.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/BFS/210. Course Schedule II.cpp -------------------------------------------------------------------------------- /Leetcode/BFS/407. Trapping Rain Water II.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/BFS/407. Trapping Rain Water II.cpp -------------------------------------------------------------------------------- /Leetcode/BFS/417. Pacific Atlantic Water Flow.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/BFS/417. Pacific Atlantic Water Flow.cpp -------------------------------------------------------------------------------- /Leetcode/BFS/488. Zuma Game.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/BFS/488. Zuma Game.cpp -------------------------------------------------------------------------------- /Leetcode/BFS/513. Find Bottom Left Tree Value.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | int findBottomLeftValue(TreeNode* root) { 13 | queueq; 14 | q.push(root); 15 | int leftValue=0; 16 | int size=root?1:0; 17 | while(size) { 18 | for (int i=0; ival; 23 | if (ptr->left) 24 | q.push(ptr->left); 25 | if (ptr->right) 26 | q.push(ptr->right); 27 | } 28 | size=q.size(); 29 | } 30 | return leftValue; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /Leetcode/BFS/529. Minesweeper.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/BFS/529. Minesweeper.cpp -------------------------------------------------------------------------------- /Leetcode/BFS/675. Cut Off Trees for Golf Event.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/BFS/675. Cut Off Trees for Golf Event.cpp -------------------------------------------------------------------------------- /Leetcode/Backtracking/140. Word Break II.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Backtracking/140. Word Break II.cpp -------------------------------------------------------------------------------- /Leetcode/Backtracking/17. Letter Combinations of a Phone Number.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Backtracking/17. Letter Combinations of a Phone Number.cpp -------------------------------------------------------------------------------- /Leetcode/Backtracking/216. Combination Sum III.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> combinationSum3(int k, int n) { 4 | vector>ans; 5 | vectortem; 6 | solve(ans,tem,k,n,1); 7 | return ans; 8 | } 9 | void solve(vector>&ans, vector&tem,int k,int n,int s) { 10 | if (n==0&&k==0) { 11 | ans.push_back(tem); 12 | return ; 13 | } else if (k<=0||n<=0) 14 | return ; 15 | for (; s<10; ++s) { 16 | tem.push_back(s); 17 | solve(ans,tem,k-1,n-s,s+1); 18 | tem.pop_back(); 19 | } 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Leetcode/Backtracking/39. Combination Sum.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Backtracking/39. Combination Sum.cpp -------------------------------------------------------------------------------- /Leetcode/Backtracking/40. Combination Sum II.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Backtracking/40. Combination Sum II.cpp -------------------------------------------------------------------------------- /Leetcode/Backtracking/401. Binary Watch.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Backtracking/401. Binary Watch.cpp -------------------------------------------------------------------------------- /Leetcode/Backtracking/46. Permutations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> permute(vector& nums) { 4 | vector>ans; 5 | solve(ans,0,nums); 6 | return ans; 7 | } 8 | void solve(vector>&ans,int index,vector&nums) { 9 | if (index+1==nums.size()) { 10 | ans.push_back(nums); 11 | return ; 12 | } 13 | for (int i=index; i col(n, true); 5 | vector anti(2*n-1, true); 6 | vector main(2*n-1, true); 7 | vector row(n, 0); 8 | int count = 0; 9 | dfs(0, row, col, main, anti, count); 10 | return count; 11 | } 12 | void dfs(int i, vector &row, vector &col, vector& main, vector &anti, int &count) { 13 | if (i == row.size()) { 14 | count++; 15 | return; 16 | } 17 | for (int j = 0; j < col.size(); j++) { 18 | if (col[j] && main[i+j] && anti[i+col.size()-1-j]) { 19 | row[i] = j; 20 | col[j] = main[i+j] = anti[i+col.size()-1-j] = false; 21 | dfs(i+1, row, col, main, anti, count); 22 | col[j] = main[i+j] = anti[i+col.size()-1-j] = true; 23 | } 24 | } 25 | } 26 | }; 27 | 28 | -------------------------------------------------------------------------------- /Leetcode/Backtracking/60. Permutation Sequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string getPermutation(int n, int k) { 4 | vectorptable(n); 5 | vectorvset; 6 | ptable[0]=1; 7 | for(int i=1; i=0; --i) { 14 | int kth=k/ptable[i]; 15 | k%=ptable[i]; 16 | ans.push_back(vset[kth]); 17 | vset.erase(vset.begin()+kth); 18 | } 19 | return ans; 20 | } 21 | }; 22 | 23 | -------------------------------------------------------------------------------- /Leetcode/Backtracking/77. Combinations.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> combine(int n, int k) { 4 | vector>res; 5 | vectorpath; 6 | dfs(res,path,n,k); 7 | return res; 8 | } 9 | void dfs(vector>&res,vector&path,int n,int k) { 10 | if(k<=0) { 11 | res.push_back(path); 12 | return ; 13 | } 14 | if(n0; --n) { 17 | path.push_back(n); 18 | dfs(res,path,n-1,k-1); 19 | path.pop_back(); 20 | } 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Leetcode/Backtracking/78. Subsets.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> subsets(vector& nums) { 4 | vector>ans; 5 | vectortem; 6 | solve(ans,nums,0,tem); 7 | return ans; 8 | } 9 | void solve(vector>&ans,vector&nums,int index,vector&tem) { 10 | if (index==nums.size()) { 11 | ans.push_back(tem); 12 | return ; 13 | } 14 | tem.push_back(nums[index]); 15 | solve(ans,nums,index+1,tem); 16 | tem.pop_back(); 17 | solve(ans,nums,index+1,tem); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Leetcode/Backtracking/79. Word Search.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool exist(vector > &board, string word) { 4 | m=board.size(); 5 | n=board[0].size(); 6 | for(int x=0; x > &board, const char* w, int x, int y) { 17 | if(x<0||y<0||x>=m||y>=n||board[x][y]=='\0'||*w!=board[x][y]) 18 | return false; 19 | if(*(w+1)=='\0') 20 | return true; 21 | char t=board[x][y]; 22 | board[x][y]='\0'; 23 | if(isFound(board,w+1,x-1,y)||isFound(board,w+1,x+1,y)||isFound(board,w+1,x,y-1)||isFound(board,w+1,x,y+1)) 24 | return true; 25 | board[x][y]=t; 26 | return false; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Leetcode/Backtracking/89. Gray Code.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector grayCode(int n) { 4 | vectorans(1<=0; --k) 11 | ans[max-k]=ans[k]|high; 12 | } 13 | return ans; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Leetcode/Backtracking/90. Subsets II.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Backtracking/90. Subsets II.cpp -------------------------------------------------------------------------------- /Leetcode/Backtracking/93. Restore IP Addresses.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector restoreIpAddresses(string s) { 4 | dfs(s,4,""); 5 | return ans; 6 | } 7 | vector ans; 8 | bool isValid(string s) { 9 | int i=atoi(s.c_str()); 10 | if(s.length()==1) 11 | return true; 12 | else if(s.length()==2&&i>=10) 13 | return true; 14 | else if (s.length()==3&&i>=100&&i<=255) 15 | return true; 16 | return false; 17 | } 18 | void dfs(string s, int n,string ip) { 19 | if(s.length()>(n*3)||s.length()i&&isValid(ss)) { 28 | dfs(s.substr(i),n-1,ip+ss+"."); 29 | } 30 | } 31 | } 32 | };s 33 | -------------------------------------------------------------------------------- /Leetcode/Binary Search/153. Find Minimum in Rotated Sorted Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMin(vector& nums) { 4 | int left=0,right=nums.size()-1,mid; 5 | while(left>1; 7 | if(nums[mid]& nums) { 4 | int l=0, r=nums.size()-1; 5 | while(l>1; 7 | if(nums[m]& nums) { 4 | sort(nums.begin(),nums.end()); 5 | int ans=0; 6 | for(int i=0; i<(int)nums.size()-2; ++i) 7 | for(int j=i+1; j<(int)nums.size()-1; ++j) { 8 | int c=nums[i]+nums[j]-1; 9 | int cnt=upper_bound(nums.begin()+j+1,nums.end(),c)-nums.begin()-j-1; 10 | ans+=cnt; 11 | } 12 | return ans; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Leetcode/Bit Manipulation/397. Integer Replacement.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int integerReplacement(int n) { 4 | return n==1?0:n==0x7fffffff?32:n&1?min(integerReplacement(n+1), integerReplacement(n-1))+1:integerReplacement(n>>1)+1; 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /Leetcode/Bit Manipulation/477. Total Hamming Distance.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Bit Manipulation/477. Total Hamming Distance.cpp -------------------------------------------------------------------------------- /Leetcode/DFS/100. Same Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | bool isSameTree(TreeNode *p, TreeNode *q) { 13 | if (p == NULL || q == NULL) return (p == q); 14 | return (p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right)); 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Leetcode/DFS/104. Maximum Depth of Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | int maxDepth(TreeNode *root) { 13 | return root == NULL ? 0 : max(maxDepth(root -> left), maxDepth(root -> right)) + 1; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Leetcode/DFS/108. Convert Sorted Array to Binary Search Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | TreeNode *sortedArrayToBST(vector &num) { 13 | if(num.size() == 0) return NULL; 14 | if(num.size() == 1) { 15 | return new TreeNode(num[0]); 16 | } 17 | 18 | int middle = num.size()/2; 19 | TreeNode* root = new TreeNode(num[middle]); 20 | 21 | vector leftInts(num.begin(), num.begin()+middle); 22 | vector rightInts(num.begin()+middle+1, num.end()); 23 | 24 | root->left = sortedArrayToBST(leftInts); 25 | root->right = sortedArrayToBST(rightInts); 26 | 27 | return root; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Leetcode/DFS/110. Balanced Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | int dfsHeight (TreeNode *root) { 13 | if (root == NULL) return 0; 14 | 15 | int leftHeight = dfsHeight (root -> left); 16 | if (leftHeight == -1) return -1; 17 | int rightHeight = dfsHeight (root -> right); 18 | if (rightHeight == -1) return -1; 19 | 20 | if (abs(leftHeight - rightHeight) > 1) return -1; 21 | return max (leftHeight, rightHeight) + 1; 22 | } 23 | bool isBalanced(TreeNode *root) { 24 | return dfsHeight (root) != -1; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Leetcode/DFS/112. Path Sum.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | bool hasPathSum(TreeNode *root, int sum) { 13 | if (root == NULL) return false; 14 | if (root->val == sum && root->left == NULL && root->right == NULL) return true; 15 | return hasPathSum(root->left, sum-root->val) || hasPathSum(root->right, sum-root->val); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Leetcode/DFS/113. Path Sum II.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/DFS/113. Path Sum II.cpp -------------------------------------------------------------------------------- /Leetcode/DFS/117. Populating Next Right Pointers in Each Node II.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/DFS/117. Populating Next Right Pointers in Each Node II.cpp -------------------------------------------------------------------------------- /Leetcode/DFS/129. Sum Root to Leaf Numbers.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | int sumNumbers(TreeNode* root) { 13 | if (root==NULL) { 14 | this->ans+=sum; 15 | return this->ans; 16 | } 17 | this->sum=this->sum*10+root->val; 18 | if (root->left==NULL&&root->right==NULL) 19 | this->ans+=this->sum; 20 | 21 | if (root->left) 22 | sumNumbers(root->left); 23 | if (root->right) 24 | sumNumbers(root->right); 25 | this->sum/=10; 26 | return this->ans; 27 | } 28 | int ans; 29 | int sum; 30 | Solution () { 31 | this->ans=this->sum=0; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Leetcode/DFS/329. Longest Increasing Path in a Matrix.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/DFS/329. Longest Increasing Path in a Matrix.cpp -------------------------------------------------------------------------------- /Leetcode/DFS/332. Reconstruct Itinerary.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/DFS/332. Reconstruct Itinerary.cpp -------------------------------------------------------------------------------- /Leetcode/DFS/337. House Robber III.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/DFS/337. House Robber III.cpp -------------------------------------------------------------------------------- /Leetcode/DFS/37. Sudoku Solver.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/DFS/37. Sudoku Solver.cpp -------------------------------------------------------------------------------- /Leetcode/DFS/394. Decode String.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | string decodeString(string s) { 8 | str=s; 9 | index=0; 10 | return helpDecode(); 11 | } 12 | private: 13 | string str; 14 | int index; 15 | string helpDecode() { 16 | string s1=""; 17 | 18 | for (; index='1'&&ch<='9') { 21 | int nextIndex=str.find('[',index); 22 | stringstream ss; 23 | int count; 24 | ss<>count; 26 | index=nextIndex+1; 27 | string s2=helpDecode(); 28 | while(count--) 29 | s1+=s2; 30 | } else if (ch==']') { 31 | return s1; 32 | } else 33 | s1+=ch; 34 | } 35 | return s1; 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /Leetcode/DFS/473. Matchsticks to Square.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/DFS/473. Matchsticks to Square.cpp -------------------------------------------------------------------------------- /Leetcode/DFS/491. Increasing Subsequences.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/DFS/491. Increasing Subsequences.cpp -------------------------------------------------------------------------------- /Leetcode/DFS/514. Freedom Trail.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/DFS/514. Freedom Trail.cpp -------------------------------------------------------------------------------- /Leetcode/DFS/547. Friend Circles.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vectorvisited; 4 | int findCircleNum(vector>& M) { 5 | visited.resize(M.size(),false); 6 | int circleNum=0; 7 | for(int i=0; i>&M,int i) { 15 | if(visited[i]) 16 | return ; 17 | visited[i]=true; 18 | for(int j=0; jval); 15 | 16 | if (t->left) { 17 | s += "(" + tree2str(t->left) + ")"; 18 | } else if (t->right) { 19 | s += "()"; 20 | } 21 | 22 | if (t->right) { 23 | s += "(" + tree2str(t->right) + ")"; 24 | } 25 | 26 | return s; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Leetcode/DFS/637. Average of Levels in Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | vectorsum; 3 | vectorcnt; 4 | void dfs(TreeNode* root,int level) { 5 | if(root==NULL) 6 | return ; 7 | if(sum.size()>level) { 8 | sum[level]+=root->val; 9 | ++cnt[level]; 10 | } else { 11 | sum.push_back(root->val); 12 | cnt.push_back(1); 13 | } 14 | dfs(root->left,level+1); 15 | dfs(root->right,level+1); 16 | } 17 | public: 18 | vector averageOfLevels(TreeNode* root) { 19 | dfs(root,0); 20 | vectorans; 21 | for(int i=0; ival <= minNode->val || maxNode && root->val >= maxNode->val) 19 | return false; 20 | return isValidBST(root->left, minNode, root) && isValidBST(root->right, root, maxNode); 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Leetcode/DFS/99. Recover Binary Search Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | TreeNode* pre, *p1, *p2; 13 | int preVal=INT_MIN; 14 | void recoverTree(TreeNode* root) { 15 | pre=p1=p2=NULL; 16 | inOrderTraverse(root); 17 | p1->val^=p2->val; 18 | p2->val^=p1->val; 19 | p1->val^=p2->val; 20 | } 21 | void inOrderTraverse(TreeNode* root) { 22 | if(root==NULL) 23 | return ; 24 | inOrderTraverse(root->left); 25 | if(preVal>root->val) { 26 | if(p1==NULL) 27 | p1=pre, p2=root; 28 | else 29 | p2=root; 30 | } 31 | pre=root; 32 | preVal=root->val; 33 | inOrderTraverse(root->right); 34 | } 35 | }; 36 | -------------------------------------------------------------------------------- /Leetcode/Design/631. Design Excel Sum Formula.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Design/631. Design Excel Sum Formula.cpp -------------------------------------------------------------------------------- /Leetcode/Design/635. Design Log Storage System.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Design/635. Design Log Storage System.cpp -------------------------------------------------------------------------------- /Leetcode/Divide and Conquer/169. Majority Element.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int majorityElement(vector& nums) { 4 | int major = 0, n = nums.size(); 5 | for (int i = 0, mask = 1; i < 32; i++, mask <<= 1) { 6 | int bitCounts = 0; 7 | for (int j = 0; j < n; j++) { 8 | if (nums[j] & mask) bitCounts++; 9 | if (bitCounts > n / 2) { 10 | major |= mask; 11 | break; 12 | } 13 | } 14 | } 15 | return major; 16 | } 17 | }; 18 | 19 | -------------------------------------------------------------------------------- /Leetcode/Divide and Conquer/215. Kth Largest Element in an Array.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Divide and Conquer/215. Kth Largest Element in an Array.cpp -------------------------------------------------------------------------------- /Leetcode/Divide and Conquer/240. Search a 2D Matrix II.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Divide and Conquer/240. Search a 2D Matrix II.cpp -------------------------------------------------------------------------------- /Leetcode/Divide and Conquer/241. Different Ways to Add Parentheses.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Divide and Conquer/241. Different Ways to Add Parentheses.cpp -------------------------------------------------------------------------------- /Leetcode/Divide and Conquer/315. Count of Smaller Numbers After Self.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Divide and Conquer/315. Count of Smaller Numbers After Self.cpp -------------------------------------------------------------------------------- /Leetcode/Divide and Conquer/327. Count of Range Sum.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Divide and Conquer/327. Count of Range Sum.cpp -------------------------------------------------------------------------------- /Leetcode/Divide and Conquer/4. Median of Two Sorted Arrays.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Divide and Conquer/4. Median of Two Sorted Arrays.cpp -------------------------------------------------------------------------------- /Leetcode/Divide and Conquer/493. Reverse Pairs.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Divide and Conquer/493. Reverse Pairs.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/10. Regular Expression Matching.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/10. Regular Expression Matching.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/114 Flatten Binary Tree to Linked List.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/114 Flatten Binary Tree to Linked List.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/115. Distinct Subsequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numDistinct(string s, string t) { 4 | int slen=s.length(); 5 | int tlen=t.length(); 6 | if (tlen>slen) 7 | return 0; 8 | vector>dp(slen+1,vector(tlen+1,0)); 9 | for (int i=0;i<=slen;++i) 10 | dp[i][0]=1; 11 | for(int i=1; i<=slen; ++i) { 12 | for(int j=1; j<=tlen; ++j) { 13 | if (s[i-1]==t[j-1]) 14 | dp[i][j]+=dp[i-1][j-1]+dp[i-1][j]; 15 | else 16 | dp[i][j]=dp[i-1][j]; 17 | } 18 | } 19 | return dp[slen][tlen]; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/120. Triangle.cpp: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | For the kth level: 4 | minpath[i] = min( minpath[i], minpath[i+1]) + triangle[k][i]; 5 | */ 6 | int minimumTotal(vector > &triangle) { 7 | int n = triangle.size(); 8 | vector minlen(triangle.back()); 9 | for (int layer = n-2; layer >= 0; layer--) { // For each layer 10 | for (int i = 0; i <= layer; i++) { // Check its every 'node' 11 | // Find the lesser of its two children, and sum the current value in the triangle with it. 12 | minlen[i] = min(minlen[i], minlen[i+1]) + triangle[layer][i]; 13 | } 14 | } 15 | return minlen[0]; 16 | } 17 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/121. Best Time to Buy and Sell Stock.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector &prices) { 4 | int maxPro = 0; 5 | int minPrice = INT_MAX; 6 | for(int i = 0; i < prices.size(); i++) { 7 | minPrice = min(minPrice, prices[i]); 8 | maxPro = max(maxPro, prices[i] - minPrice); 9 | } 10 | return maxPro; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/122. Best Time to Buy and Sell Stock II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector &prices) { 4 | int ret = 0; 5 | for (size_t p = 1; p < prices.size(); ++p) 6 | ret += max(prices[p] - prices[p - 1], 0); 7 | return ret; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/123. Best Time to Buy and Sell Stock III.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | int hold1 = INT_MIN, hold2 = INT_MIN; 5 | int release1 = 0, release2 = 0; 6 | for(auto i:prices) { // Assume we only have 0 money at first 7 | release2 = max(release2, hold2+i); // The maximum if we've just sold 2nd stock so far. 8 | hold2 = max(hold2, release1-i); // The maximum if we've just buy 2nd stock so far. 9 | release1 = max(release1, hold1+i); // The maximum if we've just sold 1nd stock so far. 10 | hold1 = max(hold1, -i); // The maximum if we've just buy 1st stock so far. 11 | } 12 | return release2; ///Since release1 is initiated as 0, so release2 will always higher than release1. 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/132. Palindrome Partitioning II.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/132. Palindrome Partitioning II.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/139. Word Break.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | using namespace std; 10 | 11 | class Solution { 12 | public: 13 | bool wordBreak(string s, vector& wordDict) { 14 | unordered_setdict; 15 | for (vector::iterator it = wordDict.begin(); it != wordDict.end(); ++it) 16 | dict.insert(*it); 17 | 18 | int i, j; 19 | int slen = s.length(); 20 | vectorv(slen + 1, false); 21 | v[0] = true; 22 | for (i = 1; i <= slen; ++i) 23 | for (j = i - 1; j >= 0; --j) 24 | if (v[j] && dict.count(s.substr(j, i - j))) { 25 | v[i] = true; 26 | break; 27 | } 28 | return v[slen]; 29 | } 30 | }; -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/152. Maximum Product Subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector& nums) { 4 | if(nums.empty()) 5 | return 0; 6 | int maxMul=nums[0]; 7 | int minMul=nums[0]; 8 | int ans=nums[0]; 9 | for(int i=1; i& nums) { 4 | int n = nums.size(); 5 | if (n < 2) return n ? nums[0] : 0; 6 | return max(robber(nums, 0, n - 2), robber(nums, 1, n - 1)); 7 | } 8 | private: 9 | int robber(vector& nums, int l, int r) { 10 | int pre = 0, cur = 0; 11 | for (int i = l; i <= r; i++) { 12 | int temp = max(pre + nums[i], cur); 13 | pre = cur; 14 | cur = temp; 15 | } 16 | return cur; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/264. Ugly Number II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int nthUglyNumber(int n) { 4 | if(n <= 0) return false; 5 | int t2 = 0, t3 = 0, t5 = 0; //pointers for 2, 3, 5 6 | vector k(n); 7 | k[0] = 1; 8 | for(int i = 1; i < n ; i ++) { 9 | k[i] = min(k[t2]*2,min(k[t3]*3,k[t5]*5)); 10 | if(k[i] == k[t2]*2) t2++; 11 | if(k[i] == k[t3]*3) t3++; 12 | if(k[i] == k[t5]*5) t5++; 13 | } 14 | return k[n-1]; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/279. Perfect Squares.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/279. Perfect Squares.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/300. Longest Increasing Subsequence.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/300. Longest Increasing Subsequence.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/309. Best Time to Buy and Sell Stock with Cooldown.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/309. Best Time to Buy and Sell Stock with Cooldown.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/312. Burst Balloons.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/312. Burst Balloons.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/32. Longest Valid Parentheses.cpp: -------------------------------------------------------------------------------- 1 | #include "headFile" 2 | 3 | class Solution { 4 | public: 5 | int longestValidParentheses(string s) { 6 | int slen=s.length(); 7 | vectordp(slen+1); 8 | dp[0]=0; 9 | int ans=0; 10 | for (int i=1; i<=slen; ++i) { 11 | if (s[i-1]=='(') 12 | dp[i]=0; 13 | else { 14 | int pos=i-2-dp[i-1]; 15 | if (pos>=0&&s[pos]=='(') 16 | dp[i]=dp[i-1]+2+dp[pos]; 17 | else 18 | dp[i]=0; 19 | } 20 | ans=max(ans,dp[i]); 21 | } 22 | return ans; 23 | } 24 | }; 25 | 26 | int main() 27 | { 28 | string str("()()(()()("); 29 | Solution s; 30 | cout< 1 && availableNumber > 0) { 10 | uniqueDigits = uniqueDigits * availableNumber; 11 | res += uniqueDigits; 12 | availableNumber--; 13 | } 14 | return res; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/363. Max Sum of Rectangle No Larger Than K.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/363. Max Sum of Rectangle No Larger Than K.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/368. Largest Divisible Subset.cpp: -------------------------------------------------------------------------------- 1 | #include "headFile" 2 | 3 | class Solution { 4 | public: 5 | vector largestDivisibleSubset(vector& nums) { 6 | vector num(nums); 7 | int size=num.size(); 8 | sort(num.begin(),num.end()); 9 | vectorsn(size,1); 10 | vectorparent(size,-1); 11 | 12 | int ansn=0,ansp=-1; 13 | for(int i=0; i=0; --j) { 15 | if (num[i]%num[j]==0&&sn[j]+1>sn[i]) { 16 | sn[i]=sn[j]+1; 17 | parent[i]=j; 18 | } 19 | } 20 | if(ansnrev(ansn); 27 | while(ansp!=-1) { 28 | rev[--ansn]=num[ansp]; 29 | ansp=parent[ansp]; 30 | } 31 | return rev; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/375. Guess Number Higher or Lower II.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/375. Guess Number Higher or Lower II.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/376. Wiggle Subsequence.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/376. Wiggle Subsequence.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/392. Is Subsequence.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/392. Is Subsequence.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/403. Frog Jump.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/403. Frog Jump.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/410. Split Array Largest Sum.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/410. Split Array Largest Sum.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/413. Arithmetic Slices.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/413. Arithmetic Slices.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/416. Partition Equal Subset Sum.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/416. Partition Equal Subset Sum.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/44. Wildcard Matching.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isMatch(string s, string p) { 4 | int slen=s.length(); 5 | int plen=p.length(); 6 | vector>dp(slen+1,vector(plen+1,false)); 7 | dp[0][0]=true; 8 | for (int i=1; i<=plen; ++i) 9 | if (dp[0][i-1]&&p[i-1]=='*') 10 | dp[0][i]=true; 11 | for (int i=1; i<=slen; ++i) 12 | if (dp[i-1][0]&&s[i-1]=='*') 13 | dp[i][0]=true; 14 | for (int i=1; i<=slen; ++i) { 15 | for (int j=1; j<=plen; ++j) { 16 | if(s[i-1]=='*'||p[j-1]=='*') 17 | dp[i][j]=dp[i-1][j-1]||dp[i][j-1]||dp[i-1][j]; 18 | else if (s[i-1]=='?'||p[j-1]=='?'||s[i-1]==p[j-1]) 19 | dp[i][j]=dp[i-1][j-1]; 20 | } 21 | } 22 | return dp[slen][plen]; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/446. Arithmetic Slices II - Subsequence.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/446. Arithmetic Slices II - Subsequence.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/464. Can I Win.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/464. Can I Win.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/466. Count The Repetitions.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/466. Count The Repetitions.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/467. Unique Substrings in Wraparound String.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/467. Unique Substrings in Wraparound String.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/472. Concatenated Words.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/472. Concatenated Words.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/474. Ones and Zeroes.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/474. Ones and Zeroes.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/486. Predict the Winner.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/486. Predict the Winner.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/494. Target Sum.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/494. Target Sum.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/516. Longest Palindromic Subsequence.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | int longestPalindromeSubseq(string s) { 8 | int len = s.length(); 9 | vector>dp(len, vector(len, 0)); 10 | 11 | for (int i = len - 1; i >= 0; --i) { 12 | dp[i][i] = 1; 13 | for (int j = i + 1; j < len; ++j) 14 | if (s[i] == s[j]) 15 | dp[i][j] = dp[i + 1][j - 1] + 2; 16 | else 17 | dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]); 18 | } 19 | return dp[0][len - 1]; 20 | } 21 | }; -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/523. Continuous Subarray Sum.cpp: -------------------------------------------------------------------------------- 1 | #include "headFile" 2 | 3 | class Solution { 4 | public: 5 | bool checkSubarraySum(vector& nums, int k) { 6 | int size=nums.size(); 7 | if (size<2) 8 | return false; 9 | if (k==0) { 10 | for (int i=1; i0?k:-k; 16 | vectordp(size); 17 | for (int i=0; i=0; --j) { 20 | dp[j]+=nums[i]; 21 | if (dp[j]%k==0) 22 | return true; 23 | } 24 | } 25 | return false; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/53. Maximum Subarray.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/53. Maximum Subarray.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/546. Remove Boxes.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/546. Remove Boxes.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/552. Student Attendance Record II.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/552. Student Attendance Record II.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/583. Delete Operation for Two Strings.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDistance(string word1, string word2) { 4 | int len1=word1.length(); 5 | int len2=word2.length(); 6 | vector>dp(len1+1,vector(len2+1,0)); 7 | for(int i=1; i<=len1; ++i) 8 | for(int j=1; j<=len2; ++j) { 9 | dp[i][j]=max(dp[i-1][j],dp[i][j-1]); 10 | if(word1[i-1]==word2[j-1]) 11 | dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1); 12 | } 13 | return len1+len2-dp[len1][len2]*2; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/600. Non-negative Integers without Consecutive Ones.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/600. Non-negative Integers without Consecutive Ones.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/62. Unique Paths.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int uniquePaths(int m, int n) { 4 | vector>dp(m+1,vector(n+1,0)); 5 | dp[0][1]=1; 6 | for (int i=1; i<=m; ++i) 7 | for (int j=1; j<=n; ++j) 8 | dp[i][j]=dp[i-1][j]+dp[i][j-1]; 9 | return dp[m][n]; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/629. K Inverse Pairs Array.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/629. K Inverse Pairs Array.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/63. Unique Paths II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int uniquePathsWithObstacles(vector>& obstacleGrid) { 4 | int m=obstacleGrid.size(); 5 | if (!m) 6 | return 0; 7 | int n=obstacleGrid[0].size(); 8 | if (!n) 9 | return 0; 10 | vector>dp(m+1,vector(n+1,0)); 11 | dp[0][1]=1; 12 | for (int i=1; i<=m; ++i) 13 | for (int j=1; j<=n; ++j) 14 | if (!obstacleGrid[i-1][j-1]) 15 | dp[i][j]=dp[i-1][j]+dp[i][j-1]; 16 | return dp[m][n]; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/634. Find the Derangement of An Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | #define MOD 1000000007 4 | int findDerangement(int n) { 5 | if(n<=3) 6 | return n-1; 7 | vectordp(n+1); 8 | dp[0]=dp[1]=0; 9 | dp[2]=1; 10 | for(int i=3; i<=n; ++i) 11 | dp[i]=((i-1)*(dp[i-2]+dp[i-1]))%MOD; 12 | return dp[n]; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/639. Decode Ways II.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/639. Decode Ways II.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/64. Minimum Path Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minPathSum(vector>& grid) { 4 | int m = grid.size(); 5 | int n = grid[0].size(); 6 | vector > sum(m, vector(n, grid[0][0])); 7 | for (int i = 1; i < m; i++) 8 | sum[i][0] = sum[i - 1][0] + grid[i][0]; 9 | for (int j = 1; j < n; j++) 10 | sum[0][j] = sum[0][j - 1] + grid[0][j]; 11 | for (int i = 1; i < m; i++) 12 | for (int j = 1; j < n; j++) 13 | sum[i][j] = min(sum[i - 1][j], sum[i][j - 1]) + grid[i][j]; 14 | return sum[m - 1][n - 1]; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/664. Strange Printer.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/664. Strange Printer.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/673. Number of Longest Increasing Subsequence.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/673. Number of Longest Increasing Subsequence.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/688. Knight Probability in Chessboard.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/688. Knight Probability in Chessboard.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/689. Maximum Sum of 3 Non-Overlapping Subarrays.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/689. Maximum Sum of 3 Non-Overlapping Subarrays.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/70. Climbing Stairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int climbStairs(int n) { 4 | if(n == 0 || n == 1 || n == 2) { 5 | return n; 6 | } 7 | vector mem(n); 8 | mem[0] = 1; 9 | mem[1] = 2; 10 | for(int i = 2; i < n; i++) { 11 | mem[i] = mem[i-1] + mem[i-2]; 12 | } 13 | return mem[n-1]; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/714. Best Time to Buy and Sell Stock with Transaction Fee.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution 6 | { 7 | public: 8 | int maxProfit(vector &prices, int fee){ 9 | int i0=0, i1=-prices.front(); 10 | int n0, n1; 11 | for(int i=1; i>dif(len1+1,vector(len2+1)); 7 | for (int i=0; i<=len1; ++i) 8 | dif[i][0]=i; 9 | for (int j=0; j<=len2; ++j) 10 | dif[0][j]=j; 11 | for (int i=1; i<=len1; ++i) { 12 | for (int j=1; j<=len2; ++j) { 13 | int tm=min(dif[i-1][j],dif[i][j-1]); 14 | if (word1[i-1]==word2[j-1]) 15 | dif[i][j]=min(tm+1,dif[i-1][j-1]); 16 | else 17 | dif[i][j]=min(tm,dif[i-1][j-1])+1; 18 | } 19 | } 20 | return dif[len1][len2]; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/730. Count Different Palindromic Subsequences.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/730. Count Different Palindromic Subsequences.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/741. Cherry Pickup.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/741. Cherry Pickup.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/86 Partition List.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/86 Partition List.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/91. Decode Ways.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/91. Decode Ways.cpp -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/96. Unique Binary Search Trees.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | /** 4 | * Taking 1~n as root respectively: 5 | * 1 as root: # of trees = F(0) * F(n-1) // F(0) == 1 6 | * 2 as root: # of trees = F(1) * F(n-2) 7 | * 3 as root: # of trees = F(2) * F(n-3) 8 | * ... 9 | * n-1 as root: # of trees = F(n-2) * F(1) 10 | * n as root: # of trees = F(n-1) * F(0) 11 | * 12 | * So, the formulation is: 13 | * F(n) = F(0) * F(n-1) + F(1) * F(n-2) + F(2) * F(n-3) + ... + F(n-2) * F(1) + F(n-1) * F(0) 14 | */ 15 | 16 | int numTrees(int n) { 17 | int dp[n+1]; 18 | dp[0] = dp[1] = 1; 19 | for (int i=2; i<=n; i++) { 20 | dp[i] = 0; 21 | for (int j=1; j<=i; j++) { 22 | dp[i] += dp[j-1] * dp[i-j]; 23 | } 24 | } 25 | return dp[n]; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Leetcode/Dynamic Programming/97. Interleaving String.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Dynamic Programming/97. Interleaving String.cpp -------------------------------------------------------------------------------- /Leetcode/Greedy/134. Gas Station.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Greedy/134. Gas Station.cpp -------------------------------------------------------------------------------- /Leetcode/Greedy/135. Candy.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Greedy/135. Candy.cpp -------------------------------------------------------------------------------- /Leetcode/Greedy/316. Remove Duplicate Letters.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Greedy/316. Remove Duplicate Letters.cpp -------------------------------------------------------------------------------- /Leetcode/Greedy/330. Patching Array.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Greedy/330. Patching Array.cpp -------------------------------------------------------------------------------- /Leetcode/Greedy/402. Remove K Digits.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Greedy/402. Remove K Digits.cpp -------------------------------------------------------------------------------- /Leetcode/Greedy/406. Queue Reconstruction by Height.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Greedy/406. Queue Reconstruction by Height.cpp -------------------------------------------------------------------------------- /Leetcode/Greedy/435. Non-overlapping Intervals.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for an interval. 3 | * struct Interval { 4 | * int start; 5 | * int end; 6 | * Interval() : start(0), end(0) {} 7 | * Interval(int s, int e) : start(s), end(e) {} 8 | * }; 9 | */ 10 | bool operator <(const Interval &I1,const Interval &I2) { 11 | return I1.start& intervals) { 17 | sort(intervals.begin(),intervals.end()); 18 | int ans=0; 19 | int i,r; 20 | r=intervals.size()>0?intervals[0].end:0; 21 | for(i=1; iintervals[i].start) { 23 | ++ans; 24 | r=min(r,intervals[i].end); 25 | } else 26 | r=intervals[i].end; 27 | } 28 | return ans; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Leetcode/Greedy/45. Jump Game II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int jump(vector& nums) { 4 | if (nums.empty()) 5 | return 0; 6 | int step=0,cur=0,next=0; 7 | for (int i=0; cur& g, vector& s) { 4 | sort(g.begin(),g.end()); 5 | sort(s.begin(),s.end()); 6 | int ans,i,j; 7 | for(i=0,j=0,ans=0; i& nums) { 4 | int maxIndex=0; 5 | for (int i=0; maxIndex+1=nums.size(); 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /Leetcode/Greedy/573. Squirrel Simulation.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDistance(int height, int width, vector& tree, vector& squirrel, vector>& nuts) { 4 | long long total=0,ans; 5 | vector>dis; 6 | for(auto a:nuts) { 7 | int d1=abs(a[0]-tree[0])+abs(a[1]-tree[1]); 8 | int d2=abs(a[0]-squirrel[0])+abs(a[1]-squirrel[1]); 9 | dis.push_back({d1,d2}); 10 | total+=d1<<1; 11 | } 12 | ans=total+INT_MAX; 13 | for(auto &a:dis) { 14 | ans=min(ans,total-a.first+a.second); 15 | } 16 | return ans; 17 | } 18 | }; 19 | 20 | -------------------------------------------------------------------------------- /Leetcode/Greedy/621. Task Scheduler.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Greedy/621. Task Scheduler.cpp -------------------------------------------------------------------------------- /Leetcode/Greedy/630. Course Schedule III.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Greedy/630. Course Schedule III.cpp -------------------------------------------------------------------------------- /Leetcode/Greedy/632. Smallest Range.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Greedy/632. Smallest Range.cpp -------------------------------------------------------------------------------- /Leetcode/Hash/209. Minimum Size Subarray Sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minSubArrayLen(int s, vector& nums) { 4 | int begin=0,end=0,sum=0,minlen=INT_MAX; 5 | for(int end=0; end=s) { 8 | minlen=min(minlen,end-begin); 9 | sum-=nums[begin++]; 10 | } 11 | } 12 | return minlen==INT_MAX?0:minlen; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Leetcode/Hash/3. Longest Substring Without Repeating Characters.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLongestSubstring(string s) { 4 | vectormap(256,0); 5 | int begin=0,end=0,maxlen=0; 6 | for(; end 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector findAnagrams(string s, string p) { 7 | vector sc(256, 0), pc(256, 0); 8 | int diff=p.length(); 9 | for(int i=0; ires; 13 | for(int i=0; iabs(++sc[ch]-pc[ch]) ?-1 :1; 16 | if(i>=p.length()){ 17 | ch=s[i-p.length()]; 18 | diff+=abs(sc[ch]-pc[ch])>abs(--sc[ch]-pc[ch]) ?-1 :1; 19 | } 20 | if(!diff){ 21 | res.emplace_back(i+1-p.length()); 22 | } 23 | } 24 | return res; 25 | } 26 | }; -------------------------------------------------------------------------------- /Leetcode/Hash/454. 4Sum II.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Hash/454. 4Sum II.cpp -------------------------------------------------------------------------------- /Leetcode/Hash/463. Island Perimeter.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int islandPerimeter(vector>& grid) { 4 | int count=0, repeat=0; 5 | for(int i=0; i findFrequentTreeSum(TreeNode* root) { 4 | unordered_map counts; 5 | int maxCount = 0; 6 | countSubtreeSums(root, counts, maxCount); 7 | 8 | vector maxSums; 9 | for(const auto& x : counts) { 10 | if(x.second == maxCount) maxSums.push_back(x.first); 11 | } 12 | return maxSums; 13 | } 14 | 15 | int countSubtreeSums(TreeNode *r, unordered_map &counts, int& maxCount) { 16 | if(r == nullptr) return 0; 17 | 18 | int sum = r->val; 19 | sum += countSubtreeSums(r->left, counts, maxCount); 20 | sum += countSubtreeSums(r->right, counts, maxCount); 21 | ++counts[sum]; 22 | maxCount = max(maxCount, counts[sum]); 23 | return sum; 24 | } 25 | }; 26 | -------------------------------------------------------------------------------- /Leetcode/Hash/525. Contiguous Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMaxLength(vector& nums) { 4 | unordered_maphash; 5 | hash[0]=-1; 6 | int cnt1=0,maxlen=0,gap01,i; 7 | for(i=0; i 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | 8 | class Solution { 9 | public: 10 | int leastBricks(vector>& wall) { 11 | unordered_maphashMap; 12 | int maxb=0; 13 | for(auto &a:wall) { 14 | for(int sum=0,i=0; iumap; 23 | umap[0]="0"; 24 | umap[1]; 25 | umap[2]+="123"; 26 | for(auto &a:umap) 27 | cout<& candies) { 4 | sort(candies.begin(),candies.end()); 5 | int diff=0; 6 | int pre=INT_MIN; 7 | for(auto &a:candies) 8 | if(a!=pre) { 9 | pre=a; 10 | ++diff; 11 | } 12 | return min(diff,(int)candies.size()/2); 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Leetcode/Hash/594. Longest Harmonious Subsequence.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLHS(vector& nums) { 4 | 5 | unordered_map map; 6 | auto max = 0; 7 | 8 | for(auto element : nums) { 9 | 10 | map[element]++; 11 | if (map.count(element-1) > 0) { 12 | max = std::max(max, map[element-1] + map[element] ); 13 | } 14 | if (map.count(element+1) > 0) { 15 | max = std::max(max, map[element+1] + map[element] ); 16 | } 17 | } 18 | return max; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Leetcode/Hash/599. Minimum Index Sum of Two Lists.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector findRestaurant(vector& list1, vector& list2) { 4 | unordered_maphash; 5 | for(int i=0; ians; 9 | for(int i=0; i> findDuplicate(vector& paths) { 4 | unordered_map> files; 5 | vector> result; 6 | 7 | for (auto path : paths) { 8 | stringstream ss(path); 9 | string root; 10 | string s; 11 | getline(ss, root, ' '); 12 | while (getline(ss, s, ' ')) { 13 | string fileName = root + '/' + s.substr(0, s.find('(')); 14 | string fileContent = s.substr(s.find('(') + 1, s.find(')') - s.find('(') - 1); 15 | files[fileContent].push_back(fileName); 16 | } 17 | } 18 | 19 | for (auto file : files) { 20 | if (file.second.size() > 1) 21 | result.push_back(file.second); 22 | } 23 | 24 | return result; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /Leetcode/Hash/895. Maximum Frequency Stack.cpp: -------------------------------------------------------------------------------- 1 | class FreqStack { 2 | public: 3 | unordered_map freq; 4 | unordered_map> m; 5 | int maxfreq = 0; 6 | 7 | void push(int x) { 8 | maxfreq = max(maxfreq, ++freq[x]); 9 | m[freq[x]].push(x); 10 | } 11 | 12 | int pop() { 13 | int x = m[maxfreq].top(); 14 | m[maxfreq].pop(); 15 | if (!m[freq[x]--].size()) maxfreq--; 16 | return x; 17 | } 18 | }; 19 | 20 | /** 21 | * Your FreqStack object will be instantiated and called as such: 22 | * FreqStack obj = new FreqStack(); 23 | * obj.push(x); 24 | * int param_2 = obj.pop(); 25 | */ -------------------------------------------------------------------------------- /Leetcode/Heap/218. The Skyline Problem.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Heap/218. The Skyline Problem.cpp -------------------------------------------------------------------------------- /Leetcode/Heap/23. Merge k Sorted Lists.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Heap/23. Merge k Sorted Lists.cpp -------------------------------------------------------------------------------- /Leetcode/Heap/239. Sliding Window Maximum.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Heap/239. Sliding Window Maximum.cpp -------------------------------------------------------------------------------- /Leetcode/Heap/priority_queue/priority_queue.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Heap/priority_queue/priority_queue.cpp -------------------------------------------------------------------------------- /Leetcode/Heap/priority_queue/test1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Heap/priority_queue/test1.cpp -------------------------------------------------------------------------------- /Leetcode/Heap/priority_queue/test11.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Heap/priority_queue/test11.cpp -------------------------------------------------------------------------------- /Leetcode/Heap/priority_queue/test2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Heap/priority_queue/test2.cpp -------------------------------------------------------------------------------- /Leetcode/Heap/priority_queue/test3.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Heap/priority_queue/test3.cpp -------------------------------------------------------------------------------- /Leetcode/LinkList/141. Linked List Cycle.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | bool hasCycle(ListNode *head) { 12 | ListNode* p1, *p2; 13 | for(p1=head, p2=head; p2&&p2->next; ) { 14 | p1=p1->next; 15 | p2=p2->next->next; 16 | if(p1==p2) 17 | return true; 18 | } 19 | return false; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Leetcode/LinkList/142. Linked List Cycle II.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode *detectCycle(ListNode *head) { 12 | ListNode* p1=head, *p2=head; 13 | for(; p2&&p2->next;) { 14 | p1=p1->next; 15 | p2=p2->next->next; 16 | if(p1==p2) { 17 | p1=head; 18 | while(p1!=p2) { 19 | p1=p1->next; 20 | p2=p2->next; 21 | } 22 | return p1; 23 | } 24 | } 25 | return NULL; 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /Leetcode/LinkList/160. Intersection of Two Linked Lists.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { 12 | ListNode *cur1 = headA, *cur2 = headB; 13 | while(cur1 != cur2) { 14 | cur1 = cur1?cur1->next:headB; 15 | cur2 = cur2?cur2->next:headA; 16 | } 17 | return cur1; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Leetcode/LinkList/19. Remove Nth Node From End of List.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode* removeNthFromEnd(ListNode* head, int n) { 12 | ListNode* ph=new ListNode(0); 13 | ph->next=head; 14 | ListNode* p1=ph, *p2=ph; 15 | for(int i=n; i>=0; --i) { 16 | p2=p2->next; 17 | } 18 | for(; p2; p2=p2->next) { 19 | p1=p1->next; 20 | } 21 | if(p1->next) { 22 | p2=p1->next; 23 | p1->next=p2->next; 24 | delete p2; 25 | } 26 | p1=ph->next; 27 | delete ph; 28 | return p1; 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Leetcode/LinkList/21. Merge Two Sorted Lists.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { 4 | ListNode dummy(INT_MIN); 5 | ListNode *tail = &dummy; 6 | 7 | while (l1 && l2) { 8 | if (l1->val < l2->val) { 9 | tail->next = l1; 10 | l1 = l1->next; 11 | } else { 12 | tail->next = l2; 13 | l2 = l2->next; 14 | } 15 | tail = tail->next; 16 | } 17 | 18 | tail->next = l1 ? l1 : l2; 19 | return dummy.next; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Leetcode/LinkList/234. Palindrome Linked List.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode* temp; 12 | bool isPalindrome(ListNode* head) { 13 | temp = head; 14 | return check(head); 15 | } 16 | 17 | bool check(ListNode* p) { 18 | if (NULL == p) return true; 19 | bool isPal = check(p->next) & (temp->val == p->val); 20 | temp = temp->next; 21 | return isPal; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Leetcode/LinkList/237. Delete Node in a Linked List.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | void deleteNode(ListNode* node) { 12 | auto next = node->next; 13 | *node = *next; 14 | delete next; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Leetcode/LinkList/24. Swap Nodes in Pairs.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode* swapPairs(ListNode* head) { 12 | if(head==NULL||head->next==NULL) 13 | return head; 14 | ListNode *p1=head->next; 15 | head->next=swapPairs(p1->next); 16 | p1->next=head; 17 | return p1; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /Leetcode/LinkList/25. Reverse Nodes in k-Group.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/LinkList/25. Reverse Nodes in k-Group.cpp -------------------------------------------------------------------------------- /Leetcode/LinkList/61. Rotate List.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode *rotateRight(ListNode *head, int k) { 12 | if(head == NULL || head->next == NULL||k==0) 13 | return head; 14 | 15 | ListNode* node = head; 16 | int size =1; 17 | while(node->next != NULL) { 18 | size++; 19 | node = node->next; 20 | } 21 | 22 | //loop the list 23 | node->next=head; 24 | 25 | //handle the case of k>size 26 | k = k%size; 27 | 28 | //find the node to break the loop at 29 | while(--size >= k) { 30 | node=node->next; 31 | } 32 | 33 | ListNode* first = node->next; 34 | node->next=NULL; 35 | 36 | return first; 37 | } 38 | }; 39 | -------------------------------------------------------------------------------- /Leetcode/LinkList/83. Remove Duplicates from Sorted List.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode* deleteDuplicates(ListNode* head) { 12 | ListNode* cur = head, *pt; 13 | while(cur) { 14 | while(cur->next && cur->val == cur->next->val) { 15 | pt=cur->next; 16 | cur->next=pt->next; 17 | delete pt; 18 | } 19 | cur = cur->next; 20 | } 21 | return head; 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Leetcode/Math/224. Basic Calculator.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int calculate(string s) { 4 | stack nums, ops; 5 | int num = 0; 6 | int rst = 0; 7 | int sign = 1; 8 | for (char c : s) { 9 | if (isdigit(c)) { 10 | num = num * 10 + c - '0'; 11 | } else { 12 | rst += sign * num; 13 | num = 0; 14 | if (c == '+') sign = 1; 15 | if (c == '-') sign = -1; 16 | if (c == '(') { 17 | nums.push(rst); 18 | ops.push(sign); 19 | rst = 0; 20 | sign = 1; 21 | } 22 | if (c == ')' && ops.size()) { 23 | rst = ops.top() * rst + nums.top(); 24 | ops.pop(); 25 | nums.pop(); 26 | } 27 | } 28 | } 29 | rst += sign * num; 30 | return rst; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /Leetcode/Math/233. Number of Digit One.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Math/233. Number of Digit One.cpp -------------------------------------------------------------------------------- /Leetcode/Math/598. Range Addition II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxCount(int m, int n, vector>& ops) { 4 | if(ops.empty()) 5 | return m*n; 6 | int minRow=INT_MAX,minCol=INT_MAX; 7 | for(int i=0; i>& ops) { 18 | for (auto op : ops) { 19 | m = min(op[0], m); 20 | n = min(op[1], n); 21 | } 22 | return m * n; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Leetcode/Math/633. Sum of Square Numbers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool judgeSquareSum(int c) { 4 | int begin=0; 5 | int end=(int)sqrt(c); 6 | for(; begin<=end; ++begin) { 7 | int t1=begin*begin; 8 | int t2=(int)sqrt(c-t1); 9 | if(t1+t2*t2==c) 10 | return true; 11 | } 12 | return false; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Leetcode/Math/672. Bulb Switcher II.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Math/672. Bulb Switcher II.cpp -------------------------------------------------------------------------------- /Leetcode/Other/128. Longest Consecutive Sequence.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Other/128. Longest Consecutive Sequence.cpp -------------------------------------------------------------------------------- /Leetcode/Other/563. Binary Tree Tilt.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | int findTilt(TreeNode* root) { 13 | int s=0; 14 | return helpFindTilt(root,s); 15 | } 16 | int helpFindTilt(TreeNode* root,int &sum){ 17 | if(root==NULL){ 18 | sum=0; 19 | return 0; 20 | } 21 | int l,r,tilt; 22 | tilt=helpFindTilt(root->left,l); 23 | tilt+=helpFindTilt(root->right,r); 24 | tilt+=abs(l-r); 25 | sum=l+r+root->val; 26 | return tilt; 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /Leetcode/Other/572. Subtree of Another Tree.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 | * }; 9 | */ 10 | class Solution { 11 | public: 12 | bool isSame(TreeNode *s,TreeNode*t) { 13 | if(s==NULL&&t==NULL) 14 | return true; 15 | else if(s==NULL&&t!=NULL||s!=NULL&&t==NULL) 16 | return false; 17 | else if(s->val!=t->val) 18 | return false; 19 | return isSame(s->left,t->left)&&isSame(s->right,t->right); 20 | } 21 | bool isSubtree(TreeNode* s, TreeNode* t) { 22 | if(isSame(s,t)) 23 | return true; 24 | if(s) 25 | if(isSubtree(s->left,t)||isSubtree(s->right,t)) 26 | return true; 27 | return false; 28 | } 29 | }; 30 | -------------------------------------------------------------------------------- /Leetcode/Other/617. Merge Two Binary Trees.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Other/617. Merge Two Binary Trees.cpp -------------------------------------------------------------------------------- /Leetcode/Other/84. Largest Rectangle in Histogram.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Leetcode/Other/84. Largest Rectangle in Histogram.cpp -------------------------------------------------------------------------------- /Leetcode/String/12. Integer to Roman.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string intToRoman(int num) { 4 | char* c[4][10]= { 5 | {"","I","II","III","IV","V","VI","VII","VIII","IX"}, 6 | {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"}, 7 | {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}, 8 | {"","M","MM","MMM"} 9 | }; 10 | string roman; 11 | roman.append(c[3][num / 1000 % 10]); 12 | roman.append(c[2][num / 100 % 10]); 13 | roman.append(c[1][num / 10 % 10]); 14 | roman.append(c[0][num % 10]); 15 | 16 | return roman; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Leetcode/String/13. Roman to Integer.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int romanToInt(string s) { 4 | unordered_map T = { { 'I' , 1 }, 5 | { 'V' , 5 }, 6 | { 'X' , 10 }, 7 | { 'L' , 50 }, 8 | { 'C' , 100 }, 9 | { 'D' , 500 }, 10 | { 'M' , 1000 } 11 | }; 12 | 13 | int sum = T[s.back()]; 14 | for (int i = s.length() - 2; i >= 0; --i) { 15 | if (T[s[i]] < T[s[i + 1]]) { 16 | sum -= T[s[i]]; 17 | } else { 18 | sum += T[s[i]]; 19 | } 20 | } 21 | 22 | return sum; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /Leetcode/String/14. Longest Common Prefix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string longestCommonPrefix(vector& strs) { 4 | string ans; 5 | int maxlen=0; 6 | for (int i=0;istk; 7 | for (int i=0; i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class Solution { 7 | public: 8 | string shortestPalindrome(string s) { 9 | int pe=s.length()-1; 10 | int ps,t; 11 | for (ps=0; pe>ps; --pe) { 12 | for (ps=0,t=pe; ps=t) 15 | break; 16 | } 17 | string prefix=s.substr(pe+1,s.length()-pe-1); 18 | reverse(prefix.begin(),prefix.end()); 19 | return prefix+s; 20 | } 21 | }; 22 | 23 | int main() { 24 | string str="aacecaaa"; 25 | Solution s; 26 | cout<count(26,0); 5 | int i; 6 | for (i=0; i='a'?0:word[1]>='a'?0:1; 7 | for (int i=1; i='a'||!Upper&&word[i]<='Z') 9 | return false; 10 | return true; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Leetcode/String/539. Minimum Time Difference.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findMinDifference(vector& times) { 4 | int n = times.size(); 5 | sort(times.begin(), times.end()); 6 | int mindiff = INT_MAX; 7 | for (int i = 0; i < times.size(); i++) { 8 | int diff = abs(timeDiff(times[(i - 1 + n) % n], times[i])); 9 | diff = min(diff, 1440 - diff); 10 | mindiff = min(mindiff, diff); 11 | } 12 | return mindiff; 13 | } 14 | 15 | private: 16 | int timeDiff(string t1, string t2) { 17 | int h1 = stoi(t1.substr(0, 2)); 18 | int m1 = stoi(t1.substr(3, 2)); 19 | int h2 = stoi(t2.substr(0, 2)); 20 | int m2 = stoi(t2.substr(3, 2)); 21 | return (h2 - h1) * 60 + (m2 - m1); 22 | } 23 | }; 24 | -------------------------------------------------------------------------------- /Leetcode/String/541. Reverse String II.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | class Solution { 5 | public: 6 | string reverseStr(string s, int k) { 7 | int flag=1; 8 | string ans; 9 | for (int i=0; i=2||l>2) return false; 10 | } 11 | return true; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /Leetcode/String/553. Optimal Division.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string optimalDivision(vector& nums) { 4 | string ans=""; 5 | int size=nums.size(); 6 | if(size==0) 7 | return ans; 8 | else if(size==1) 9 | return to_string(nums[0]); 10 | ans=to_string(nums[0])+"/"; 11 | if(size>2) 12 | ans+="("; 13 | for(int i=1; i2) 16 | ans.back()=')'; 17 | else 18 | ans.erase(ans.length()-1); 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /Leetcode/String/556. Next Greater Element III.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int nextGreaterElement(int n) { 4 | string str=to_string(n); 5 | next_permutation(str.begin(),str.end()); 6 | long long ans=stoll(str); 7 | return ans>INT_MAX||ans<=n ? -1:(int)ans; 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /Leetcode/String/557. Reverse Words in a String III.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Solution { 6 | public: 7 | string reverseWords(string s) { 8 | string ans=""; 9 | unsigned int p1,p2; 10 | for(p1=0,p2=0; p2=0&&s[end]==' ') 6 | --end; 7 | int begin=end; 8 | while(begin>=0&&s[begin]!=' ') 9 | --begin; 10 | return end-begin; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /Leetcode/String/686. Repeated String Match.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int repeatedStringMatch(string A, string B) { 4 | int alen=A.length(); 5 | int blen=B.length(); 6 | int i,j,k,ans; 7 | for(i=0; i& height) { 4 | if (height.size()<3) 5 | return 0; 6 | int ans=0; 7 | vector::iterator left=height.begin(),right=height.end()-1; 8 | while(left+1 2 | using namespace std; 3 | 4 | #define INF 0x3f3f3f3f 5 | 6 | vectornums; 7 | vector>over; 8 | 9 | int solve(int begin, int end) { 10 | if(over[begin][end]!=INF) { 11 | return over[begin][end]; 12 | } 13 | if(begin==end) { 14 | return over[begin][end]=nums[begin]; 15 | } 16 | int t1=nums[begin]-solve(begin+1, end); 17 | int t2=nums[end]-solve(begin, end-1); 18 | return over[begin][end]=max(t1, t2); 19 | } 20 | 21 | int main() { 22 | int n; 23 | int i, t; 24 | scanf("%d", &n); 25 | nums=vector(n, 0); 26 | over=vector>(n, vector(n, INF)); 27 | for (i=0; i0?1:ret<0?2:0)< 2 | using namespace std; 3 | 4 | int main(){ 5 | int T; 6 | cin>>T; 7 | while(T--){ 8 | int n; 9 | int x1, y1, x2, y2; 10 | int ans=0; 11 | cin>>n; 12 | 13 | while(n--){ 14 | cin>>x1>>y1>>x2>>y2; 15 | ans+=(x2+1-x1)*(y2+1-y1); 16 | } 17 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | for (int n, m; cin >> n >> m; ) { 7 | vector > arr; 8 | for (int i = 0, d, p; i < n; cin >> d >> p, arr.emplace_back(d, p), i++) {} 9 | sort(arr.begin(), arr.end()); 10 | vector ans(n); 11 | ans[0] = arr[0].second; 12 | for (int i = 1; i < (int)arr.size(); ans[i] = max(ans[i - 1], arr[i].second), ++i) {} 13 | 14 | for (int x; m--; ) { 15 | cin >> x; 16 | cout << ans[upper_bound(arr.begin(), arr.end(), make_pair(x, 0x3f3f3f3f)) - 1 - arr.begin()] << endl; 17 | } 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Nowcoder/2019-校招实习-编程集合/2019-网易实习-2-被3整除.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/2019-校招实习-编程集合/2019-网易实习-2-被3整除.cpp -------------------------------------------------------------------------------- /Nowcoder/2019-校招实习-编程集合/2019-网易实习-3-安置路灯.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | int t, n; 8 | char ch; 9 | int i, ans; 10 | while(cin>>t) { 11 | while(t--) { 12 | vectorv; 13 | cin>>n; 14 | while(n--) { 15 | cin>>ch; 16 | v.push_back(ch); 17 | } 18 | ans=0; 19 | for (i=0; i 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | for (int n; cin >> n; ) { 7 | vector arr; 8 | for (int i = 0, x, y; i < n; cin >> x >> y, arr.push_back(x * 60 + y), ++i) {} 9 | sort(arr.begin(), arr.end()); 10 | int x, A, B; 11 | cin >> x >> A >> B; 12 | int ans = *(lower_bound(arr.begin(), arr.end(), A * 60 + B - x + 1) - 1); 13 | cout << ans / 60 << " " << ans % 60 << endl; 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Nowcoder/2019-校招实习-编程集合/2019-网易实习-7-矩形重叠.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/2019-校招实习-编程集合/2019-网易实习-7-矩形重叠.cpp -------------------------------------------------------------------------------- /Nowcoder/2019-校招实习-编程集合/2019-网易实习-8-牛牛的背包问题.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/2019-校招实习-编程集合/2019-网易实习-8-牛牛的背包问题.cpp -------------------------------------------------------------------------------- /Nowcoder/2019-校招实习-编程集合/2019-网易校招-1-表达式求值.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // freopen("test1.txt", "r", stdin); 6 | 7 | int a, b, c; 8 | while(scanf("%d %d %d", &a, &b, &c)!=EOF) { 9 | int ans1, ans2, t; 10 | t=max(a+b, a*b); 11 | ans1=max(t+c, t*c); 12 | t=max(b+c, b*c); 13 | ans2=max(t+a, t*a); 14 | printf("%d\n", max(ans1, ans2)); 15 | } 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /Nowcoder/2019-校招实习-编程集合/2019-网易校招-2-瞌睡.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main() { 5 | // freopen("test1.txt", "r", stdin); 6 | 7 | int n, k; 8 | while(scanf("%d %d", &n, &k)!=EOF) { 9 | vectorvalue(n); 10 | vectorflag(n); 11 | int i, v; 12 | int sum=0, ans=0; 13 | for (i=0; i 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | multisetS1; 8 | setS2; 9 | 10 | int m; 11 | cin >> m; 12 | while (m--) { 13 | int tem; 14 | cin >> tem; 15 | S1.insert(tem); 16 | S2.insert(tem); 17 | } 18 | 19 | set::iterator it; 20 | for (it = S2.begin(); it != S2.end(); ++it) 21 | cout << *it << ":" << S1.count(*it) << endl; 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /Nowcoder/DS Problem Set -- ZheJiangUniversity/5-24 树种统计.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/DS Problem Set -- ZheJiangUniversity/5-24 树种统计.cpp -------------------------------------------------------------------------------- /Nowcoder/DS Problem Set -- ZheJiangUniversity/5-31 判断笛卡尔树.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/DS Problem Set -- ZheJiangUniversity/5-31 判断笛卡尔树.cpp -------------------------------------------------------------------------------- /Nowcoder/DS Problem Set -- ZheJiangUniversity/5-33 地下迷宫探索.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/DS Problem Set -- ZheJiangUniversity/5-33 地下迷宫探索.cpp -------------------------------------------------------------------------------- /Nowcoder/DS Problem Set -- ZheJiangUniversity/5-35 城市间紧急救援.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/DS Problem Set -- ZheJiangUniversity/5-35 城市间紧急救援.cpp -------------------------------------------------------------------------------- /Nowcoder/DS Problem Set -- ZheJiangUniversity/5-4 是否同一颗二叉搜索树.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/DS Problem Set -- ZheJiangUniversity/5-4 是否同一颗二叉搜索树.cpp -------------------------------------------------------------------------------- /Nowcoder/DS Problem Set -- ZheJiangUniversity/5-5 堆中的路径.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | using namespace std; 4 | 5 | int N, M; 6 | int * H; 7 | 8 | void heapAdjust(int n) 9 | { 10 | int i = H[n]; 11 | int j; 12 | 13 | while ((j = n / 2) && H[j] > i) { 14 | H[n] = H[j]; 15 | n = j; 16 | } 17 | H[n] = i; 18 | } 19 | 20 | void printHeapPath(int pos) 21 | { 22 | while (pos != 1) { 23 | printf("%d ", H[pos]); 24 | pos /= 2; 25 | } 26 | printf("%d\n", H[pos]); 27 | 28 | } 29 | int main() 30 | { 31 | scanf("%d %d", &N, &M); 32 | H = new int[N + 1]; 33 | 34 | int i; 35 | for (i = 1; i <= N; ++i) { 36 | scanf("%d", H + i); 37 | heapAdjust(i); 38 | } 39 | 40 | while (M--) { 41 | scanf("%d", &i); 42 | printHeapPath(i); 43 | } 44 | 45 | return 0; 46 | } -------------------------------------------------------------------------------- /Nowcoder/DS Problem Set -- ZheJiangUniversity/5-50 畅通工程之局部最小花费问题.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/DS Problem Set -- ZheJiangUniversity/5-50 畅通工程之局部最小花费问题.cpp -------------------------------------------------------------------------------- /Nowcoder/DS Problem Set -- ZheJiangUniversity/5-53 两个有序序列的中位数.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | int N; 8 | scanf("%d", &N); 9 | 10 | int *n1 = (int *)malloc(N*sizeof(int)); 11 | int *n2 = (int *)malloc(N*sizeof(int)); 12 | int *n = (int *)malloc(2 * N*sizeof(int)); 13 | 14 | int i, j, k; 15 | 16 | for (i = 0; i < N; ++i) 17 | scanf("%d", n1 + i); 18 | 19 | for (i = 0; i < N; ++i) 20 | scanf("%d", n2 + i); 21 | 22 | for (i = 0, j = 0, k = 0; i < N&&j < N;) 23 | if (n1[i] < n2[j]) 24 | n[k++] = n1[i++]; 25 | else 26 | n[k++] = n2[j++]; 27 | 28 | while (i < N) 29 | n[k++] = n1[i++]; 30 | 31 | while (j < N) 32 | n[k++] = n2[j++]; 33 | 34 | printf("%d", n[(2 * N - 1) / 2]); 35 | 36 | free(n1); 37 | free(n2); 38 | free(n); 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /Nowcoder/DS Problem Set -- ZheJiangUniversity/5-6 列出连通集.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/DS Problem Set -- ZheJiangUniversity/5-6 列出连通集.cpp -------------------------------------------------------------------------------- /Nowcoder/Easy Problems Set/BFS 地牢逃脱.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Easy Problems Set/BFS 地牢逃脱.cpp -------------------------------------------------------------------------------- /Nowcoder/Easy Problems Set/Hash 下厨房.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Easy Problems Set/Hash 下厨房.cpp -------------------------------------------------------------------------------- /Nowcoder/Easy Problems Set/Math 分苹果.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Easy Problems Set/Math 分苹果.cpp -------------------------------------------------------------------------------- /Nowcoder/Easy Problems Set/Math 星际穿越.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | void test(){ 6 | long long h=-1; 7 | cout<<(unsigned long long)h<>h){ 17 | cout<<((int)sqrt(1+4*h)-1)/2< 2 | #include 3 | using namespace std; 4 | 5 | #define N 1010 6 | 7 | int isPrimer(int num) { 8 | int n1, in, i; 9 | for (i=2, in=sqrt(num)+1; i<=in; ++i) { 10 | if(num/i*i==num) { 11 | return 0; 12 | } 13 | } 14 | return 1; 15 | } 16 | 17 | int main() { 18 | int primer[N]; 19 | for (int i=1; i>n) { 25 | int ans=0; 26 | for (int i=1; i<=n/2; ++i) { 27 | ans+=primer[i]&&primer[n-i]; 28 | } 29 | cout< 3 | 4 | #define INF 100000000 5 | int carry[] = { 29,17,INF }; 6 | 7 | int main() 8 | { 9 | int a[3]; 10 | int b[3]; 11 | int ans[3]; 12 | scanf("%d.%d.%d %d.%d.%d", a + 2, a + 1, a, b + 2, b + 1, b); 13 | 14 | int i; 15 | int c = 0; 16 | for (i = 0; i < 3; ++i) 17 | { 18 | c += a[i]; 19 | c += b[i]; 20 | ans[i] = c%carry[i]; 21 | c /= carry[i]; 22 | } 23 | printf("%d.%d.%d", ans[2], ans[1], ans[0]); 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Acute Stroke.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Fundmental Practice/Acute Stroke.cpp -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/All Roads Lead to Rome.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Fundmental Practice/All Roads Lead to Rome.cpp -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Be Unique.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | #define NMAX 100000 9 | 10 | int main() 11 | { 12 | //freopen("dataIn.txt", "r", stdin); 13 | //freopen("dataOut.txt", "w", stdout); 14 | 15 | int N; 16 | int n[NMAX + 1]; 17 | int t; 18 | 19 | while (scanf("%d", &N) != EOF) { 20 | memset(n, 0, sizeof(n)); 21 | vectorv; 22 | 23 | int i; 24 | for (i = 1; i <= N; ++i) { 25 | scanf("%d", &t); 26 | if (!n[t]++) 27 | v.push_back(t); 28 | } 29 | for (i = 0; i < v.size();++i) 30 | if (n[v[i]] == 1) { 31 | printf("%d", v[i]); 32 | break; 33 | } 34 | if (i == v.size()) 35 | printf("None"); 36 | printf("\n"); 37 | } 38 | return 0; 39 | } -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Complete Binary Search Tree.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | 5 | #define NMAX 1001 6 | 7 | int tree[NMAX]; 8 | int order[NMAX]; 9 | int N; 10 | int pos = 1; 11 | 12 | int myCompare(const void *p1, const void *p2) 13 | { 14 | return *(int *)p1 - *(int *)p2; 15 | } 16 | void buildTree(int r) 17 | { 18 | if (r > N) 19 | return; 20 | int lson = r << 1, rson = (r << 1) + 1; 21 | buildTree(lson); 22 | tree[r] = order[pos++]; 23 | buildTree(rson); 24 | } 25 | int main() 26 | { 27 | scanf("%d", &N); 28 | int i; 29 | for (i = 1; i <= N; ++i) 30 | scanf("%d", order + i); 31 | qsort(order + 1, N, sizeof(int), myCompare); 32 | buildTree(1); 33 | for (printf("%d", tree[1]), i = 2; i <= N; ++i) 34 | printf(" %d", tree[i]); 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Consecutive Factors.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Fundmental Practice/Consecutive Factors.cpp -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Count PAT's.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #define MAXLEN 100000 5 | #define MODE 1000000007 6 | 7 | int main() 8 | { 9 | char str[MAXLEN + 1]; 10 | while (cin.getline(str, MAXLEN + 1)) { 11 | int i; 12 | int p = 0, pa = 0, pat = 0; 13 | for (i = 0; str[i]; ++i) 14 | if (str[i] == 'P') 15 | ++p; 16 | else if (str[i] == 'A') 17 | pa += p; 18 | else 19 | pat = (pat + pa) % MODE; 20 | cout << pat; 21 | } 22 | return 0; 23 | } -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Counting Ones.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Fundmental Practice/Counting Ones.cpp -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Deduplication on a Linked List.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Fundmental Practice/Deduplication on a Linked List.cpp -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Find More Coins.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Fundmental Practice/Find More Coins.cpp -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Forwards on Weibo.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Fundmental Practice/Forwards on Weibo.cpp -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Hashing.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | 5 | #define MSize 10000 6 | bool b[MSize] = { false }; 7 | 8 | int isPrime(int n) 9 | { 10 | if (n == 1) 11 | return false; 12 | int q = (int)sqrt(n); 13 | int i; 14 | for (i = 2; i <= q; ++i) 15 | if (n%i == 0) 16 | return false; 17 | return true; 18 | } 19 | int main() 20 | { 21 | int size, N; 22 | scanf("%d %d", &size, &N); 23 | while (!isPrime(size)) 24 | ++size; 25 | int i; 26 | int t, hash; 27 | while (N--) { 28 | scanf("%d", &t); 29 | for (i = 0; i < size; ++i) { 30 | hash = (t + i*i) % size; 31 | if (!b[hash]) { 32 | b[hash] = true; 33 | printf("%d%s", hash, N ? " " : "\n"); 34 | break; 35 | } 36 | } 37 | if (i == size) 38 | printf("-%c", N ? ' ' : '\n'); 39 | } 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Hello World for U.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | #define MAXLNE 80 9 | 10 | int main() 11 | { 12 | int N; 13 | int n1, n2; 14 | char str[MAXLNE + 1]; 15 | 16 | while (scanf("%s", str) != EOF) { 17 | N = strlen(str); 18 | n1 = (N + 2) / 3; 19 | n2 = N - 2 * n1; 20 | 21 | int i; 22 | for (i = 1; i < n1; ++i) 23 | cout << str[i - 1] << setw(n2 + 1) << right << str[N - i] << endl; 24 | str[N - i + 1] = '\0'; 25 | cout << str + i - 1 << endl; 26 | } 27 | return 0; 28 | } -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Insert or Merge.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Fundmental Practice/Insert or Merge.cpp -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Insertion or Heap Sort.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Fundmental Practice/Insertion or Heap Sort.cpp -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Kuchiguse.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Fundmental Practice/Kuchiguse.cpp -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Linked List Sorting.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Fundmental Practice/Linked List Sorting.cpp -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Longest Symmetric String.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Fundmental Practice/Longest Symmetric String.cpp -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Path of Equal Weight.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Fundmental Practice/Path of Equal Weight.cpp -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Pop Sequence.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | #define MAX 1000 8 | 9 | int main() 10 | { 11 | int maxSize, N, K; 12 | int elem; 13 | int sequence[MAX + 1]; 14 | 15 | while (scanf("%d %d %d", &maxSize, &N, &K) != EOF) { 16 | while (K--) { 17 | stacks; 18 | int i; 19 | for (i = 1; i <= N; ++i) 20 | scanf("%d", sequence + i); 21 | 22 | for (i = 1, elem = 0; i <= N; ++i) { 23 | if (elem < sequence[i]) { 24 | while (++elem < sequence[i]) 25 | s.push(elem); 26 | if ((int)s.size() >= maxSize) 27 | break; 28 | } 29 | else if (s.top() != sequence[i]) 30 | break; 31 | else 32 | s.pop(); 33 | } 34 | printf("%s\n", s.empty() ? "YES" : "NO"); 35 | } 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Recover the Smallest Number.cpp: -------------------------------------------------------------------------------- 1 | # include 2 | # include 3 | #include 4 | # include 5 | using namespace std; 6 | 7 | const int _size = 10000; 8 | string num[_size]; 9 | bool cmp(const string& a, const string& b) { return a + b< b + a; } 10 | int main() 11 | { 12 | int n, i; 13 | cin >> n; 14 | for (i = 0; i < n; i++) 15 | cin >> num[i]; 16 | sort(num, num + n, cmp); 17 | string out; 18 | for (i = 0; i < n; i++) 19 | out += num[i]; 20 | for (i = 0; i < out.size() && out[i] == '0'; i++); 21 | if (i == out.size()) 22 | printf("0"); 23 | else 24 | printf("%s", out.c_str() + i); 25 | printf("\n"); 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Root of AVL Tree.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Fundmental Practice/Root of AVL Tree.cpp -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Set Similarity.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define NMAX 51 7 | 8 | int main() 9 | { 10 | sets[51]; 11 | int N, M, K; 12 | scanf("%d", &N); 13 | int i,t; 14 | for (i = 1; i <= N; ++i) { 15 | scanf("%d", &M); 16 | while (M--) { 17 | scanf("%d", &t); 18 | s[i].insert(t); 19 | } 20 | } 21 | int nc; 22 | scanf("%d", &K); 23 | while (K--) { 24 | int q1, q2; 25 | scanf("%d%d", &q1, &q2); 26 | nc = 0; 27 | for (set::iterator iter = s[q1].begin();iter!=s[q1].end(); ++iter) { 28 | if (s[q2].find(*iter) != s[q2].end()) 29 | ++nc; 30 | } 31 | printf("%.1f%%\n", 100.0*nc / (s[q1].size() + s[q2].size() - nc)); 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Speech Patterns.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Fundmental Practice/Speech Patterns.cpp -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/Stack.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Fundmental Practice/Stack.cpp -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/String Subtraction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | #define MAXLEN 10000 7 | 8 | int main() 9 | { 10 | char s1[MAXLEN + 1]; 11 | char s2[MAXLEN + 1]; 12 | char s[MAXLEN + 1]; 13 | 14 | while (cin.getline(s1, MAXLEN)) { 15 | cin.getline(s2, MAXLEN); 16 | 17 | setsubSet; 18 | int i, j; 19 | for (i = 0; s2[i]; ++i) 20 | subSet.insert(s2[i]); 21 | 22 | for (i = 0, j = 0; s1[i]; ++i) 23 | if (subSet.find(s1[i]) == subSet.end()) 24 | s[j++] = s1[i]; 25 | s[j] = '\0'; 26 | 27 | printf("%s\n", s); 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/The Dominant Color.cpp: -------------------------------------------------------------------------------- 1 | #define _CRT_SECURE_NO_WARNINGS 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int N, M; 9 | while (scanf("%d %d", &N, &M) != EOF) { 10 | mapm; 11 | map::iterator iter; 12 | 13 | int i, pixValue; 14 | int totalPix = N*M; 15 | for (i = totalPix; i--;) { 16 | scanf("%d", &pixValue); 17 | if ((iter = m.find(pixValue)) == m.end()) 18 | m[pixValue] = 1; 19 | else 20 | ++iter->second; 21 | } 22 | 23 | int half = totalPix / 2; 24 | for (iter = m.begin(); iter != m.end(); ++iter) 25 | if (iter->second > half) 26 | break; 27 | printf("%d\n", iter->first); 28 | } 29 | return 0; 30 | } -------------------------------------------------------------------------------- /Nowcoder/Fundmental Practice/To Fill or Not to Fill.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Fundmental Practice/To Fill or Not to Fill.cpp -------------------------------------------------------------------------------- /Nowcoder/Middle Problems Set/BinarySearch 分田地.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Middle Problems Set/BinarySearch 分田地.cpp -------------------------------------------------------------------------------- /Nowcoder/Middle Problems Set/DP or Cursively 放苹果.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Middle Problems Set/DP or Cursively 放苹果.cpp -------------------------------------------------------------------------------- /Nowcoder/Middle Problems Set/DP 合唱.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Middle Problems Set/DP 合唱.cpp -------------------------------------------------------------------------------- /Nowcoder/Middle Problems Set/DP 合唱团.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Middle Problems Set/DP 合唱团.cpp -------------------------------------------------------------------------------- /Nowcoder/Middle Problems Set/Tree 游历魔法王国.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/Nowcoder/Middle Problems Set/Tree 游历魔法王国.cpp -------------------------------------------------------------------------------- /Nowcoder/Middle Problems Set/字典序.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | long long getSeg(int n1) { 6 | long long ret=0; 7 | for (int i=0; i0&&i>n>>m) { 29 | 30 | } 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /OJ/Google/Kickstart 2018 Round H B.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int solve(int N){ 5 | string s; 6 | cin>>s; 7 | int res=0, sum=0; 8 | for(int len=(N+1)>>1, i=0; i=0? s[i-len]-'0': 0; 11 | res=max(res, sum); 12 | } 13 | return res; 14 | } 15 | 16 | int main(){ 17 | int T; 18 | int N; 19 | scanf("%d", &T); 20 | for(int t=1; t<=T; ++t){ 21 | scanf("%d", &N); 22 | printf("Case #%d: %d\n", t, solve(N)); 23 | } 24 | return 0; 25 | } -------------------------------------------------------------------------------- /OJ/OJ解题报告.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/OJ/OJ解题报告.docx -------------------------------------------------------------------------------- /OJ/POJ 1151 线段树/POJ 1151.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/OJ/POJ 1151 线段树/POJ 1151.cpp -------------------------------------------------------------------------------- /OJ/POJ 1182 并查集/POJ 1182 .cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/OJ/POJ 1182 并查集/POJ 1182 .cpp -------------------------------------------------------------------------------- /OJ/POJ 1330 LCA/POJ 1330.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/OJ/POJ 1330 LCA/POJ 1330.cpp -------------------------------------------------------------------------------- /OJ/POJ 1568 极大极小值剪枝/POJ 1568.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/OJ/POJ 1568 极大极小值剪枝/POJ 1568.cpp -------------------------------------------------------------------------------- /OJ/POJ 1568 极大极小值剪枝/input.txt: -------------------------------------------------------------------------------- 1 | ? 2 | .... 3 | .xo. 4 | .ox. 5 | .... 6 | ? 7 | o... 8 | .ox. 9 | .xxx 10 | xooo 11 | ? 12 | .... 13 | .... 14 | .... 15 | .... 16 | ? 17 | .... 18 | xxx. 19 | .... 20 | .... 21 | $ -------------------------------------------------------------------------------- /OJ/POJ 1611 并查集/POJ 1611.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/OJ/POJ 1611 并查集/POJ 1611.cpp -------------------------------------------------------------------------------- /OJ/POJ 2299 树状数组/POJ 2299 .cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/OJ/POJ 2299 树状数组/POJ 2299 .cpp -------------------------------------------------------------------------------- /OJ/TOJ/1003 美人鱼/1003 美人鱼.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/OJ/TOJ/1003 美人鱼/1003 美人鱼.cpp -------------------------------------------------------------------------------- /OJ/TOJ/1005 多米诺骨牌/1005 多米诺骨牌.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | const int maxn=200000+10; 6 | struct node { 7 | int p; 8 | int l; 9 | }; 10 | 11 | int n,q; 12 | int i,maxd,x,y; 13 | node po[maxn]; 14 | int cost[maxn]; 15 | 16 | int main() { 17 | cin>>n; 18 | for(i=1; i<=n; ++i) 19 | //cin>>po[i].p>>po[i].l; 20 | scanf("%d %d",&po[i].p ,&po[i].l); 21 | cost[1]=0; 22 | maxd=po[1].p+po[1].l; 23 | for(i=2; i<=n; ++i) { 24 | if(maxd>q; 31 | for(i=0; i>x>>y; 33 | scanf("%d %d",&x,&y); 34 | printf("%d\n",cost[y]-cost[x]); 35 | //cout< 2 | int main() { 3 | int L,W; 4 | while(~scanf("%d %d",&L,&W)) { 5 | if ((L&1)^(W&1)) 6 | printf("God save the pig!\n"); 7 | else 8 | printf("My God!\n"); 9 | } 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /OJ/TOJ/1013 Escape/1013 Escape.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | long long x, p,n; 7 | while(~scanf("%I64d %I64d %I64d",&x,&p,&n)&&x&&p&&n) { 8 | long long ans=0,i; 9 | for (i=p; i>p-n; --i) 10 | if ((x>>i)&1) 11 | ans=(ans<<1)|1; 12 | else 13 | ans<<=1; 14 | printf("%I64d\n",ans); 15 | } 16 | return 0; 17 | } 18 | 19 | //int main() { 20 | // long long x, p,n; 21 | // while(cin>>x>>p>>n) { 22 | // if (x||p||n) { 23 | // long long ans=0,i; 24 | // for (i=p; i>p-n; --i) 25 | // if ((x>>i)&1) 26 | // ans=(ans<<1)|1; 27 | // else 28 | // ans<<=1; 29 | // cout< 2 | #include 3 | 4 | int main(){ 5 | int n; 6 | bool c[410]; 7 | while(~scanf("%d",&n)&&n){ 8 | memset(c,0,sizeof(c)); 9 | int i,k; 10 | for (i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main() { 7 | int N; 8 | while(~scanf("%d",&N)&&N) { 9 | int nmax=sqrt(2*N); 10 | int mid,n; 11 | int ans=0; 12 | for (n=1; n<=nmax; ++n) { 13 | mid=N/n; 14 | if((n&1)&&mid*n==N) 15 | ++ans; 16 | else if (!(n&1)&&(mid*2+1)*n/2==N) 17 | ++ans; 18 | } 19 | printf("%d\n",ans); 20 | } 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /OJ/TOJ/1019 Trouble/1019 Trouble.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | const int maxn=5100; 5 | int num[maxn]; 6 | 7 | int main() { 8 | int n; 9 | while(cin>>n) 10 | if (n) { 11 | int i; 12 | for(i=0; i>num[i]; 14 | if (n&1) { 15 | printf("No\n"); 16 | continue; 17 | } 18 | sort(num,num+n); 19 | int h=num[0]+num[n-1]; 20 | for(i=1; i>1; ++i) 21 | if(num[i]+num[n-i-1]!=h) 22 | break; 23 | if(i==n>>1) 24 | printf("Yes\n"); 25 | else 26 | printf("No\n" ); 27 | } 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /OJ/TOJ/1035 最优比赛策略/1035 最优比赛策略.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | int main(){ 7 | int N; 8 | while(cin>>N){ 9 | vectorn(N); 10 | int i; 11 | for(i=0;i>n[i]; 13 | sort(n.begin(),n.end()); 14 | int ans=0,sum=0; 15 | for (i=0;i 2 | #include 3 | 4 | bool s[1005][1005]; 5 | void init() { 6 | int i,j,k; 7 | for(i=0; i<=1000; ++i) { 8 | s[i][0]=false; 9 | for (j=1; j 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | int main() { 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /OJ/TOJ/TOJ2017/2/2.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/OJ/TOJ/TOJ2017/2/2.cpp -------------------------------------------------------------------------------- /OJ/TOJ/TOJ2017/3/3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std; 13 | 14 | int main(){ 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /OJ/ZOJ 3794 最短路径/ZOJ 3794.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/OJ/ZOJ 3794 最短路径/ZOJ 3794.cpp -------------------------------------------------------------------------------- /book/acm-book.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/book/acm-book.pdf -------------------------------------------------------------------------------- /book/acm-icpc-template.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/book/acm-icpc-template.pdf -------------------------------------------------------------------------------- /book/mine/algorithm Template.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/book/mine/algorithm Template.docx -------------------------------------------------------------------------------- /book/mine/algorithm Template.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/book/mine/algorithm Template.pdf -------------------------------------------------------------------------------- /book/mine/dijkstra.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/book/mine/dijkstra.cpp -------------------------------------------------------------------------------- /book/mine/lca.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/book/mine/lca.cpp -------------------------------------------------------------------------------- /book/mine/spfa.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/book/mine/spfa.cpp -------------------------------------------------------------------------------- /book/mine/spfa1.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/book/mine/spfa1.cpp -------------------------------------------------------------------------------- /book/mine/sspath.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/book/mine/sspath.cpp -------------------------------------------------------------------------------- /book/mine/treeArray.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/book/mine/treeArray.cpp -------------------------------------------------------------------------------- /book/mine/unionSet.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/book/mine/unionSet.cpp -------------------------------------------------------------------------------- /book/动态规划加速原理之四边形不等式.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/book/动态规划加速原理之四边形不等式.pdf -------------------------------------------------------------------------------- /book/北京大学--DP.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/book/北京大学--DP.pdf -------------------------------------------------------------------------------- /book/最短路径spfa算法.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/book/最短路径spfa算法.pdf -------------------------------------------------------------------------------- /book/线段树与树状数组.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/book/线段树与树状数组.pdf -------------------------------------------------------------------------------- /book/背包问题.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/book/背包问题.pdf -------------------------------------------------------------------------------- /book/贪心算法.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BaiJiazm/DS-Algorithm/0b4f96f1b474c2e6dbc26f195a03cea4310013de/book/贪心算法.pdf --------------------------------------------------------------------------------