[
  {
    "path": ".codacy.yml",
    "content": "---\nexclude_paths:\n  - '**/*.md'\n  - 'README.md'\n"
  },
  {
    "path": ".codecov.yml",
    "content": "codecov:\n  notify:\n    require_ci_to_pass: yes\n\ncoverage:\n  round: up\n  range: 0..10\n  precision: 2\n\n  status:\n    project: yes\n    patch: yes\n    changes: yes\n\ncomment:\n  layout: \"reach, diff, flags, files\"\n  behavior: default\n  require_changes: false  # if true: only post the comment if coverage changes\n  require_base: yes        # [yes :: must have a base report to post]\n  require_head: yes       # [yes :: must have a head report to post]\n  branches: null\n"
  },
  {
    "path": ".gitignore",
    "content": "# Binaries for programs and plugins\n*.exe\n*.exe~\n*.dll\n*.so\n*.dylib\n\n# Test binary, build with `go test -c`\n*.test\n\n# Output of the go coverage tool, specifically when used with LiteIDE\n*.out\n\n# JetBrains\n.idea/\n\n# vs code\n.vscode/\n\n# mac \n.DS_Store\n"
  },
  {
    "path": ".travis.yml",
    "content": "language: go\n\ngo:\n  - \"1.12.x\"\n\ngo_import_path: leetcode\n\ninstall:\n  - go get -t -v ./...\n\nscript:\n  - go test ./... -race -coverprofile=coverage.txt -covermode=atomic\n\nafter_success:\n    - bash <(curl -s https://codecov.io/bash)\n\nbranches:\n  only:\n    - master\n"
  },
  {
    "path": "LICENSE",
    "content": "This is free and unencumbered software released into the public domain.\n\nAnyone is free to copy, modify, publish, use, compile, sell, or\ndistribute this software, either in source code form or as a compiled\nbinary, for any purpose, commercial or non-commercial, and by any\nmeans.\n\nIn jurisdictions that recognize copyright laws, the author or authors\nof this software dedicate any and all copyright interest in the\nsoftware to the public domain. We make this dedication for the benefit\nof the public at large and to the detriment of our heirs and\nsuccessors. We intend this dedication to be an overt act of\nrelinquishment in perpetuity of all present and future rights to this\nsoftware under copyright law.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR\nOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,\nARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\nOTHER DEALINGS IN THE SOFTWARE.\n\nFor more information, please refer to <http://unlicense.org>\n"
  },
  {
    "path": "Makefile",
    "content": "# Basic go commands\nGOCMD=go\nGOBUILD=$(GOCMD) build\nGOCLEAN=$(GOCMD) clean\nGOTEST=$(GOCMD) test\nGOGET=$(GOCMD) get\nGOFMT=$(GOCMD) fmt\n\ntest:\n\t@echo \"unit test\"\n\t$(GOTEST) ./...\n\nclean:\n\t@echo \"clean test cache\"\n\t$(GOCLEAN) -testcache\n\nfmt:\n\t@echo \"fmt code\"\n\t$(GOFMT) ./...\n"
  },
  {
    "path": "_config.yml",
    "content": "theme: jekyll-theme-architect"
  },
  {
    "path": "bak.README.md",
    "content": "# Go Solution for Leetcode algorithm problems\n\n[![Build Status](https://travis-ci.org/zwfang/leetcode.svg?branch=master)](https://travis-ci.org/zwfang/leetcode)\n[![codecov](https://codecov.io/gh/zwfang/leetcode/branch/master/graph/badge.svg)](https://codecov.io/gh/zwfang/leetcode)\n[![Codacy Badge](https://api.codacy.com/project/badge/Grade/86cf2613fa544ab5b254e2a7e5d9deb8)](https://www.codacy.com/app/zwfang/leetcode?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=zwfang/leetcode&amp;utm_campaign=Badge_Grade)\n[![Go Report Card](https://goreportcard.com/badge/github.com/zwfang/leetcode)](https://goreportcard.com/report/github.com/zwfang/leetcode)\n\ncontinually updating 😃.\n\n### [view by sorted](./solutions/README.md)\n\n### Array\n* [1. Two Sum](solutions/0001_two_sum/twosum.go)&nbsp;&nbsp;&nbsp;*`lookup table;`*&nbsp;&nbsp;*`hash table`*\n* [4. Median of Two Sorted Arrays](solutions/0004_median_of_two_sorted_arrays/motsa.go)&nbsp;&nbsp;&nbsp;*`binary search;`*&nbsp;&nbsp;*`divide and conquer`*\n* [11. Container With Most Water](solutions/0011_container_with_most_water/container_with_most_water.go)&nbsp;&nbsp;&nbsp;*`double index;`*&nbsp;&nbsp;*`array`*\n* [15. 3Sum](solutions/0015_3Sum/3sum.go)&nbsp;&nbsp;&nbsp;*`double index;`*&nbsp;&nbsp;*`array`*\n* [26. Remove Duplicates from Sorted Array](solutions/0026_remove_duplicates_from_sorted_array/rdfsa.go)&nbsp;&nbsp;&nbsp;*`double index;`*&nbsp;&nbsp;*`array`*\n* [27. Remove Element](solutions/0027_remove_element/remove_element.go)&nbsp;&nbsp;&nbsp;*`double index;`*&nbsp;&nbsp;*`array`*\n* [48. Rotate Image](solutions/0048_rotate_image/rotate_image.go)\n* [53. Maximum Subarray](solutions/0053_maximum_subarray/maximum_subarray.go)&nbsp;&nbsp;&nbsp;*`dynamic programming`*\n* [75. Sort Colors](solutions/0075_sort_colors/sort_colors.go)&nbsp;&nbsp;&nbsp;*`sort;`*&nbsp;&nbsp;*`array`*\n* [80. Remove Duplicates from Sorted Array II](solutions/0080_remove_duplicates_from_sorted_array2/rdfsa2.go)&nbsp;&nbsp;&nbsp;*`double index;`*&nbsp;&nbsp;*`array`*\n* [88. Merge Sorted Array](solutions/0088_merge_sorted_array/msa.go)&nbsp;&nbsp;&nbsp;*`sort;`*&nbsp;&nbsp;*`array`*\n* [121. Best Time to Buy and Sell Stock](solutions/0121_best_time_to_buy_and_sell_stock/maxprofit.go)&nbsp;&nbsp;&nbsp;*`dynamic programming;`*&nbsp;&nbsp;*`array`*\n* [122. Best Time to Buy and Sell Stock II](solutions/0122_best_time_to_buy_and_sell_stock_2/maxprofit.go)&nbsp;&nbsp;&nbsp;*`greedy;`*&nbsp;&nbsp;*`array`*\n* [136. Single Number](solutions/0136_single_number/single_number.go)&nbsp;&nbsp;&nbsp;*`hash table;`*&nbsp;&nbsp;*`bit manipulation`*\n* [167. Two Sum II - Input array is sorted](solutions/0167_two_sum2/two_sum2.go)&nbsp;&nbsp;&nbsp;*`double index;`*&nbsp;&nbsp;*`binary search`*\n* [179. Largest Number](solutions/0179_largest_number/ln.go)&nbsp;&nbsp;&nbsp;*`sort`*\n* [200. Number of Islands](solutions/0200_number_of_island/number_of_island.go)&nbsp;&nbsp;&nbsp;*`dfs;`*&nbsp;&nbsp;*`bfs`*\n* [209. Minimum Size Subarray Sum](solutions/0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)&nbsp;&nbsp;&nbsp;*`sliding window`*\n* [215. Kth Largest Element in an Array](solutions/0215_kth_largest_element_in_an_array/kthleiaa.go)&nbsp;&nbsp;&nbsp;*`sort`*\n* [217. Contains Duplicate](solutions/0217_contains_duplicate/contains_duplicate.go)&nbsp;&nbsp;&nbsp;*`map`*\n* [219. Contains Duplicate II](solutions/0219_contains_duplicate_2/contains_duplicate_2.go)&nbsp;&nbsp;&nbsp;*`map`*\n* [283. Move Zeroes(solution1)](solutions/0283_move_zeroes/move_zeroes.go)&nbsp;&nbsp;&nbsp;*`sliding window`*\n* [283. Move Zeroes(solution2)](solutions/0283_move_zeroes/move_zeroes2.go)&nbsp;&nbsp;&nbsp;*`sliding window`*\n* [303. Range Sum Query - Immutable](solutions/0303_range_sum_query/rsqim.go)\n* [347. Top K Frequent Elements](solutions/0347_top_k_frequent_elements/topkfe.go)&nbsp;&nbsp;&nbsp;*`hash table;`*&nbsp;&nbsp;*`heap`*\n* [349. Intersection of Two Arrays](solutions/0349_intersection_of_2_arrays/intersection_of_two_arrays.go)&nbsp;&nbsp;&nbsp;*`set`*\n* [350. Intersection of Two Arrays II](solutions/0350_intersection_of_two_arrays2/intersection_of_two_arrays2.go)&nbsp;&nbsp;&nbsp;*`hash table`*\n* [447. Number of Boomerangs](solutions/0447_number_of_boomerangs/number_of_boomerangs.go)&nbsp;&nbsp;&nbsp;*`hash table`*\n* [454. 4Sum II](solutions/0454_4sum2/4sum2.go)&nbsp;&nbsp;&nbsp;*`hash table`*\n* [674. Longest Continuous Increasing Subsequence](solutions/0674_longest_continuous_increasing_subsequence/lcis.go)\n* [713. Subarray Product Less Than K](solutions/0713_subarray_product_less_than_k/spltk.go)&nbsp;&nbsp;&nbsp;*`sliding window`*&nbsp;&nbsp;*`array`*\n* [717. 1-bit and 2-bit Characters](solutions/0717_1_bit_and_2_bit_characters/1bitand2bitc.go)\n* [747. Largest Number At Least Twice of Others](solutions/0747_largest_number_at_least_twice_of_others/largest_number_at_least_twice_of_others.go)\n\n### Stack\n* [155. Min Stack](solutions/0155_min_stack/min_stack.go)\n* [735. Asteroid Collision](solutions/0735_asteroid_collision/ac.go)\n\n### String\n* [3. Longest Substring Without Repeating Characters](solutions/0003_longest_substring_without_repeating_characters/longest_substring_without_repeating_characters.go)&nbsp;&nbsp;&nbsp;*`sliding window;`*&nbsp;&nbsp;*`hash table`*\n* [14. Longest Common Prefix](solutions/0014_longest_common_prefix/lcp.go)\n* [17. Letter Combinations of a Phone Number](solutions/0017_letter_combination_of_a_phone_number/letter_combination_of_phone_number.go)&nbsp;&nbsp;&nbsp;*`tree`*\n* [20. Valid Parentheses](solutions/0020_valid_parentheses/valid_parentheses.go)&nbsp;&nbsp;&nbsp;*`stack`*\n* [28. Implement strStr()](solutions/0028_implement_strstr/implement_strstr.go)&nbsp;&nbsp;&nbsp;*`double index`*\n* [58. Length of Last Word](solutions/0058_length_of_last_word/len_of_last_word.go)\n* [67. Add Binary](solutions/0067_add_binary/add_binary.go)&nbsp;&nbsp;&nbsp;*`brute force`*\n* [76. Minimum Window Substring](solutions/0076_minimum_window_substring/minimum_window_substring.go) &nbsp;&nbsp;&nbsp;*`sliding window`*\n* [125. Valid Palindrome](solutions/0125_valid_palindrome/valid_palindrome.go)&nbsp;&nbsp;&nbsp;*`string;`*&nbsp;&nbsp;*`double index`*\n* [165. Compare Version Numbers](solutions/0165_compare_version_numbers/compare_version_numbers.go)\n* [344. Reverse String](solutions/0344_reverse_string/reverse_string.go)&nbsp;&nbsp;&nbsp;*`string;`*&nbsp;&nbsp;*`double index`*\n* [345. Reverse Vowels of a String](solutions/0345_reverse_vowels_of_a_string/reverse_vowels.go)&nbsp;&nbsp;&nbsp;*`string;`*&nbsp;&nbsp;*`double index`*\n* [438. Find All Anagrams in a String](solutions/0438_all_anagrams_in_a_string/all_anagrams_in_a_string.go)&nbsp;&nbsp;&nbsp;*`sliding window`*\n* [557. Reverse Words in a String III](solutions/0557_reverse_words_in_a_string_3/reverse_words_in_a_string_3.go)\n* [1021. Remove Outermost Parentheses](solutions/1021_Remove_Outermost_Parentheses/remove_outmost_parentheses.go)&nbsp;&nbsp;&nbsp;*`stack`*\n\n### Linked List\n* [2. Add Two Numbers](solutions/0002_add_two_numbers/add_two_numbers.go)&nbsp;&nbsp;&nbsp;*`recursion;`*&nbsp;&nbsp;*`math`*\n* [19. Remove Nth Node From End of List](solutions/0019_remove_nth_node_from_end_of_list/remove_nth_node_from_end_of_list.go)&nbsp;&nbsp;&nbsp;*`two pointers`*\n* [21. Merge Two Sorted Lists](solutions/0021_merge_two_sorted_lists/mergeTwoLists.go)\n* [23. Merge k Sorted Lists](solutions/0023_merge_k_sorted_lists/mksl.go)&nbsp;&nbsp;&nbsp;*`heap`*\n* [24. Swap Nodes in Pairs](solutions/0024_swap_nodes_in_pairs/swap_nodes_in_pairs.go)\n* [25. Reverse Nodes in k-Group](solutions/0025_reverse_nodes_in_k_group/reverse_node_k_group.go)\n* [61. Rotate List](solutions/0061_rotate_list/rotate_list.go)\n* [82. Remove Duplicates from Sorted List II](solutions/0082_remove_duplicates_from_sorted_list_2/rdfsl.go)\n* [83. Remove Duplicates from Sorted List](solutions/0083_remove_duplicates_from_sorted_list/rdfsl.go)\n* [86. Partition List](solutions/0086_partition_list/partition_list.go)&nbsp;&nbsp;&nbsp;*`two pointers`*\n* [92. Reverse Linked List II](solutions/0092_reverse_linked_list_2/reverse_linked_list2.go)\n* [148. Sort List](solutions/148_Sort_List/sortlist.go)&nbsp;&nbsp;&nbsp;*`sort`*\n* [203. Remove Linked List Elements](solutions/0203_remove_linked_list_elements/remove_linked_list_elements.go)\n* [206. Reverse Linked List](solutions/0206_reverse_linked_list/reverse_linked_list.go)\n* [237. Delete Node in a Linked List](solutions/0237_delete_node_in_a_linked_list/dniall.go)\n* [328. Odd Even Linked List](solutions/0328_odd_even_linked_list/odd_even_linked_list.go)\n\n### Dynamic Programming\n* [62. Unique Paths](solutions/0062_unique_paths/unique_paths.go)&nbsp;&nbsp;&nbsp;*`array`*\n* [63. Unique Paths 2](solutions/0063_unique_paths_2/unique_paths2.go)&nbsp;&nbsp;&nbsp;*`array`*\n* [64. Minimum Path Sum](solutions/0064_minimum_path_sum/minimum_path_sum.go)&nbsp;&nbsp;&nbsp;*`array`*\n* [70. Climbing Stairs](solutions/0070_climbing_stairs/climbing_stairs.go)\n* [120. Triangle](solutions/0120_triangle/triangle.go)&nbsp;&nbsp;&nbsp;*`array`*\n* [198. House Robber](solutions/0198_house_robber/house_robber.go)\n* [300. Longest Increasing Subsequence](solutions/0300_longest_increasing_subsequence/lis.go)\n* [304. Range Sum Query 2D - Immutable](solutions/304_Range_Sum_Query_2D/rsq.go)\n* [343. Integer Break](solutions/0343_integer_break/integer_break.go)&nbsp;&nbsp;&nbsp;*`math`*\n* [376. Wiggle Subsequence](solutions/0376_wiggle_subsequence/wiggle_subsequence.go)&nbsp;&nbsp;&nbsp;*`greedy;`*&nbsp;&nbsp;*`dynamic programming`*\n* [416. Partition Equal Subset Sum](solutions/0416_partition_equal_subset_sum/partition_equal_subset_sum.go)&nbsp;&nbsp;*`0-1 knapsack problem`*\n* [435. Non-overlapping Intervals](solutions/0435_non_overlapping_intervals/dp_solution.go)&nbsp;&nbsp;&nbsp;*`greedy;`*&nbsp;&nbsp;*`dynamic programming`*&nbsp;&nbsp;*`0-1 knapsack problem`*\n\n### Greedy\n* [392. Is Subsequence](solutions/0392_is_subsequence/is_subsequence.go)\n* [435. Non-overlapping Intervals](solutions/0435_non_overlapping_intervals/greedy_solution.go)&nbsp;&nbsp;&nbsp;*`greedy;`*&nbsp;&nbsp;*`dynamic programming`*\n* [455. Assign Cookies](solutions/0455_assign_cookies/assign_cookies.go)\n\n### Backtracking\n* [77. Combinations](solutions/0077_combinations/combinations.go)&nbsp;&nbsp;&nbsp;*`combine`*\n* [79. Word Search](solutions/0079_word_search/word_search.go)&nbsp;&nbsp;&nbsp;*`array`*\n\n### Tree\n* [94. Binary Tree Inorder Traversal](solutions/0094_binary_tree_inorder_traversal/binary_tree_inorder_traversal.go)&nbsp;&nbsp;&nbsp;*`binary tree;`*&nbsp;&nbsp;&nbsp;*`stack`*\n* [100. Same Tree](solutions/0100_same_tree/same_tree.go)&nbsp;&nbsp;&nbsp;*`binary tree;`*&nbsp;&nbsp;&nbsp;*`dfs`*\n* [101. Symmetric Tree](solutions/0101_symmetric_tree/symmetric_tree.go)&nbsp;&nbsp;&nbsp;*`binary tree;`*&nbsp;&nbsp;&nbsp;*`dfs;`*&nbsp;&nbsp;*`bfs;`*\n* [102. Binary Tree Level Order Traversal](solutions/0102_binary_tree_level_order_traversal/binary_tree_level_order_traversal.go)&nbsp;&nbsp;&nbsp;*`binary tree;`*&nbsp;&nbsp;&nbsp;*`dfs`*\n* [104. Maximum Depth of Binary Tree](solutions/0104_maximun_depth_of_binary_tree/maxdobt.go)&nbsp;&nbsp;&nbsp;*`binary tree depth`*\n* [107. Binary Tree Level Order Traversal II](solutions/0107_binary_tree_level_order_traversal_2/binary_tree_level_order_traversal2.go)&nbsp;&nbsp;&nbsp;*`binary tree;`*&nbsp;&nbsp;&nbsp;*`bfs`*\n* [111. Minimum Depth of Binary Tree](solutions/0111_minimum_depth_of_binary_tree/minimum_depth_of_binary_tree.go)&nbsp;&nbsp;&nbsp;*`binary tree;`*&nbsp;&nbsp;&nbsp;*`dfs`*\n* [112. Path Sum](solutions/0112_path_sum/path_sum.go)&nbsp;&nbsp;&nbsp;*`binary tree;`*&nbsp;&nbsp;&nbsp;*`dfs`*\n* [144. Binary Tree Preorder Traversal](solutions/0144_binary_tree_preorder_traversal/binary_tree_preorder_traversal.go)&nbsp;&nbsp;&nbsp;*`binary tree;`*&nbsp;&nbsp;&nbsp;*`pre-order traversal`*\n* [208. Implement Trie (Prefix Tree)](solutions/0208_implement_trie_prefix_tree/impltrie.go)&nbsp;&nbsp;&nbsp;*`trie`*\n* [226. Invert Binary Tree](solutions/0226_invert_binary_tree/invert_binary_tree.go)&nbsp;&nbsp;&nbsp;*`binary tree`*\n* [211. Add and Search Word - Data structure design](solutions/0211_add_and_search_word/add_and_search_word.go)&nbsp;&nbsp;&nbsp;*`trie`*\n* [235. Lowest Common Ancestor of a Binary Search Tree](solutions/0235_lowest_common_ancestor_of_a_binary_search_tree/lcaoabst.go)&nbsp;&nbsp;&nbsp;*`binary tree`*\n* [236. Lowest Common Ancestor of a Binary Tree](solutions/0236_Lowest_Common_Ancestor_of_a_Binary_Tree/lca.go)&nbsp;&nbsp;&nbsp;*`binary tree`*\n* [257. Binary Tree Paths](solutions/0257_binary_tree_paths/binary_tree_paths.go)&nbsp;&nbsp;&nbsp;*`binary tree`*\n* [307. Range Sum Query - Mutable](solutions/0307_Range_Sum_Query_Mutable/range_sum_query_mut.go)&nbsp;&nbsp;&nbsp;*`segment tree`*\n* [404. Sum of Left Leaves](solutions/0404_sum_of_left_leaves/sum_of_left_leaves.go)&nbsp;&nbsp;&nbsp;*`binary tree`*\n* [437. Path Sum III](solutions/0437_path_sum_3/path_sum_3.go)&nbsp;&nbsp;&nbsp;*`binary tree`*\n* [677. Map Sum Pairs](solutions/0677_map_sum_pairs/map_sum_pairs.go)&nbsp;&nbsp;&nbsp;*`trie`*\n* [872. Leaf-Similar Trees](solutions/0872_leaf_similar_trees/leaf_similar_trees.go)&nbsp;&nbsp;&nbsp;*`binary tree`*\n\n### Binary Search\n* [33. Search in Rotated Sorted Array](solutions/0033_search_in_rotated_sorted_array/search_in_rotated_sorted_array.go)&nbsp;&nbsp;&nbsp;*`array;`*&nbsp;&nbsp;*`binary search`*\n* [34. Find First and Last Position of Element in Sorted Array](solutions/0034_find_first_and_last_position_of_element_in_sorted_array/find_first_and_last_position_of_element_in_sorted_array.go)&nbsp;&nbsp;&nbsp;*`array;`*&nbsp;&nbsp;*`binary search`*\n* [35. Search Insert Position](solutions/0035_search_insert_position/search_insert_position.go)&nbsp;&nbsp;&nbsp;*`array;`*&nbsp;&nbsp;*`binary search`*\n* [69. Sqrt(x)](solutions/0069_sqrtx/sqrtx.go)&nbsp;&nbsp;&nbsp;*`math;`*&nbsp;&nbsp;*`binary search`*\n* [153. Find Minimum in Rotated Sorted Array](solutions/0153_find_minimum_in_rotated_sorted_array/fmirsa.go)\n* [704. Binary Search](solutions/0704_binary_search/binary_search.go)\n\n### Math\n* [7. Reverse Integer](solutions/0007_reverse_integer/reverse_integer.go)\n* [9. Palindrome Number](solutions/0009_palindrome_number/palindrome_number.go)\n* [13. Roman to Integer](solutions/0013_roman_to_integer/roman_to_integer.go)&nbsp;&nbsp;&nbsp;*`string`*\n* [66. Plus One](solutions/0066_plus_one/plus_one.go)&nbsp;&nbsp;&nbsp;*`array`*\n* [150. Evaluate Reverse Polish Notation](solutions/0150_evaluate_reverse_polish_notation/evaluate_reverse_polish_notation.go)&nbsp;&nbsp;&nbsp;*`stack`*\n* [258. Add Digits](solutions/0258_add_digits/add_digits.go)\n"
  },
  {
    "path": "docs/_config.yml",
    "content": "theme: jekyll-theme-hacker"
  },
  {
    "path": "docs/index.md",
    "content": "## Welcome to GitHub Pages\n\nYou can use the [editor on GitHub](https://github.com/keep-in-practice/leetcode-go/edit/master/docs/index.md) to maintain and preview the content for your website in Markdown files.\n\nWhenever you commit to this repository, GitHub Pages will run [Jekyll](https://jekyllrb.com/) to rebuild the pages in your site, from the content in your Markdown files.\n\n### Markdown\n\nMarkdown is a lightweight and easy-to-use syntax for styling your writing. It includes conventions for\n\n```markdown\nSyntax highlighted code block\n\n# Header 1\n## Header 2\n### Header 3\n\n- Bulleted\n- List\n\n1. Numbered\n2. List\n\n**Bold** and _Italic_ and `Code` text\n\n[Link](url) and ![Image](src)\n```\n\nFor more details see [GitHub Flavored Markdown](https://guides.github.com/features/mastering-markdown/).\n\n### Jekyll Themes\n\nYour Pages site will use the layout and styles from the Jekyll theme you have selected in your [repository settings](https://github.com/keep-in-practice/leetcode-go/settings). The name of this theme is saved in the Jekyll `_config.yml` configuration file.\n\n### Support or Contact\n\nHaving trouble with Pages? Check out our [documentation](https://docs.github.com/categories/github-pages-basics/) or [contact support](https://github.com/contact) and we’ll help you sort it out.\n"
  },
  {
    "path": "go.mod",
    "content": "module leetcode\n\ngo 1.13\n"
  },
  {
    "path": "solutions/0001_two_sum/twosum.go",
    "content": "/*\n1. Two Sum\n\nSource: https://leetcode.com/problems/two-sum/\n\nGiven an array of integers, return indices of the two numbers such that they add up to a specific target.\n\nYou may assume that each input would have exactly one solution, and you may not use the same element twice.\n\nExample:\n\nGiven nums = [2, 7, 11, 15], target = 9,\n\nBecause nums[0] + nums[1] = 2 + 7 = 9,\nreturn [0, 1].\n\n*/\n\npackage twosum\n\n// Time complexity: O(n)\n// Space complexity: O(n)\nfunc twoSum(nums []int, target int) []int {\n\trecord := make(map[int]int)\n\n\tfor i, j := range nums {\n\t\tcomplement := target - j\n\t\tif res, ok := record[complement]; ok {\n\t\t\treturn []int{res, i}\n\t\t}\n\t\trecord[j] = i\n\t}\n\treturn []int{}\n}\n\n// brute force\n// Time complexity: O(n^2)\n// Space complexity: O(1)\nfunc twoSum1(nums []int, target int) []int {\n\tfor i, j := range nums {\n\t\tfor k := i + 1; k < len(nums); k++ {\n\t\t\tif nums[k]+j == target {\n\t\t\t\treturn []int{i, k}\n\t\t\t}\n\t\t}\n\t}\n\treturn []int{}\n}\n"
  },
  {
    "path": "solutions/0001_two_sum/twosum_test.go",
    "content": "package twosum\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestTwoSum(t *testing.T) {\n\tnums := []int{2, 7, 11, 15}\n\n\tfuncs := []func([]int, int) []int{twoSum, twoSum1}\n\n\tfor _, testFunc := range funcs {\n\t\tif res := testFunc(nums, 9); !reflect.DeepEqual(res, []int{0, 1}) {\n\t\t\tt.Error(\"Failed, two sum\")\n\t\t}\n\n\t\tif res := testFunc(nums, 6); !reflect.DeepEqual(res, []int{}) {\n\t\t\tt.Error(\"Failed, two sum\")\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0002_add_two_numbers/add_two_numbers.go",
    "content": "/*\n2. Add Two Numbers\n\nSource: https://leetcode.com/problems/add-two-numbers/\n\nYou are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.\n\nYou may assume the two numbers do not contain any leading zero, except the number 0 itself.\n\nExample:\n\nInput: (2 -> 4 -> 3) + (5 -> 6 -> 4)\nOutput: 7 -> 0 -> 8\nExplanation: 342 + 465 = 807.\n*/\n\npackage addtwonumbers\n\n// ListNode is node of linked list\ntype ListNode struct {\n\tVal  int\n\tNext *ListNode\n}\n\n// 解法一，暴力解法\nfunc addTwoNumbers1(l1 *ListNode, l2 *ListNode) *ListNode {\n\thead := ListNode{Val: -1}\n\tcur := &head\n\tcarry := 0\n\tfor l1 != nil && l2 != nil {\n\t\tsum := l1.Val + l2.Val + carry\n\t\tval := sum % 10\n\t\tcarry = sum / 10\n\t\tnode := ListNode{Val: val}\n\t\tcur.Next = &node\n\t\tcur = cur.Next\n\t\tl1 = l1.Next\n\t\tl2 = l2.Next\n\t}\n\tif carry == 0 {\n\t\tif l1 != nil {\n\t\t\tcur.Next = l1\n\t\t}\n\t\tif l2 != nil {\n\t\t\tcur.Next = l2\n\t\t}\n\t\treturn head.Next\n\t}\n\tfor l1 != nil {\n\t\tsum := l1.Val + carry\n\t\tval := sum % 10\n\t\tcarry = sum / 10\n\t\tnode := ListNode{Val: val}\n\t\tcur.Next = &node\n\t\tcur = cur.Next\n\t\tl1 = l1.Next\n\t}\n\tfor l2 != nil {\n\t\tsum := l2.Val + carry\n\t\tval := sum % 10\n\t\tcarry = sum / 10\n\t\tnode := ListNode{Val: val}\n\t\tcur.Next = &node\n\t\tcur = cur.Next\n\t\tl2 = l2.Next\n\t}\n\tfor carry != 0 {\n\t\tval := carry % 10\n\t\tcarry = carry / 10\n\t\tnode := ListNode{Val: val}\n\t\tcur.Next = &node\n\t\tcur = cur.Next\n\t}\n\treturn head.Next\n}\n\n// 解法二，递归\nfunc addTwoNumbers2(l1 *ListNode, l2 *ListNode) *ListNode {\n\tif l1 == nil && l2 == nil {\n\t\treturn nil\n\t} else if l1 == nil && l2 != nil {\n\t\treturn l2\n\t} else if l1 != nil && l2 == nil {\n\t\treturn l1\n\t}\n\n\tsum := l1.Val + l2.Val\n\tnext := addTwoNumbers2(l1.Next, l2.Next)\n\n\tif sum >= 10 {\n\t\tcarry := sum / 10\n\t\tsum %= 10\n\t\tnext = addTwoNumbers2(next, &ListNode{Val: carry})\n\t}\n\treturn &ListNode{Val: sum, Next: next}\n}\n"
  },
  {
    "path": "solutions/0002_add_two_numbers/add_two_numbers_test.go",
    "content": "package addtwonumbers\n\nimport (\n\t\"os\"\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc createSingleLinkedList(arr []int) *ListNode {\n\thead := &ListNode{}\n\tcur := head\n\n\tfor _, j := range arr {\n\t\tcur.Next = &ListNode{Val: j}\n\t\tcur = cur.Next\n\t}\n\treturn head.Next\n}\n\nfunc TestAddTwoNumbers1(t *testing.T) {\n\ttestDatas := []struct {\n\t\tname     string\n\t\targ1     *ListNode\n\t\targ2     *ListNode\n\t\texpected *ListNode\n\t}{\n\t\t{\n\t\t\tname:     \"one\",\n\t\t\targ1:     createSingleLinkedList([]int{2, 4, 3}),\n\t\t\targ2:     createSingleLinkedList([]int{5, 6, 4}),\n\t\t\texpected: createSingleLinkedList([]int{7, 0, 8}),\n\t\t},\n\t\t{\n\t\t\tname:     \"two\",\n\t\t\targ1:     createSingleLinkedList([]int{5}),\n\t\t\targ2:     createSingleLinkedList([]int{5}),\n\t\t\texpected: createSingleLinkedList([]int{0, 1}),\n\t\t},\n\t\t{\n\t\t\tname:     \"three\",\n\t\t\targ1:     createSingleLinkedList([]int{1}),\n\t\t\targ2:     createSingleLinkedList([]int{9, 9, 9}),\n\t\t\texpected: createSingleLinkedList([]int{0, 0, 0, 1}),\n\t\t},\n\t\t{\n\t\t\tname:     \"four\",\n\t\t\targ1:     createSingleLinkedList([]int{9, 9, 9}),\n\t\t\targ2:     createSingleLinkedList([]int{1}),\n\t\t\texpected: createSingleLinkedList([]int{0, 0, 0, 1}),\n\t\t},\n\t\t{\n\t\t\tname:     \"five\",\n\t\t\targ1:     createSingleLinkedList([]int{4, 3, 1}),\n\t\t\targ2:     createSingleLinkedList([]int{1}),\n\t\t\texpected: createSingleLinkedList([]int{5, 3, 1}),\n\t\t},\n\t\t{\n\t\t\tname:     \"six\",\n\t\t\targ1:     createSingleLinkedList([]int{1}),\n\t\t\targ2:     createSingleLinkedList([]int{4, 3, 1}),\n\t\t\texpected: createSingleLinkedList([]int{5, 3, 1}),\n\t\t},\n\t}\n\n\tfor _, testData := range testDatas {\n\t\tt.Run(testData.name, func(t *testing.T) {\n\t\t\tif result := addTwoNumbers1(testData.arg1, testData.arg2); !reflect.DeepEqual(result, testData.expected) {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", testData.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestAddTwoNumbers2(t *testing.T) {\n\ttestDatas := []struct {\n\t\tname     string\n\t\targ1     *ListNode\n\t\targ2     *ListNode\n\t\texpected *ListNode\n\t}{\n\t\t{\n\t\t\tname:     \"one\",\n\t\t\targ1:     createSingleLinkedList([]int{2, 4, 3}),\n\t\t\targ2:     createSingleLinkedList([]int{5, 6, 4}),\n\t\t\texpected: createSingleLinkedList([]int{7, 0, 8}),\n\t\t},\n\t\t{\n\t\t\tname:     \"two\",\n\t\t\targ1:     createSingleLinkedList([]int{5}),\n\t\t\targ2:     createSingleLinkedList([]int{5}),\n\t\t\texpected: createSingleLinkedList([]int{0, 1}),\n\t\t},\n\t\t{\n\t\t\tname:     \"three\",\n\t\t\targ1:     createSingleLinkedList([]int{1}),\n\t\t\targ2:     createSingleLinkedList([]int{9, 9, 9}),\n\t\t\texpected: createSingleLinkedList([]int{0, 0, 0, 1}),\n\t\t},\n\t\t{\n\t\t\tname:     \"four\",\n\t\t\targ1:     createSingleLinkedList([]int{9, 9, 9}),\n\t\t\targ2:     createSingleLinkedList([]int{1}),\n\t\t\texpected: createSingleLinkedList([]int{0, 0, 0, 1}),\n\t\t},\n\t\t{\n\t\t\tname:     \"five\",\n\t\t\targ1:     createSingleLinkedList([]int{4, 3, 1}),\n\t\t\targ2:     createSingleLinkedList([]int{1}),\n\t\t\texpected: createSingleLinkedList([]int{5, 3, 1}),\n\t\t},\n\t\t{\n\t\t\tname:     \"six\",\n\t\t\targ1:     createSingleLinkedList([]int{1}),\n\t\t\targ2:     createSingleLinkedList([]int{4, 3, 1}),\n\t\t\texpected: createSingleLinkedList([]int{5, 3, 1}),\n\t\t},\n\t}\n\n\tfor _, testData := range testDatas {\n\t\tt.Run(testData.name, func(t *testing.T) {\n\t\t\tif result := addTwoNumbers2(testData.arg1, testData.arg2); !reflect.DeepEqual(result, testData.expected) {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", testData.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n\nfunc TestMain(m *testing.M) {\n\tos.Exit(m.Run())\n}\n"
  },
  {
    "path": "solutions/0003_longest_substring_without_repeating_characters/longest_substring_without_repeating_characters.go",
    "content": "/*\n3. Longest Substring Without Repeating Characters\n\nSource: https://leetcode.com/problems/longest-substring-without-repeating-characters/\n\nGiven a string, find the length of the longest substring without repeating characters.\n\nExample 1:\n\nInput: \"abcabcbb\"\nOutput: 3\nExplanation: The answer is \"abc\", with the length of 3.\nExample 2:\n\nInput: \"bbbbb\"\nOutput: 1\nExplanation: The answer is \"b\", with the length of 1.\nExample 3:\n\nInput: \"pwwkew\"\nOutput: 3\nExplanation: The answer is \"wke\", with the length of 3.\n             Note that the answer must be a substring, \"pwke\" is a subsequence and not a substring.\n*/\n\npackage longestsubstringwithoutrepeatingcharacters\n\n// Time complexity: O(n)\n// Space complexity:  O(n)\nfunc lengthOfLongestSubstring(s string) int {\n\n\tvar (\n\t\tstart              = 0\n\t\tres                = 0\n\t\tlastOccurredRecord = make(map[rune]int)\n\t)\n\n\tfor i, ch := range []rune(s) {\n\t\tif lastIndex, ok := lastOccurredRecord[ch]; ok && lastIndex >= start {\n\t\t\tstart = lastIndex + 1\n\t\t}\n\t\tif i-start+1 > res {\n\t\t\tres = i - start + 1\n\t\t}\n\t\tlastOccurredRecord[ch] = i\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0003_longest_substring_without_repeating_characters/longest_substring_without_repeating_characters_test.go",
    "content": "package longestsubstringwithoutrepeatingcharacters\n\nimport \"testing\"\n\nfunc TestLengthOfLongestSubstring(t *testing.T) {\n\ttestData := []string{\n\t\t\"abcabcbb\",\n\t\t\"bbbbb\",\n\t\t\"pwwkew\",\n\t}\n\texpectedData := []int{3, 1, 3}\n\n\tfor index, data := range testData {\n\t\tif res := lengthOfLongestSubstring(data); res != expectedData[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expectedData[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0004_median_of_two_sorted_arrays/motsa.go",
    "content": "/*\n4. Median of Two Sorted Arrays\nhttps://leetcode.com/problems/median-of-two-sorted-arrays/\n\nThere are two sorted arrays nums1 and nums2 of size m and n respectively.\n\nFind the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).\n\nYou may assume nums1 and nums2 cannot be both empty.\n*/\n// time: 2018-12-29\n\npackage motsa\n\nimport \"leetcode/utils\"\n\n// binary search\n// time complexity: O(log(m+n)), where m is nums1's length, n is nums2's length.\n// space complexity: O(log(m+n))\nfunc findMedianSortedArrays(nums1 []int, nums2 []int) float64 {\n\tvar (\n\t\tm = len(nums1)\n\t\tn = len(nums2)\n\t)\n\n\tif 0 == m && 0 == n {\n\t\tpanic(\"nums1 and nums2 cannot be both empty.\")\n\t}\n\n\tif 0 == m {\n\t\treturn float64(nums2[n>>1]+nums2[n>>1-(n+1)%2]) / 2\n\t}\n\n\tif 0 == n {\n\t\treturn float64(nums1[m>>1]+nums1[m>>1-(m+1)%2]) / 2\n\t}\n\n\tvar (\n\t\tleft  = (m + n + 1) >> 1\n\t\tright = (m + n + 2) >> 1\n\t)\n\treturn float64(findKth(nums1, 0, nums2, 0, left)+findKth(nums1, 0, nums2, 0, right)) / 2\n}\n\n/*\n使用二分法, 在两个有序数组中找到第k个元素。\n使用两个变量i和j分别来标记数组nums1和nums2的起始位置。\n1. \t当某一个数组的起始位置大于等于其数组长度时，说明其所有数字均已经被淘汰了，\n\t相当于一个空数组了，那么实际上就变成了在另一个数组中找数字，直接就可以找出来了\n2. \t如果K=1的话，那么我们只要比较nums1和nums2的起始位置i和j上的数字就可以了。\n3.\t对K二分，分别在nums1和nums2中查找第K/2个元素，注意这里由于两个数组的长度不定，所以有可能某个数组没有第K/2个数字，\n\t需要先检查一下，数组中到底存不存在第k/2个数字，如果存在就取出来，反之赋值为整型最大值，\n\t比较两个数组的第k/2个数字，如果第一个数组的第k/2个数字小的话，说明我们要找的数字肯定不再第一个数组的前k/2个数字中，\n\t所以舍弃掉，将第一个数组的起始位置移动k/2，并且此时k也减去k/2（舍弃了k/2个之后，只需要在剩下的里面寻找k-k/2就可以了）\n*/\nfunc findKth(nums1 []int, i int, nums2 []int, j int, k int) int {\n\tif i >= len(nums1) {\n\t\treturn nums2[j+k-1]\n\t}\n\tif j >= len(nums2) {\n\t\treturn nums1[i+k-1]\n\t}\n\n\tif 1 == k {\n\t\tif nums1[i] < nums2[j] {\n\t\t\treturn nums1[i]\n\t\t}\n\t\treturn nums2[j]\n\t}\n\tvar (\n\t\tmidVal1, midVal2 int\n\t)\n\n\tif i+k/2-1 < len(nums1) {\n\t\tmidVal1 = nums1[i+k/2-1]\n\t} else {\n\t\tmidVal1 = utils.MaxInt\n\t}\n\n\tif j+k/2-1 < len(nums2) {\n\t\tmidVal2 = nums2[j+k/2-1]\n\t} else {\n\t\tmidVal2 = utils.MaxInt\n\t}\n\n\tif midVal1 < midVal2 {\n\t\treturn findKth(nums1, i+k/2, nums2, j, k-k/2)\n\t}\n\treturn findKth(nums1, i, nums2, j+k/2, k-k/2)\n}\n"
  },
  {
    "path": "solutions/0004_median_of_two_sorted_arrays/motsa_test.go",
    "content": "package motsa\n\nimport (\n\t\"testing\"\n)\n\nfunc TestFindMedianSortedArrays(t *testing.T) {\n\ttype arg struct {\n\t\tnums1 []int\n\t\tnums2 []int\n\t}\n\n\ttestCases := []arg{\n\t\t{nums1: []int{1, 3}, nums2: []int{2}},\n\t\t{nums1: []int{1, 2}, nums2: []int{3, 4}},\n\t\t{nums1: []int{}, nums2: []int{3, 4, 5}},\n\t\t{nums1: []int{4, 5, 6}, nums2: []int{}},\n\t\t{nums1: []int{2, 3, 4, 5}, nums2: []int{1}},\n\t\t{nums1: []int{1}, nums2: []int{2, 3, 4, 5, 6, 7}},\n\t\t{nums1: []int{1, 2, 3, 4, 5, 6, 8, 9, 23, 34}, nums2: []int{7}},\n\t}\n\n\texpected := []float64{2, 2.5, 4, 5, 3, 4, 6}\n\n\tfor index, data := range testCases {\n\t\tif res := findMedianSortedArrays(data.nums1, data.nums2); res != expected[index] {\n\t\t\tt.Errorf(\"expected %f, got %f\", expected[index], res)\n\t\t}\n\t}\n\n\tdefer func() {\n\t\tif err := recover(); err == nil || err != \"nums1 and nums2 cannot be both empty.\" {\n\t\t\tt.Errorf(\"expected err: nums1 and nums2 cannot be both empty.\")\n\t\t}\n\t}()\n\tfindMedianSortedArrays([]int{}, []int{})\n}\n"
  },
  {
    "path": "solutions/0007_reverse_integer/reverse_integer.go",
    "content": "/*\n7. Reverse Integer\nhttps://leetcode.com/problems/reverse-integer/\nGiven a 32-bit signed integer, reverse digits of an integer.\n\n*/\n// time: 2018-12-29\n\npackage reverseinteger\n\n// time complexity: O(log 10 x ) = O(log x)\n// space complexity: O(1)\nfunc reverse(x int) int {\n\tif 0 == x {\n\t\treturn x\n\t}\n\n\tisPositive := true\n\tif x < 0 {\n\t\tisPositive = false\n\t\tx *= -1\n\t}\n\n\tres := 0\n\n\tfor x > 0 {\n\t\tres = res*10 + x%10\n\t\tx /= 10\n\t}\n\n\tif !isPositive {\n\t\tres *= -1\n\t}\n\n\tif res < -1<<31 || res > (1<<31)-1 {\n\t\treturn 0\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0007_reverse_integer/reverse_integer_test.go",
    "content": "package reverseinteger\n\nimport \"testing\"\n\nfunc TestReverse(t *testing.T) {\n\ttestCases := []int{\n\t\t123, -123, 10, 0, 12239999999999,\n\t}\n\texpected := []int{321, -321, 1, 0, 0}\n\n\tfor index, data := range testCases {\n\t\tif res := reverse(data); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0009_palindrome_number/palindrome_number.go",
    "content": "/*\n9. Palindrome Number\nhttps://leetcode.com/problems/palindrome-number/\n\nDetermine whether an integer is a palindrome.\nAn integer is a palindrome when it reads the same backward as forward.\n*/\n// time: 2018-12-30\n\npackage palindromenumber\n\n// Time complexity: O(log 10 (n))\n// Space complexity : O(1)\nfunc isPalindrome(x int) bool {\n\tvar (\n\t\ty int\n\t\tz = x\n\t)\n\n\tfor x > 0 {\n\t\ty = y*10 + x%10\n\t\tx /= 10\n\t}\n\treturn y == z\n}\n"
  },
  {
    "path": "solutions/0009_palindrome_number/palindrome_number_test.go",
    "content": "package palindromenumber\n\nimport \"testing\"\n\nfunc TestIsPalindrome(t *testing.T) {\n\ttestCases := []int{121, -121, 10, 0}\n\texpected := []bool{true, false, false, true}\n\tfor index, data := range testCases {\n\t\tif res := isPalindrome(data); res != expected[index] {\n\t\t\tt.Errorf(\"expected %t, got %t\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0011_container_with_most_water/container_with_most_water.go",
    "content": "/*\n11. Container With Most Water\nhttps://leetcode.com/problems/container-with-most-water/\n\nGiven n non-negative integers a1, a2, ..., an ,\nwhere each represents a point at coordinate (i, ai).\nn vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0).\nFind two lines, which together with x-axis forms a container,\nsuch that the container contains the most water.\n\nNote: You may not slant the container and n is at least 2.\n*/\n// time: 2018-12-26\n\npackage containerwithmostwater\n\nimport \"leetcode/utils\"\n\n// time complexity: O(n)\n// space complexity: O(1)\nfunc maxArea(height []int) int {\n\tvar (\n\t\twater int\n\t\tl     int\n\t\tr     = len(height) - 1\n\t)\n\n\tfor l < r {\n\t\th := utils.CalcMinInt(height[l], height[r])\n\t\twater = utils.CalcMaxInt(water, (r-l)*h)\n\t\tfor height[l] <= h && l < r {\n\t\t\tl++\n\t\t}\n\t\tfor height[r] <= h && l < r {\n\t\t\tr--\n\t\t}\n\t}\n\treturn water\n}\n\n// time complexity: O(n^2)\n// space complexity: O(n)\nfunc maxArea1(height []int) int {\n\tn := len(height)\n\tif n >= 0 && n < 2 {\n\t\treturn 0\n\t}\n\n\tmemo := make([]int, n)\n\tmemo[1] = 1 * utils.CalcMinInt(height[0], height[1])\n\tfor i := 2; i < n; i++ {\n\t\tfor j := i - 1; j >= 0; j-- {\n\t\t\tmemo[i] = utils.CalcMaxInt(memo[i], (i-j)*utils.CalcMinInt(height[i], height[j]))\n\t\t}\n\t}\n\treturn utils.CalcMaxInt(memo...)\n}\n\n// time complexity: O(n^2)\n// space complexity: O(1)\nfunc maxArea2(height []int) int {\n\tres := 0\n\n\tfor i := 0; i < len(height); i++ {\n\t\tfor j := i + 1; j < len(height); j++ {\n\t\t\ttmp := (j - i) * utils.CalcMinInt(height[i], height[j])\n\t\t\tif tmp > res {\n\t\t\t\tres = tmp\n\t\t\t}\n\t\t}\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0011_container_with_most_water/container_with_most_water_test.go",
    "content": "package containerwithmostwater\n\nimport \"testing\"\n\nfunc TestMaxArea(t *testing.T) {\n\ttestCases := [][]int{\n\t\t{1, 8, 6, 2, 5, 4, 8, 3, 7},\n\t\t{},\n\t\t{1},\n\t\t{1, 2},\n\t\t{2, 3, 5, 7, 8, 9, 5, 3, 2},\n\t}\n\texpected := []int{49, 0, 0, 1, 20}\n\tfor index, data := range testCases {\n\t\tif res := maxArea(data); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n\nfunc TestMaxArea1(t *testing.T) {\n\ttestCases := [][]int{\n\t\t{1, 8, 6, 2, 5, 4, 8, 3, 7},\n\t\t{},\n\t\t{1},\n\t\t{1, 2},\n\t\t{2, 3, 5, 7, 8, 9, 5, 3, 2},\n\t}\n\texpected := []int{49, 0, 0, 1, 20}\n\tfor index, data := range testCases {\n\t\tif res := maxArea1(data); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n\nfunc TestMaxArea2(t *testing.T) {\n\ttestCases := [][]int{\n\t\t{1, 8, 6, 2, 5, 4, 8, 3, 7},\n\t\t{},\n\t\t{1},\n\t\t{1, 2},\n\t\t{2, 3, 5, 7, 8, 9, 5, 3, 2},\n\t}\n\texpected := []int{49, 0, 0, 1, 20}\n\tfor index, data := range testCases {\n\t\tif res := maxArea2(data); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0013_roman_to_integer/roman_to_integer.go",
    "content": "/*\n13. Roman to Integer\nhttps://leetcode.com/problems/roman-to-integer/\n*/\n// time: 2018-12-31\n\npackage rti\n\n// time complexity: O(n)\n// space complexity: O(1)\nfunc romanToInt(s string) int {\n\tif 0 == len(s) {\n\t\treturn 0\n\t}\n\n\tdigits := map[byte]int{'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}\n\tsum := digits[s[len(s)-1]]\n\n\tfor i := len(s) - 1; i > 0; i-- {\n\t\tcur := digits[s[i]]\n\t\tpre := digits[s[i-1]]\n\t\tif cur > pre {\n\t\t\tsum -= pre\n\t\t} else {\n\t\t\tsum += pre\n\t\t}\n\t}\n\treturn sum\n}\n"
  },
  {
    "path": "solutions/0013_roman_to_integer/roman_to_integer_test.go",
    "content": "package rti\n\nimport \"testing\"\n\nfunc TestRomanToInt(t *testing.T) {\n\ttestCases := []string{\n\t\t\"III\",\n\t\t\"IV\",\n\t\t\"IX\",\n\t\t\"LVIII\",\n\t\t\"MCMXCIV\",\n\t\t\"\",\n\t}\n\texpected := []int{3, 4, 9, 58, 1994, 0}\n\n\tfor index, s := range testCases {\n\t\tif res := romanToInt(s); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0014_longest_common_prefix/lcp.go",
    "content": "/*\n14. Longest Common Prefix\nhttps://leetcode.com/problems/longest-common-prefix/\n\nWrite a function to find the longest common prefix string amongst an array of strings.\n\nIf there is no common prefix, return an empty string \"\".\n*/\n// time: 2018-12-31\n\npackage lcp\n\n// time complexity: O(len(strs) * max len of string)\n// space complexity: O(1)\nfunc longestCommonPrefix(strs []string) string {\n\tif 0 == len(strs) {\n\t\treturn \"\"\n\t}\n\tres := \"\"\n\tfor i := 0; i < len(strs[0]); i++ {\n\t\tc := strs[0][i]\n\t\tfor j := 1; j < len(strs); j++ {\n\t\t\tif i >= len(strs[j]) || strs[j][i] != c {\n\t\t\t\treturn res\n\t\t\t}\n\t\t}\n\t\tres += string(c)\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0014_longest_common_prefix/lcp_test.go",
    "content": "package lcp\n\nimport \"testing\"\n\nfunc TestLongestCommonPrefix(t *testing.T) {\n\ttestCases := [][]string{\n\t\t{\"flower\", \"flow\", \"flight\"},\n\t\t{\"dog\", \"racecar\", \"car\"},\n\t\t{},\n\t\t{\"\"},\n\t}\n\n\texpected := []string{\n\t\t\"fl\",\n\t\t\"\",\n\t\t\"\",\n\t\t\"\",\n\t}\n\n\tfor index, strs := range testCases {\n\t\tif res := longestCommonPrefix(strs); res != expected[index] {\n\t\t\tt.Errorf(\"expected %s, got %s\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0015_3Sum/3sum.go",
    "content": "/*\n15. 3Sum\nhttps://leetcode.com/problems/3sum/\n*/\n// time: 2019-03-01\n\npackage threesum\n\nimport \"sort\"\n\n// time complexity: O(n^2)\nfunc threeSum(nums []int) [][]int {\n\tsort.Ints(nums)\n\tvar res [][]int\n\tfor i := 0; i < len(nums)-2; i++ {\n\t\tif nums[i] > 0 {\n\t\t\tbreak\n\t\t}\n\t\tif i > 0 && nums[i] == nums[i-1] {\n\t\t\tcontinue\n\t\t}\n\t\tfor l, r := i+1, len(nums)-1; l < r; {\n\t\t\tif l > i+1 && nums[l] == nums[l-1] {\n\t\t\t\tl++\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tif r < len(nums)-1 && nums[r] == nums[r+1] {\n\t\t\t\tr--\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tswitch sum := nums[i] + nums[l] + nums[r]; {\n\t\t\tcase sum < 0:\n\t\t\t\tl++\n\t\t\tcase sum > 0:\n\t\t\t\tr--\n\t\t\tdefault:\n\t\t\t\tres = append(res, []int{nums[i], nums[l], nums[r]})\n\t\t\t\tl++\n\t\t\t\tr--\n\t\t\t}\n\t\t}\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0015_3Sum/3sum_test.go",
    "content": "package threesum\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestThreeSum(t *testing.T) {\n\ttestData := [][]int{\n\t\t{-1, 0, 1, 2, -6, -4},\n\t\t{-7, 3, -7, 3, 4, 5, 6, 6, 4},\n\t}\n\texpected := [][][]int{\n\t\t{{-1, 0, 1}},\n\t\t{{-7, 3, 4}},\n\t}\n\n\tfor index, nums := range testData {\n\t\tif res := threeSum(nums); !reflect.DeepEqual(res, expected[index]) {\n\t\t\tt.Errorf(\"expected %v, got %v\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0017_letter_combination_of_a_phone_number/letter_combination_of_phone_number.go",
    "content": "/*\n17. Letter Combinations of a Phone Number\n\nSource: https://leetcode.com/problems/letter-combinations-of-a-phone-number/\n\nGiven a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.\n\nA mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.\n\nExample:\n\nInput: \"23\"\nOutput: [\"ad\", \"ae\", \"af\", \"bd\", \"be\", \"bf\", \"cd\", \"ce\", \"cf\"].\nNote:\n\nAlthough the above answer is in lexicographical order, your answer could be in any order you want.\n*/\n\npackage lettercombinationsofaphonenumber\n\nvar (\n\tletterMap = []string{\n\t\t\" \",\n\t\t\"\",\n\t\t\"abc\",\n\t\t\"def\",\n\t\t\"ghi\",\n\t\t\"jkl\",\n\t\t\"mno\",\n\t\t\"pqrs\",\n\t\t\"tuv\",\n\t\t\"wxyz\",\n\t}\n\tres []string\n)\n\n// Time complexity: O(2^n)\n// Space complexity: O(n)\nfunc letterCombinations(digits string) []string {\n\tres = []string{}\n\tif digits == \"\" {\n\t\treturn res\n\t}\n\n\tfindCombinations(digits, 0, \"\")\n\treturn res\n}\n\nfunc findCombinations(digits string, index int, s string) {\n\tif index == len(digits) {\n\t\tres = append(res, s)\n\t\treturn\n\t}\n\n\tch := digits[index]\n\tletters := letterMap[ch-'0']\n\tfor _, i := range letters {\n\t\tfindCombinations(digits, index+1, s+string(i))\n\t}\n\treturn\n}\n"
  },
  {
    "path": "solutions/0017_letter_combination_of_a_phone_number/letter_combination_of_phone_number_test.go",
    "content": "package lettercombinationsofaphonenumber\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestLetterCombinations(t *testing.T) {\n\ttestData := []string{\n\t\t\"23\",\n\t\t\"\",\n\t}\n\n\texpectedData := [][]string{\n\t\t{\"ad\", \"ae\", \"af\", \"bd\", \"be\", \"bf\", \"cd\", \"ce\", \"cf\"},\n\t\t{},\n\t}\n\n\tfor index, digits := range testData {\n\t\tif res := letterCombinations(digits); !reflect.DeepEqual(res, expectedData[index]) {\n\t\t\tt.Errorf(\"expected %v, got %v\", expectedData[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0019_remove_nth_node_from_end_of_list/remove_nth_node_from_end_of_list.go",
    "content": "/*\n19. Remove Nth Node From End of List\nhttps://leetcode.com/problems/remove-nth-node-from-end-of-list/\n\nGiven a linked list, remove the n-th node from the end of list and return its head.\n*/\n// time: 2018-12-31\n\npackage rnthnfeol\n\n// ListNode Definition for singly-linked list.\ntype ListNode struct {\n\tVal  int\n\tNext *ListNode\n}\n\n// time complexity: O(n)\n// space complexity: O(1)\nfunc removeNthFromEnd(head *ListNode, n int) *ListNode {\n\tif n <= 0 {\n\t\treturn head\n\t}\n\tdummy := &ListNode{}\n\tdummy.Next = head\n\n\tvar (\n\t\tp = dummy\n\t\tq = dummy\n\t)\n\n\tfor i := 0; i < n; i++ {\n\t\tq = q.Next\n\t}\n\n\tfor q.Next != nil {\n\t\tp = p.Next\n\t\tq = q.Next\n\t}\n\n\tp.Next = p.Next.Next\n\treturn dummy.Next\n}\n"
  },
  {
    "path": "solutions/0019_remove_nth_node_from_end_of_list/remove_nth_node_from_end_of_list_test.go",
    "content": "package rnthnfeol\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestRemoveNthFromEnd(t *testing.T) {\n\ttype arg struct {\n\t\thead *ListNode\n\t\tn    int\n\t}\n\n\ttestCase := []arg{\n\t\t{head: createSingleLinkedList([]int{1, 2, 3, 4, 5}), n: 2},\n\t\t{head: createSingleLinkedList([]int{1, 2, 3, 4, 5}), n: 0},\n\t}\n\n\texpected := []*ListNode{\n\t\tcreateSingleLinkedList([]int{1, 2, 3, 5}),\n\t\tcreateSingleLinkedList([]int{1, 2, 3, 4, 5}),\n\t}\n\n\tfor index, data := range testCase {\n\t\tif res := removeNthFromEnd(data.head, data.n); !reflect.DeepEqual(res, expected[index]) {\n\t\t\tt.Errorf(\"expected\")\n\t\t}\n\t}\n}\n\nfunc createSingleLinkedList(arr []int) *ListNode {\n\thead := &ListNode{}\n\tcur := head\n\n\tfor _, j := range arr {\n\t\tcur.Next = &ListNode{Val: j}\n\t\tcur = cur.Next\n\t}\n\treturn head.Next\n}\n"
  },
  {
    "path": "solutions/0020_valid_parentheses/valid_parentheses.go",
    "content": "/*\n20. Valid Parentheses\n\nSource: https://leetcode.com/problems/valid-parentheses/\n\nGiven a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.\n\nAn input string is valid if:\n\n\t1. Open brackets must be closed by the same type of brackets.\n\t2. Open brackets must be closed in the correct order.\n\nNote that an empty string is also considered valid.\n\nExample 1:\n\nInput: \"()\"\nOutput: true\nExample 2:\n\nInput: \"()[]{}\"\nOutput: true\nExample 3:\n\nInput: \"(]\"\nOutput: false\nExample 4:\n\nInput: \"([)]\"\nOutput: false\nExample 5:\n\nInput: \"{[]}\"\nOutput: true\n*/\n\npackage validparentheses\n\n// Time complexity: O(n)\n// Space complexity: O(n)\nfunc isValid(s string) bool {\n\tvar (\n\t\tstacks  []rune\n\t\tmapping = map[rune]rune{')': '(', ']': '[', '}': '{'}\n\t)\n\tfor _, char := range s {\n\t\tif char == ')' || char == '}' || char == ']' {\n\t\t\tvar topElement rune\n\t\t\tif len(stacks) > 0 {\n\t\t\t\ttopElement = stacks[len(stacks)-1]\n\t\t\t\tstacks = stacks[:len(stacks)-1]\n\t\t\t} else {\n\t\t\t\ttopElement = '#'\n\t\t\t}\n\t\t\tif mapping[char] != topElement {\n\t\t\t\treturn false\n\t\t\t}\n\t\t} else {\n\t\t\tstacks = append(stacks, char)\n\t\t}\n\t}\n\treturn len(stacks) == 0\n}\n"
  },
  {
    "path": "solutions/0020_valid_parentheses/valid_parentheses_test.go",
    "content": "package validparentheses\n\nimport \"testing\"\n\nfunc TestIsValid(t *testing.T) {\n\ttestData := []string{\"()\", \"(((((())))))\", \"()()()()\", \"(((((((()\", \"((()(())))\", \"\", \")(\", \"}{\", \"][\", \"(][)\"}\n\texpectedData := []bool{true, true, true, false, true, true, false, false, false, false}\n\n\tfor i, s := range testData {\n\t\tresult := isValid(s)\n\t\tif result != expectedData[i] {\n\t\t\tt.Error(\"测试不通过\")\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0021_merge_two_sorted_lists/mergeTwoLists.go",
    "content": "/*\n21. Merge Two Sorted Lists\nSource: https://leetcode.com/problems/merge-two-sorted-lists/\n\nMerge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.\n\nExample:\n\nInput: 1->2->4, 1->3->4\nOutput: 1->1->2->3->4->4\n*/\n\npackage mergetwolists\n\n// ListNode is node of linked list\ntype ListNode struct {\n\tVal  int\n\tNext *ListNode\n}\n\n// Recursion\n// Time complexity: O(len(l1)+len(l2))\n// Space complexity: O(len(l1)+len(l2))\nfunc mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {\n\tif l1 == nil && l2 == nil {\n\t\treturn nil\n\t}\n\tif l1 == nil {\n\t\treturn l2\n\t}\n\n\tif l2 == nil {\n\t\treturn l1\n\t}\n\n\tif l1.Val < l2.Val {\n\t\tl1.Next = mergeTwoLists(l1.Next, l2)\n\t\treturn l1\n\t}\n\tl2.Next = mergeTwoLists(l1, l2.Next)\n\treturn l2\n}\n"
  },
  {
    "path": "solutions/0021_merge_two_sorted_lists/mergeTwoLists_test.go",
    "content": "package mergetwolists\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc createSingleLinkedList(arr []int) *ListNode {\n\thead := &ListNode{}\n\tcur := head\n\n\tfor _, j := range arr {\n\t\tcur.Next = &ListNode{Val: j}\n\t\tcur = cur.Next\n\t}\n\treturn head.Next\n}\n\nfunc TestMergeTwoLists(t *testing.T) {\n\ttestDatas := []struct {\n\t\tname     string\n\t\targ1     *ListNode\n\t\targ2     *ListNode\n\t\texpected *ListNode\n\t}{\n\t\t{\n\t\t\tname:     \"one\",\n\t\t\targ1:     createSingleLinkedList([]int{1, 3, 5}),\n\t\t\targ2:     createSingleLinkedList([]int{2, 4, 6}),\n\t\t\texpected: createSingleLinkedList([]int{1, 2, 3, 4, 5, 6}),\n\t\t},\n\t\t{\n\t\t\tname:     \"two\",\n\t\t\targ1:     createSingleLinkedList([]int{1}),\n\t\t\targ2:     createSingleLinkedList([]int{2, 4, 6}),\n\t\t\texpected: createSingleLinkedList([]int{1, 2, 4, 6}),\n\t\t},\n\t\t{\n\t\t\tname:     \"three\",\n\t\t\targ1:     nil,\n\t\t\targ2:     nil,\n\t\t\texpected: nil,\n\t\t},\n\t\t{\n\t\t\tname:     \"four\",\n\t\t\targ1:     createSingleLinkedList([]int{2, 4, 6}),\n\t\t\targ2:     createSingleLinkedList([]int{1}),\n\t\t\texpected: createSingleLinkedList([]int{1, 2, 4, 6}),\n\t\t},\n\t}\n\n\tfor _, testData := range testDatas {\n\t\tt.Run(testData.name, func(t *testing.T) {\n\t\t\tif result := mergeTwoLists(testData.arg1, testData.arg2); !reflect.DeepEqual(result, testData.expected) {\n\t\t\t\tt.Errorf(\"mergeTwoLists() = %v, expected %v\", result, testData.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n"
  },
  {
    "path": "solutions/0023_merge_k_sorted_lists/mksl.go",
    "content": "/*\n23. Merge k Sorted Lists\nhttps://leetcode.com/problems/merge-k-sorted-lists/\n\nMerge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.\n*/\n// time: 2019-01-02\n\npackage mksl\n\nimport (\n\t\"sort\"\n)\n\n// ListNode Definition for singly-linked list.\ntype ListNode struct {\n\tVal  int\n\tNext *ListNode\n}\n\n// heap\n// time complexity: O(n log n), where n is len of all lists, the main complexity is sort.\n// space complexity: O(n), where n is len of all lists.\nfunc mergeKLists(lists []*ListNode) *ListNode {\n\tfor i := 0; i < len(lists); {\n\t\tif lists[i] == nil {\n\t\t\tlists = append(lists[0:i], lists[i+1:]...)\n\t\t} else {\n\t\t\ti++\n\t\t}\n\t}\n\tif 0 == len(lists) {\n\t\treturn nil\n\t}\n\n\thead := &ListNode{}\n\tcur := head\n\tfor len(lists) > 0 {\n\t\tfor i := (len(lists) - 1) / 2; i >= 0; i-- {\n\t\t\tsiftUp(lists, len(lists), i)\n\t\t}\n\t\tcur.Next = &ListNode{Val: lists[0].Val}\n\t\tcur = cur.Next\n\t\tlists[0] = lists[0].Next\n\t\tif lists[0] == nil {\n\t\t\tlists = lists[1:]\n\t\t}\n\t}\n\treturn head.Next\n}\n\n// build heap\nfunc siftUp(lists []*ListNode, n int, k int) {\n\tfor 2*k+1 < n {\n\t\tj := 2*k + 1\n\t\tif j+1 < n && lists[j+1].Val < lists[j].Val {\n\t\t\tj = j + 1\n\t\t}\n\t\tif lists[k].Val < lists[j].Val {\n\t\t\tbreak\n\t\t}\n\n\t\tlists[j], lists[k] = lists[k], lists[j]\n\t\tk = j\n\t}\n}\n\n// brute force\n// time complexity: O(n log n), where n is len of all lists, the main complexity is sort.\n// space complexity: O(2n), where n is len of all lists.\nfunc mergeKLists1(lists []*ListNode) *ListNode {\n\tnodes := make([]int, 0)\n\thead := &ListNode{}\n\tpointer := head\n\n\tfor _, j := range lists {\n\t\tfor {\n\t\t\tif j != nil {\n\t\t\t\tnodes = append(nodes, j.Val)\n\t\t\t\tj = j.Next\n\t\t\t} else {\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\t}\n\tsort.Ints(nodes)\n\tfor _, j := range nodes {\n\t\tpointer.Next = &ListNode{Val: j}\n\t\tpointer = pointer.Next\n\t}\n\treturn head.Next\n}\n"
  },
  {
    "path": "solutions/0023_merge_k_sorted_lists/mksl_test.go",
    "content": "package mksl\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestMergeKLists(t *testing.T) {\n\ttestCases := [][]*ListNode{\n\t\t{\n\t\t\tcreateSingleList([]int{1, 4, 5}),\n\t\t\tcreateSingleList([]int{2, 3, 4}),\n\t\t\tcreateSingleList([]int{4, 6}),\n\t\t},\n\t\t{\n\t\t\tcreateSingleList([]int{}),\n\t\t\tcreateSingleList([]int{4, 5}),\n\t\t\tcreateSingleList([]int{}),\n\t\t},\n\t\t{\n\t\t\tcreateSingleList([]int{}),\n\t\t\tcreateSingleList([]int{}),\n\t\t\tcreateSingleList([]int{}),\n\t\t},\n\t\t{\n\t\t\tcreateSingleList([]int{1, 4, 5}),\n\t\t\tcreateSingleList([]int{1, 3, 4}),\n\t\t\tcreateSingleList([]int{2, 6}),\n\t\t},\n\t}\n\n\texpected := []*ListNode{\n\t\tcreateSingleList([]int{1, 2, 3, 4, 4, 4, 5, 6}),\n\t\tcreateSingleList([]int{4, 5}),\n\t\tnil,\n\t\tcreateSingleList([]int{1, 1, 2, 3, 4, 4, 5, 6}),\n\t}\n\n\tfor index, lists := range testCases {\n\t\tif res := mergeKLists(lists); !reflect.DeepEqual(res, expected[index]) {\n\t\t\tt.Errorf(\"expected %v, got %v\", expected[index], res)\n\t\t}\n\t}\n}\n\nfunc TestMergeKLists1(t *testing.T) {\n\ttestCases := [][]*ListNode{\n\t\t{\n\t\t\tcreateSingleList([]int{1, 4, 5}),\n\t\t\tcreateSingleList([]int{1, 3, 4}),\n\t\t\tcreateSingleList([]int{2, 6}),\n\t\t},\n\t\t{\n\t\t\tcreateSingleList([]int{}),\n\t\t\tcreateSingleList([]int{4, 5}),\n\t\t\tcreateSingleList([]int{}),\n\t\t},\n\t\t{\n\t\t\tcreateSingleList([]int{}),\n\t\t\tcreateSingleList([]int{}),\n\t\t\tcreateSingleList([]int{}),\n\t\t},\n\t}\n\n\texpected := []*ListNode{\n\t\tcreateSingleList([]int{1, 1, 2, 3, 4, 4, 5, 6}),\n\t\tcreateSingleList([]int{4, 5}),\n\t\tnil,\n\t}\n\n\tfor index, lists := range testCases {\n\t\tif res := mergeKLists1(lists); !reflect.DeepEqual(res, expected[index]) {\n\t\t\tt.Errorf(\"expected %v, got %v\", expected[index], res)\n\t\t}\n\t}\n}\n\nfunc createSingleList(nums []int) *ListNode {\n\thead := &ListNode{}\n\tcur := head\n\tfor _, j := range nums {\n\t\tcur.Next = &ListNode{Val: j}\n\t\tcur = cur.Next\n\t}\n\treturn head.Next\n}\n"
  },
  {
    "path": "solutions/0024_swap_nodes_in_pairs/swap_nodes_in_pairs.go",
    "content": "/*\n24. Swap Nodes in Pairs\nhttps://leetcode.com/problems/swap-nodes-in-pairs/\n\nGiven a linked list, swap every two adjacent nodes and return its head.\n*/\n// time: 2019-01-02\n\npackage swapnodesinpairs\n\n// ListNode Definition for singly-linked list.\ntype ListNode struct {\n\tVal  int\n\tNext *ListNode\n}\n\n// time complexity: O(n/2), where n is len of linked list.\n// space complexity: O(1)\nfunc swapPairs(head *ListNode) *ListNode {\n\tdummy := &ListNode{}\n\tdummy.Next = head\n\tcur := dummy\n\n\tfor cur.Next != nil && cur.Next.Next != nil {\n\t\tnode1 := cur.Next\n\t\tnode2 := node1.Next\n\t\tnode3 := node2.Next\n\n\t\tnode2.Next = node1\n\t\tnode1.Next = node3\n\t\tcur.Next = node2\n\t\tcur = node1\n\t}\n\treturn dummy.Next\n}\n"
  },
  {
    "path": "solutions/0024_swap_nodes_in_pairs/swap_nodes_in_pairs_test.go",
    "content": "package swapnodesinpairs\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestSwapPairs(t *testing.T) {\n\thead := createSingleList([]int{1, 2, 3, 4})\n\texpected := createSingleList([]int{2, 1, 4, 3})\n\tif res := swapPairs(head); !reflect.DeepEqual(res, expected) {\n\t\tt.Errorf(\"expected %v, got %v\", expected, res)\n\t}\n}\n\nfunc createSingleList(nums []int) *ListNode {\n\thead := &ListNode{}\n\tcur := head\n\tfor _, j := range nums {\n\t\tcur.Next = &ListNode{Val: j}\n\t\tcur = cur.Next\n\t}\n\treturn head.Next\n}\n"
  },
  {
    "path": "solutions/0025_reverse_nodes_in_k_group/reverse_node_k_group.go",
    "content": "/*\n25. Reverse Nodes in k-Group\n\nSource: https://leetcode.com/problems/reverse-nodes-in-k-group/\n\nGiven a linked list, reverse the nodes of a linked list k at a time and return its modified list.\n\nk is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.\n\nExample:\n\nGiven this linked list: 1->2->3->4->5\n\nFor k = 2, you should return: 2->1->4->3->5\n\nFor k = 3, you should return: 3->2->1->4->5\n\nNote:\n\tOnly constant extra memory is allowed.\n\tYou may not alter the values in the list's nodes, only nodes itself may be changed.\n*/\n\npackage reversenodesinkgroup\n\n// ListNode is node of linked list\ntype ListNode struct {\n\tVal  int\n\tNext *ListNode\n}\n\nfunc reverseKGroup(head *ListNode, k int) *ListNode {\n\tif head == nil || head.Next == nil || k < 2 {\n\t\treturn head\n\t}\n\n\tp := new(ListNode)\n\ts := new(ListNode)\n\tl := k\n\tp.Next = head\n\thead = p\n\ts = p\n\n\tfor s != nil && k != 0 {\n\t\ts = s.Next\n\t\tk--\n\t}\n\n\tfor s != nil {\n\t\treverse(&p, &s)\n\t\tk = l\n\t\tfor s != nil && k != 0 {\n\t\t\ts = s.Next\n\t\t\tk--\n\t\t}\n\t}\n\treturn head.Next\n}\n\nfunc reverse(p **ListNode, s **ListNode) {\n\tvar tmp *ListNode\n\tprev := (*p).Next\n\ttail := (*p).Next\n\tflag := (*s).Next\n\n\t(*p).Next = nil\n\tfor prev != flag {\n\t\ttmp = (*p).Next\n\t\t(*p).Next = prev\n\t\tprev = prev.Next\n\t\t(*p).Next.Next = tmp\n\t}\n\ttail.Next = prev\n\t*p = tail\n\t*s = tail\n}\n"
  },
  {
    "path": "solutions/0025_reverse_nodes_in_k_group/reverse_node_k_group_test.go",
    "content": "package reversenodesinkgroup\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc createSingleLinkedList(arr []int) *ListNode {\n\thead := &ListNode{}\n\tcur := head\n\n\tfor _, j := range arr {\n\t\tcur.Next = &ListNode{Val: j}\n\t\tcur = cur.Next\n\t}\n\treturn head.Next\n}\n\nfunc TestReverseKGroup(t *testing.T) {\n\ttype args struct {\n\t\thead *ListNode\n\t\tk    int\n\t}\n\ttests := []struct {\n\t\tname     string\n\t\targs     args\n\t\texpected *ListNode\n\t}{\n\t\t{\n\t\t\tname: \"one\",\n\t\t\targs: args{\n\t\t\t\thead: createSingleLinkedList([]int{1, 2, 3, 4, 5}),\n\t\t\t\tk:    2,\n\t\t\t},\n\t\t\texpected: createSingleLinkedList([]int{2, 1, 4, 3, 5}),\n\t\t},\n\t\t{\n\t\t\tname: \"two\",\n\t\t\targs: args{\n\t\t\t\thead: createSingleLinkedList([]int{1, 2, 3, 4, 5}),\n\t\t\t\tk:    3,\n\t\t\t},\n\t\t\texpected: createSingleLinkedList([]int{3, 2, 1, 4, 5}),\n\t\t},\n\n\t\t{\n\t\t\tname: \"three\",\n\t\t\targs: args{\n\t\t\t\thead: createSingleLinkedList([]int{1}),\n\t\t\t\tk:    2,\n\t\t\t},\n\t\t\texpected: createSingleLinkedList([]int{1}),\n\t\t},\n\t}\n\tfor _, data := range tests {\n\t\tt.Run(data.name, func(t *testing.T) {\n\t\t\tif result := reverseKGroup(data.args.head, data.args.k); !reflect.DeepEqual(result, data.expected) {\n\t\t\t\tt.Errorf(\"reverseKGroup() = %v, expected %v\", result, data.expected)\n\t\t\t}\n\t\t})\n\t}\n}\n"
  },
  {
    "path": "solutions/0026_remove_duplicates_from_sorted_array/rdfsa.go",
    "content": "/*\n26. Remove Duplicates from Sorted Array\nhttps://leetcode.com/problems/remove-duplicates-from-sorted-array/\n\nGiven a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.\n\nDo not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.\n*/\n// time: 2018-12-20\n\npackage rdfsa\n\n// double index\n// time complexity: O(n)\n// space complexity: O(1)\nfunc removeDuplicates(nums []int) int {\n\tn := len(nums)\n\n\tif 0 == n {\n\t\treturn n\n\t}\n\n\tvar (\n\t\tres   = 1\n\t\ti     = 1\n\t\tindex = nextDifferentCharacterIndex(nums, 1) // 下一个不重复的地址\n\t)\n\n\tfor index < n {\n\t\tres++\n\t\tnums[i] = nums[index]\n\t\ti++\n\t\tindex = nextDifferentCharacterIndex(nums, index+1)\n\t}\n\treturn res\n}\n\nfunc nextDifferentCharacterIndex(nums []int, p int) int {\n\tfor ; p < len(nums); p++ {\n\t\tif nums[p] != nums[p-1] {\n\t\t\tbreak\n\t\t}\n\t}\n\treturn p\n}\n"
  },
  {
    "path": "solutions/0026_remove_duplicates_from_sorted_array/rdfsa_test.go",
    "content": "package rdfsa\n\nimport \"testing\"\n\nfunc TestRemoveDuplicates(t *testing.T) {\n\t// removeDuplicates([]int{0, 0, 1, 1, 1, 2, 2, 3, 3, 4})\n\ttestCases := [][]int{\n\t\t{1, 1, 2},\n\t\t{0, 0, 1, 1, 1, 2, 2, 3, 3, 4},\n\t\t{},\n\t}\n\n\texpected := []int{2, 5, 0}\n\n\tfor index, data := range testCases {\n\t\tif res := removeDuplicates(data); expected[index] != res {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0027_remove_element/remove_element.go",
    "content": "/*\n27. Remove Element\nhttps://leetcode.com/problems/remove-element/\n\nGiven an array nums and a value val, remove all instances of that value in-place and return the new length.\n\nDo not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.\n\nThe order of elements can be changed. It doesn't matter what you leave beyond the new length.\n*/\n// time: 2018-12-20\n\npackage removeelement\n\n// two pointers\n// time complexity: O(n)\n// space complexity: O(1)\nfunc removeElement(nums []int, val int) int {\n\tx := 0\n\tfor j := 0; j < len(nums); j++ {\n\t\tif nums[j] != val {\n\t\t\tif x != j {\n\t\t\t\tnums[x] = nums[j]\n\t\t\t}\n\t\t\tx++\n\t\t}\n\t}\n\treturn x\n}\n\n/*\nfunc removeElement1(nums []int, val int) int {\n\tvar (\n\t\tl int\n\t\tr = len(nums)\n\t)\n\tfor l < r {\n\t\tif nums[l] == val {\n\t\t\tr--\n\t\t\tnums[l] = nums[r]\n\t\t} else {\n\t\t\tl++\n\t\t}\n\t}\n\treturn r\n}\n*/\n"
  },
  {
    "path": "solutions/0027_remove_element/remove_element_test.go",
    "content": "package removeelement\n\nimport \"testing\"\n\nfunc TestRemoveElement(t *testing.T) {\n\tnums := []int{0, 1, 2, 2, 3, 0, 4, 2}\n\tval := 2\n\texpected := 5\n\n\tif res := removeElement(nums, val); res != expected {\n\t\tt.Errorf(\"expected %d, got %d\", expected, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0028_implement_strstr/implement_strstr.go",
    "content": "/*\n28. Implement strStr()\nhttps://leetcode.com/problems/implement-strstr/\n\nImplement strStr().\n\nReturn the index of the first occurrence of needle in haystack,\nor -1 if needle is not part of haystack.\n\nclarification:\nFor the purpose of this problem, we will return 0 when needle is an empty string.\n*/\n// time: 2019-01-02\n\npackage implstr\n\n// time complexity: O(\t(m-n+1)*n\t), where m is len(haystack), n is len(needle)\n// space complexity: O(1)\nfunc strStr(haystack string, needle string) int {\n\tif 0 == len(needle) {\n\t\treturn 0\n\t}\n\n\tfor i, j := 0, 0; i <= len(haystack)-len(needle); i++ {\n\t\tfor j = 0; j < len(needle); j++ {\n\t\t\tif haystack[i+j] != needle[j] {\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\t\tif len(needle) == j {\n\t\t\treturn i\n\t\t}\n\t}\n\treturn -1\n}\n"
  },
  {
    "path": "solutions/0028_implement_strstr/implement_strstr_test.go",
    "content": "package implstr\n\nimport \"testing\"\n\nfunc TestStrStr(t *testing.T) {\n\ttype arg struct {\n\t\thaystack string\n\t\tneedle   string\n\t}\n\n\ttestCases := []arg{\n\t\t{haystack: \"hello\", needle: \"ll\"},\n\t\t{haystack: \"aaaaa\", needle: \"bba\"},\n\t\t{haystack: \"hello\"},\n\t}\n\n\texpected := []int{2, -1, 0}\n\tfor index, data := range testCases {\n\t\tif res := strStr(data.haystack, data.needle); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0033_search_in_rotated_sorted_array/search_in_rotated_sorted_array.go",
    "content": "/*\n33. Search in Rotated Sorted Array\nhttps://leetcode.com/problems/search-in-rotated-sorted-array/\n\nSuppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.\n\n(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).\n\nYou are given a target value to search. If found in the array return its index, otherwise return -1.\n\nYou may assume no duplicate exists in the array.\n\nYour algorithm's runtime complexity must be in the order of O(log n).\n*/\n// time: 2018-12-20\n\npackage searchinrotatedsortedarray\n\n// binary search\n/*\n问题分析：\n二分情况分为三种：\n1.mid在pivot左边，即mid左边部分是有序的。\n\tnums[mid] >= nums[l] && nums[mid] > nums[r]\n\t此时，只需要比较target是否在左边有序部分。\n2.mid在pivot右边，并且包含pivot，即mid右边是有序的。\n\tnums[mid] < nums[l] && nums[mid] <= nums[r],\n\t此时，只需要比较target是否在右边有序部分。\n3. 整个array是有序的，\n\tnums[mid] >= nums[l] &&nums[mid] <= nums[r]\n\t放在第一和第二中情况中处理都可以。\n*/\n// time complexity: O(logn)\n// space complexity: O(1)\nfunc search(nums []int, target int) int {\n\tvar (\n\t\tl int\n\t\tr = len(nums) - 1\n\t)\n\n\tfor l <= r {\n\t\tmid := l + (r-l)/2\n\t\tif target == nums[mid] {\n\t\t\treturn mid\n\t\t}\n\t\tif nums[mid] >= nums[l] && nums[mid] > nums[r] {\n\t\t\tif target >= nums[l] && target < nums[mid] {\n\t\t\t\tr = mid - 1\n\t\t\t} else {\n\t\t\t\tl = mid + 1\n\t\t\t}\n\t\t} else {\n\t\t\tif target > nums[mid] && target <= nums[r] {\n\t\t\t\tl = mid + 1\n\t\t\t} else {\n\t\t\t\tr = mid - 1\n\t\t\t}\n\t\t}\n\t}\n\treturn -1\n}\n"
  },
  {
    "path": "solutions/0033_search_in_rotated_sorted_array/search_in_rotated_sorted_array_test.go",
    "content": "package searchinrotatedsortedarray\n\nimport \"testing\"\n\nfunc TestSearch(t *testing.T) {\n\ttype arg struct {\n\t\tnums   []int\n\t\ttarget int\n\t}\n\n\ttestCases := []arg{\n\t\t{nums: []int{4, 5, 6, 7, 0, 1, 2}, target: 0},\n\t\t{nums: []int{4, 5, 6, 7, 0, 1, 2}, target: 3},\n\t\t{nums: []int{}, target: 2},\n\t\t{nums: []int{4}, target: 4},\n\t\t{nums: []int{4, 5, 6, 7, 0}, target: 0},\n\t\t{nums: []int{9, 7}, target: 7},\n\t\t{nums: []int{1, 3}, target: 3},\n\t\t{nums: []int{4, 5, 6, 7, 8, 1, 2}, target: 5},\n\t}\n\n\texpected := []int{4, -1, -1, 0, 4, 1, 1, 1}\n\n\tfor index, data := range testCases {\n\t\tif res := search(data.nums, data.target); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0034_find_first_and_last_position_of_element_in_sorted_array/find_first_and_last_position_of_element_in_sorted_array.go",
    "content": "/*\n34. Find First and Last Position of Element in Sorted Array\nhttps://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/\n\nGiven an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.\n\nYour algorithm's runtime complexity must be in the order of O(log n).\n\nIf the target is not found in the array, return [-1, -1].\n*/\n// time: 2018-12-20\n\npackage findfirstandlastpositionofelementinsortedarray\n\n// binary search\n// time complexity: O(logn)\n// space complexity: O(1)\nfunc searchRange(nums []int, target int) []int {\n\tlowerIndex := firstOccurance(nums, target)\n\tfirst := -1\n\tif lowerIndex != len(nums) && target == nums[lowerIndex] {\n\t\tfirst = lowerIndex\n\t}\n\n\tupperIndex := lastOccurance(nums, target)\n\tlast := -1\n\tif upperIndex == len(nums) && len(nums) > 0 && target == nums[len(nums)-1] {\n\t\tlast = len(nums) - 1\n\t} else if upperIndex != len(nums) && upperIndex > 0 && target == nums[upperIndex-1] {\n\t\tlast = upperIndex - 1\n\t}\n\treturn []int{first, last}\n}\n\nfunc firstOccurance(nums []int, target int) int {\n\tvar (\n\t\tl int\n\t\tr = len(nums)\n\t)\n\n\tfor l != r { // 夹逼思想\n\t\tmid := l + (r-l)/2\n\t\tif nums[mid] < target {\n\t\t\tl = mid + 1\n\t\t} else {\n\t\t\tr = mid\n\t\t}\n\t}\n\treturn l\n}\n\nfunc lastOccurance(nums []int, target int) int {\n\tvar (\n\t\tl int\n\t\tr = len(nums)\n\t)\n\tfor l != r {\n\t\tmid := l + (r-l)/2\n\t\tif nums[mid] <= target {\n\t\t\tl = mid + 1\n\t\t} else {\n\t\t\tr = mid\n\t\t}\n\t}\n\treturn l\n}\n\n// double index scan\n// Time complexity: O(n)\n// Space complexity: O(1)\nfunc searchRange1(nums []int, target int) []int {\n\tvar (\n\t\tl int\n\t\tr = len(nums) - 1\n\t)\n\n\tfor l <= r {\n\t\tflag := false\n\t\tif nums[l] != target {\n\t\t\tl++\n\t\t\tflag = true\n\t\t}\n\t\tif nums[r] != target {\n\t\t\tr--\n\t\t\tflag = true\n\t\t}\n\t\tif !flag {\n\t\t\tbreak\n\t\t}\n\t}\n\tif r < l {\n\t\treturn []int{-1, -1}\n\t}\n\treturn []int{l, r}\n}\n\n// binary search + linear scan\n// Time complexity: O(logn) ~ O(n)\n// Space complexity: O(1)\nfunc searchRange2(nums []int, target int) []int {\n\tvar (\n\t\tl   int\n\t\tr   = len(nums) - 1\n\t\ttmp = -1\n\t)\n\n\tfor l <= r {\n\t\tmid := l + (r-l)/2\n\t\tif nums[mid] == target {\n\t\t\ttmp = mid\n\t\t\tbreak\n\t\t}\n\t\tif nums[mid] < target {\n\t\t\tl = mid + 1\n\t\t} else {\n\t\t\tr = mid - 1\n\t\t}\n\t}\n\tif -1 == tmp {\n\t\treturn []int{-1, -1}\n\t}\n\tl = tmp\n\tr = tmp\n\tfor true {\n\t\tif l > 0 && nums[l-1] == target {\n\t\t\tl--\n\t\t} else {\n\t\t\tbreak\n\t\t}\n\t}\n\tfor true {\n\t\tif r < len(nums)-1 && target == nums[r+1] {\n\t\t\tr++\n\t\t} else {\n\t\t\tbreak\n\t\t}\n\t}\n\treturn []int{l, r}\n}\n"
  },
  {
    "path": "solutions/0034_find_first_and_last_position_of_element_in_sorted_array/find_first_and_last_position_of_element_in_sorted_array_test.go",
    "content": "package findfirstandlastpositionofelementinsortedarray\n\nimport (\n\t\"reflect\"\n\t\"runtime\"\n\t\"testing\"\n)\n\nfunc TestSearchRange(t *testing.T) {\n\ttype arg struct {\n\t\tnums   []int\n\t\ttarget int\n\t}\n\n\ttestCases := []arg{\n\t\t{\n\t\t\tnums:   []int{5, 7, 7, 8, 8, 10},\n\t\t\ttarget: 8,\n\t\t},\n\t\t{\n\t\t\tnums:   []int{5, 7, 7, 8, 8, 10},\n\t\t\ttarget: 6,\n\t\t},\n\t\t{\n\t\t\tnums:   []int{1},\n\t\t\ttarget: 1,\n\t\t},\n\t\t{\n\t\t\tnums:   []int{},\n\t\t\ttarget: 0,\n\t\t},\n\t\t{\n\t\t\tnums:   []int{2, 2},\n\t\t\ttarget: 2,\n\t\t},\n\t\t{\n\t\t\tnums:   []int{1},\n\t\t\ttarget: 0,\n\t\t},\n\t}\n\n\texpected := [][]int{\n\t\t{3, 4},\n\t\t{-1, -1},\n\t\t{0, 0},\n\t\t{-1, -1},\n\t\t{0, 1},\n\t\t{-1, -1},\n\t}\n\n\ttestFuncs := []func([]int, int) []int{\n\t\tsearchRange,\n\t\tsearchRange1,\n\t\tsearchRange2,\n\t}\n\n\tfor _, testFunc := range testFuncs {\n\t\tfor index, testData := range testCases {\n\t\t\tif res := testFunc(testData.nums, testData.target); !reflect.DeepEqual(res, expected[index]) {\n\t\t\t\tt.Errorf(\"function %s, expected %v, got %v\", runtime.FuncForPC(reflect.ValueOf(testFunc).Pointer()).Name(), expected[index], res)\n\t\t\t}\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0035_search_insert_position/search_insert_position.go",
    "content": "/*\n35. Search Insert Position\nhttps://leetcode.com/problems/search-insert-position/\n\nGiven a sorted array and a target value,\nreturn the index if the target is found.\nIf not, return the index where it would be if it were inserted in order.\n\nYou may assume no duplicates in the array.\n*/\n// time: 2019-01-02\n\npackage sip\n\n// binary search\n// time complexity: O(\tlog n\t\t)\n// space complexity: O(1)\nfunc searchInsert(nums []int, target int) int {\n\tvar (\n\t\tl int\n\t\tr = len(nums) - 1\n\t)\n\n\tfor l <= r {\n\t\tmid := l + (r-l)/2\n\t\tif target == nums[mid] {\n\t\t\treturn mid\n\t\t}\n\t\tif target < nums[mid] {\n\t\t\tr = mid - 1\n\t\t} else {\n\t\t\tl = mid + 1\n\t\t}\n\t}\n\treturn l\n}\n"
  },
  {
    "path": "solutions/0035_search_insert_position/search_insert_position_test.go",
    "content": "package sip\n\nimport \"testing\"\n\nfunc TestSearchInsert(t *testing.T) {\n\ttestCases := [][]int{\n\t\t{1, 3, 5, 6},\n\t\t{1, 3, 5, 6},\n\t\t{1, 3, 5, 6},\n\t\t{1, 3, 5, 6},\n\t}\n\ttargets := []int{5, 2, 7, 0}\n\n\texpected := []int{2, 1, 4, 0}\n\n\tfor index, nums := range testCases {\n\t\tif res := searchInsert(nums, targets[index]); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0048_rotate_image/rotate_image.go",
    "content": "/*\n48. Rotate Image\nhttps://leetcode.com/problems/rotate-image/\n\nYou are given an n x n 2D matrix representing an image.\nRotate the image by 90 degrees (clockwise).\n\nNote:\nYou have to rotate the image in-place,\nwhich means you have to modify the input 2D matrix directly.\nDO NOT allocate another 2D matrix and do the rotation.\n*/\n// time: 2019-01-02\n\npackage ri\n\n// time complexity: O(n^2)\n// space complexity: O(1)\nfunc rotate(matrix [][]int) {\n\t/*\n\t\t\t[\n\t\t  \t\t[1,2,3],\n\t\t  \t\t[4,5,6],\n\t\t  \t\t[7,8,9]\n\t\t\t]\n\t*/\n\tn := len(matrix)\n\tfor i := 0; i < n; i++ {\n\t\tfor j := i + 1; j < n; j++ {\n\t\t\tmatrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]\n\t\t}\n\t}\n\t/*\n\t\t\t[\n\t\t  \t\t[1,4,7],\n\t\t  \t\t[2,5,8],\n\t\t  \t\t[3,6,9]\n\t\t\t]\n\t*/\n\tfor i := 0; i < n; i++ {\n\t\tfor j := 0; j < n/2; j++ {\n\t\t\tmatrix[i][j], matrix[i][n-1-j] = matrix[i][n-1-j], matrix[i][j]\n\t\t}\n\t}\n\t/*\n\t\t\t[\n\t\t  \t\t[7,4,1],\n\t\t  \t\t[8,5,2],\n\t\t  \t\t[9,6,3]\n\t\t\t]\n\t*/\n}\n"
  },
  {
    "path": "solutions/0048_rotate_image/rotate_image_test.go",
    "content": "package ri\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestRotate(t *testing.T) {\n\tmatrix := [][]int{\n\t\t{5, 1, 9, 11},\n\t\t{2, 4, 8, 10},\n\t\t{13, 3, 6, 7},\n\t\t{15, 14, 12, 16},\n\t}\n\n\texpected := [][]int{\n\t\t{15, 13, 2, 5},\n\t\t{14, 3, 4, 1},\n\t\t{12, 6, 8, 9},\n\t\t{16, 7, 10, 11},\n\t}\n\n\tif rotate(matrix); !reflect.DeepEqual(matrix, expected) {\n\t\tt.Errorf(\"expected %v, got %v\", expected, matrix)\n\t}\n}\n"
  },
  {
    "path": "solutions/0053_maximum_subarray/maximum_subarray.go",
    "content": "/*\n53. Maximum Subarray\nhttps://leetcode.com/problems/maximum-subarray/\n\nGiven an integer array nums,\nfind the contiguous subarray (containing at least one number) which has the largest sum and return its sum.\n*/\n// time: 2019-01-02\n\npackage maximumsubarray\n\nimport (\n\t\"fmt\"\n\t\"math\"\n)\n\n// time complexity: O(n)\n// space complexity: O(1)\nfunc maxSubArray(nums []int) int {\n\tvar (\n\t\tmax           = math.MinInt32\n\t\tsum           int\n\t\tstart, end, f int\n\t)\n\tfor i, j := range nums {\n\t\tsum += j\n\t\tif sum > max {\n\t\t\tif f != 0 {\n\t\t\t\tstart = f + 1\n\t\t\t\tf = 0\n\t\t\t}\n\t\t\tmax = sum\n\t\t\tend = i\n\t\t}\n\t\tif sum < 0 {\n\t\t\tf = i\n\t\t\tsum = 0\n\t\t}\n\t}\n\tfmt.Printf(\"start index is %d, end index is %d\\n\", start, end)\n\treturn max\n}\n"
  },
  {
    "path": "solutions/0053_maximum_subarray/maximum_subarray_test.go",
    "content": "package maximumsubarray\n\nimport \"testing\"\n\nfunc TestMaxSubarray(t *testing.T) {\n\tnums := []int{-2, 1, -3, 4, -1, 2, 1, -5, 4}\n\texpected := 6\n\n\tif res := maxSubArray(nums); res != expected {\n\t\tt.Errorf(\"expected %d, got %d\", expected, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0058_length_of_last_word/len_of_last_word.go",
    "content": "/*\n58. Length of Last Word\nhttps://leetcode.com/problems/length-of-last-word/\n\nGiven a string s consists of upper/lower-case alphabets and empty space characters ' ',\nreturn the length of last word in the string.\nIf the last word does not exist, return 0.\n\nNote: A word is defined as a character sequence consists of non-space characters only.\n*/\n// time: 2019-01-02\n\npackage lenoflastword\n\n// time complexity: O(n)\n// space complexity: O(1)\nfunc lengthOfLastWord(s string) int {\n\tvar (\n\t\tn   = len(s)\n\t\tcur = n - 1\n\t)\n\n\tfor cur >= 0 {\n\t\tif n-1 == cur && s[cur] == 32 {\n\t\t\tcur--\n\t\t\tn--\n\t\t\tcontinue\n\t\t}\n\t\tif s[cur] != 32 {\n\t\t\tcur--\n\t\t} else {\n\t\t\tbreak\n\t\t}\n\t}\n\treturn n - cur - 1\n}\n"
  },
  {
    "path": "solutions/0058_length_of_last_word/len_of_last_word_test.go",
    "content": "package lenoflastword\n\nimport \"testing\"\n\nfunc TestLengthOfLastWord(t *testing.T) {\n\ts := \"hello world \"\n\texpected := 5\n\tif res := lengthOfLastWord(s); res != expected {\n\t\tt.Errorf(\"expected %d, got %d\", expected, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0061_rotate_list/rotate_list.go",
    "content": "/*\n61. Rotate List\n\nSource: https://leetcode.com/problems/rotate-list/\n\nGiven a linked list, rotate the list to the right by k places, where k is non-negative.\n\nExample 1:\n\tInput: 1->2->3->4->5->NULL, k = 2\n\tOutput: 4->5->1->2->3->NULL\n\tExplanation:\n\trotate 1 steps to the right: 5->1->2->3->4->NULL\n\trotate 2 steps to the right: 4->5->1->2->3->NULL\n\nExample 2:\n\tInput: 0->1->2->NULL, k = 4\n\tOutput: 2->0->1->NULL\n\trotate 1 steps to the right: 2->0->1->NULL\n\tExplanation:\n\trotate 2 steps to the right: 1->2->0->NULL\n\totate 3 steps to the right: 0->1->2->NULL\n\trotate 4 steps to the right: 2->0->1->NULL\n*/\n\npackage rotatelist\n\n// ListNode is node of linked list\ntype ListNode struct {\n\tVal  int\n\tNext *ListNode\n}\n\nfunc rotateRight(head *ListNode, k int) *ListNode {\n\tif k == 0 || head == nil || head.Next == nil {\n\t\treturn head\n\t}\n\n\tvar (\n\t\tlistSize   int\n\t\tcount      int\n\t\tp          = head\n\t\trotateNode = head\n\t)\n\n\tfor p != nil && count < k {\n\t\tp = p.Next\n\t\tlistSize++\n\t\tcount++\n\t}\n\n\tif p == nil {\n\t\tk = k % listSize\n\t\tif k == 0 {\n\t\t\treturn head\n\t\t}\n\t\tp = head\n\t\tfor count = 0; count < k; count++ {\n\t\t\tp = p.Next\n\t\t}\n\t}\n\n\tfor p.Next != nil {\n\t\trotateNode = rotateNode.Next\n\t\tp = p.Next\n\t}\n\n\tp.Next = head\n\thead = rotateNode.Next\n\trotateNode.Next = nil\n\treturn head\n}\n"
  },
  {
    "path": "solutions/0061_rotate_list/rotate_list_test.go",
    "content": "package rotatelist\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc createSingleLinkedList(arr []int) *ListNode {\n\thead := &ListNode{}\n\tcur := head\n\n\tfor _, j := range arr {\n\t\tcur.Next = &ListNode{Val: j}\n\t\tcur = cur.Next\n\t}\n\treturn head.Next\n}\n\nfunc TestRotateList(t *testing.T) {\n\ttestDatas := []struct {\n\t\tname     string\n\t\targ      *ListNode\n\t\tk        int\n\t\texpected *ListNode\n\t}{\n\t\t{\n\t\t\tname:     \"one\",\n\t\t\targ:      createSingleLinkedList([]int{1, 2, 3, 4}),\n\t\t\tk:        2,\n\t\t\texpected: createSingleLinkedList([]int{3, 4, 1, 2}),\n\t\t},\n\t\t{\n\t\t\tname:     \"two\",\n\t\t\targ:      createSingleLinkedList([]int{1, 2, 3}),\n\t\t\tk:        3,\n\t\t\texpected: createSingleLinkedList([]int{1, 2, 3}),\n\t\t},\n\t\t{\n\t\t\tname:     \"three\",\n\t\t\targ:      createSingleLinkedList([]int{1, 2, 3}),\n\t\t\tk:        5,\n\t\t\texpected: createSingleLinkedList([]int{2, 3, 1}),\n\t\t},\n\t\t{\n\t\t\tname:     \"four\",\n\t\t\targ:      createSingleLinkedList([]int{1, 2, 3}),\n\t\t\tk:        0,\n\t\t\texpected: createSingleLinkedList([]int{1, 2, 3}),\n\t\t},\n\t\t{\n\t\t\tname:     \"five\",\n\t\t\targ:      nil,\n\t\t\tk:        0,\n\t\t\texpected: nil,\n\t\t},\n\t\t{\n\t\t\tname:     \"four\",\n\t\t\targ:      createSingleLinkedList([]int{1}),\n\t\t\tk:        5,\n\t\t\texpected: createSingleLinkedList([]int{1}),\n\t\t},\n\t}\n\n\tfor _, testData := range testDatas {\n\t\tt.Run(testData.name, func(t *testing.T) {\n\t\t\tif result := rotateRight(testData.arg, testData.k); !reflect.DeepEqual(result, testData.expected) {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", testData.expected, result)\n\t\t\t}\n\t\t})\n\t}\n}\n"
  },
  {
    "path": "solutions/0062_unique_paths/unique_paths.go",
    "content": "/*\n62. Unique Paths\nSource: https://leetcode.com/problems/unique-paths/\n\nA robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).\n\nThe robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).\n\nHow many possible unique paths are there?\n\n\nAbove is a 7 x 3 grid. How many possible unique paths are there?\n\nNote: m and n will be at most 100.\n\nExample 1:\n\nInput: m = 3, n = 2\nOutput: 3\nExplanation:\nFrom the top-left corner, there are a total of 3 ways to reach the bottom-right corner:\n1. Right -> Right -> Down\n2. Right -> Down -> Right\n3. Down -> Right -> Right\nExample 2:\n\nInput: m = 7, n = 3\nOutput: 28\n*/\n\npackage uniquepaths\n\n// recursion\n/*\nfunc uniquePaths(m int, n int) int {\n\tif m == 1 || n == 1 {\n\t\treturn 1\n\t}\n\treturn uniquePaths(m, n-1) + uniquePaths(m-1, n)\n}\n*/\n\n// memory search\n/*\nfunc uniquePaths(m int, n int) int {\n\tmemo := make([][]int, m) // memo[i][j]存储(i+1)*(j+1)grid的路径数量\n\tfor i := 0; i < m; i++ {\n\t\tfor j := 0; j < n; j++ {\n\t\t\tmemo[i] = append(memo[i], -1)\n\t\t}\n\t}\n\treturn calcPaths(m, n, memo)\n}\n\nfunc calcPaths(m int, n int, memo [][]int) int {\n\tif memo[m-1][n-1] != -1 {\n\t\treturn memo[m-1][n-1]\n\t}\n\n\tif m == 1 || n == 1 {\n\t\tmemo[m-1][n-1] = 1\n\t\treturn 1\n\t}\n\n\tres := calcPaths(m, n-1, memo) + calcPaths(m-1, n, memo)\n\tmemo[m-1][n-1] = res\n\treturn res\n}\n*/\n\nfunc uniquePaths(m int, n int) int {\n\tmemo := make([][]int, m) // memo[i][j]存储(i+1)*(j+1)grid的路径数量\n\tfor i := 0; i < m; i++ {\n\t\tmemo[i] = append(memo[i], 1)\n\t}\n\n\tfor i := 1; i < n; i++ {\n\t\tmemo[0] = append(memo[0], 1)\n\t}\n\n\tfor i := 1; i < m; i++ {\n\t\tfor j := 1; j < n; j++ {\n\t\t\tmemo[i] = append(memo[i], memo[i-1][j]+memo[i][j-1])\n\t\t}\n\t}\n\treturn memo[m-1][n-1]\n}\n"
  },
  {
    "path": "solutions/0062_unique_paths/unique_paths_test.go",
    "content": "package uniquepaths\n\nimport \"testing\"\n\nfunc TestUniquePaths(t *testing.T) {\n\ttestData := [][2]int{\n\t\t{3, 2},\n\t\t{51, 9},\n\t\t{9, 9},\n\t}\n\n\texpectedData := []int{3, 1916797311, 12870}\n\n\tfor index, data := range testData {\n\t\tif res := uniquePaths(data[0], data[1]); res != expectedData[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expectedData[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0063_unique_paths_2/unique_paths2.go",
    "content": "/*\n63. Unique Paths II\n\nSource: https://leetcode.com/problems/unique-paths-ii/\n\nA robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).\n\nThe robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).\n\nNow consider if some obstacles are added to the grids. How many unique paths would there be?\n\n\n\nAn obstacle and empty space is marked as 1 and 0 respectively in the grid.\n\nNote: m and n will be at most 100.\n\nExample 1:\n\nInput:\n[\n  [0,0,0],\n  [0,1,0],\n  [0,0,0]\n]\nOutput: 2\nExplanation:\nThere is one obstacle in the middle of the 3x3 grid above.\nThere are two ways to reach the bottom-right corner:\n1. Right -> Right -> Down -> Down\n2. Down -> Down -> Right -> Right\n*/\n\npackage uniquepaths2\n\n// recursion\n/*\nfunc uniquePathsWithObstacles(obstacleGrid [][]int) int {\n\tm := len(obstacleGrid)\n\tn := len(obstacleGrid[0])\n\n\tcur := 1\n\tif m == 1 {\n\t\tfor i := 0; i < n; i++ {\n\t\t\tif obstacleGrid[0][i] == 1 {\n\t\t\t\tcur = 0\n\t\t\t}\n\t\t}\n\t\treturn cur\n\t}\n\n\tcur = 1\n\tif n == 1 {\n\t\tfor i := 0; i < m; i++ {\n\t\t\tif obstacleGrid[i][0] == 1 {\n\t\t\t\tcur = 0\n\t\t\t}\n\t\t}\n\t\treturn cur\n\t}\n\tif obstacleGrid[m-1][n-1] == 1 {\n\t\treturn 0\n\t}\n\n\tobstacleGridCut := make([][]int, m)\n\n\tfor i := range obstacleGridCut {\n\t\tfor j := 0; j < n-1; j++ {\n\t\t\tobstacleGridCut[i] = append(obstacleGridCut[i], obstacleGrid[i][j])\n\t\t}\n\t}\n\n\treturn uniquePathsWithObstacles(obstacleGrid[:m-1]) + uniquePathsWithObstacles(obstacleGridCut)\n}\n*/\n\n// memory search\n/*\nfunc uniquePathsWithObstacles(obstacleGrid [][]int) int {\n\tm := len(obstacleGrid)\n\tn := len(obstacleGrid[0])\n\n\tmemo := make([][]int, m)\n\tfor i := 0; i < m; i++ {\n\t\tfor j := 0; j < n; j++ {\n\t\t\tmemo[i] = append(memo[i], -1)\n\t\t}\n\t}\n\n\tcur := 1\n\tfor i := 0; i < m; i++ {\n\t\tif obstacleGrid[i][0] == 1 {\n\t\t\tcur = 0\n\t\t}\n\t\tmemo[i][0] = cur\n\t}\n\n\tcur = 1\n\tfor j := 0; j < n; j++ {\n\t\tif obstacleGrid[0][j] == 1 {\n\t\t\tcur = 0\n\t\t}\n\t\tmemo[0][j] = cur\n\t}\n\treturn calcUniquePaths(obstacleGrid, m-1, n-1, memo)\n}\n\nfunc calcUniquePaths(obstacleGrid [][]int, m int, n int, memo [][]int) int {\n\tif memo[m][n] != -1 {\n\t\treturn memo[m][n]\n\t}\n\tif obstacleGrid[m][n] == 1 {\n\t\treturn 0\n\t}\n\tres := calcUniquePaths(obstacleGrid, m-1, n, memo) + calcUniquePaths(obstacleGrid, m, n-1, memo)\n\tmemo[m][n] = res\n\treturn res\n}\n*/\n\n// dynamic programming\nfunc uniquePathsWithObstacles(obstacleGrid [][]int) int {\n\tm := len(obstacleGrid)\n\tn := len(obstacleGrid[0])\n\n\tmemo := make([][]int, m)\n\tfor i := 0; i < m; i++ {\n\t\tfor j := 0; j < n; j++ {\n\t\t\tmemo[i] = append(memo[i], -1)\n\t\t}\n\t}\n\tcur := 1\n\tfor i := 0; i < m; i++ {\n\t\tif obstacleGrid[i][0] == 1 {\n\t\t\tcur = 0\n\t\t}\n\t\tmemo[i][0] = cur\n\t}\n\n\tcur = 1\n\tfor j := 0; j < n; j++ {\n\t\tif obstacleGrid[0][j] == 1 {\n\t\t\tcur = 0\n\t\t}\n\t\tmemo[0][j] = cur\n\t}\n\tfor i := 1; i < m; i++ {\n\t\tfor j := 1; j < n; j++ {\n\t\t\tif obstacleGrid[i][j] == 1 {\n\t\t\t\tmemo[i][j] = 0\n\t\t\t} else {\n\t\t\t\tmemo[i][j] = memo[i-1][j] + memo[i][j-1]\n\t\t\t}\n\t\t}\n\t}\n\treturn memo[m-1][n-1]\n}\n"
  },
  {
    "path": "solutions/0063_unique_paths_2/unique_paths2_test.go",
    "content": "package uniquepaths2\n\nimport \"testing\"\n\nfunc TestUniquePaths2(t *testing.T) {\n\ttestData := [][][]int{\n\t\t{\n\t\t\t{0, 0, 0},\n\t\t\t{0, 1, 0},\n\t\t\t{0, 0, 0},\n\t\t},\n\t\t{\n\t\t\t{0, 0, 0},\n\t\t\t{0, 1, 0},\n\t\t\t{0, 0, 0},\n\t\t\t{0, 1, 0},\n\t\t},\n\t\t{\n\t\t\t{1},\n\t\t},\n\t\t{\n\t\t\t{1, 0},\n\t\t},\n\t}\n\n\texpectedData := []int{2, 2, 0, 0}\n\n\tfor index, data := range testData {\n\t\tif res := uniquePathsWithObstacles(data); res != expectedData[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expectedData[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0064_minimum_path_sum/minimum_path_sum.go",
    "content": "/*\n64. Minimum Path Sum\nsource: https://leetcode.com/problems/minimum-path-sum/\n\nDescription\nGiven a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.\n Note: You can only move either down or right at any point in time.\n\nExample:\n\nInput:\n[\n  [1,3,1],\n  [1,5,1],\n  [4,2,1]\n]\nOutput: 7\nExplanation: Because the path 1→3→1→1→1 minimizes the sum.\n*/\n\npackage minimumpathsum\n\n// recursion dfs\n/*\nfunc minPathSum(grid [][]int) int {\n    if len(grid) == 0{\n        return 0\n    }\n\n    MaxUint:= ^uint(0)\n    MaxInt := int(MaxUint >> 1)\n\n    result := MaxInt\n\n    dfs(0,0,0, grid, &result)\n    return result\n}\n\nfunc dfs(x int, y int, sum int, grid [][]int, result *int) {\n    m := len(grid)\n    n := len(grid[0])\n\n    sum += grid[x][y]\n\n    if x+1 < m && y+1 < n {\n        dfs(x+1, y, sum, grid, result)\n        dfs(x, y+1, sum, grid, result)\n    }\n\n    if x +1 == m && y+1 < n {\n        dfs(x, y+1, sum, grid, result)\n    }\n\n    if y+1 == n && x+1 < m {\n        dfs(x+1, y, sum, grid, result)\n    }\n\n\n    if x+1 == m && y+1 == n {\n        if sum < *result {\n            *result = sum\n        }\n        return\n    }\n}*/\n\n// dynamic programming\nfunc minPathSum(grid [][]int) int {\n\tif len(grid) == 0 {\n\t\treturn 0\n\t}\n\tm := len(grid)\n\tn := len(grid[0])\n\n\tdp := make([][]int, m)\n\n\tdp[0] = append(dp[0], grid[0][0])\n\n\tfor i := 1; i < m; i++ {\n\t\tdp[i] = append(dp[i], grid[i][0]+dp[i-1][0])\n\t}\n\n\tfor i := 1; i < n; i++ {\n\t\tdp[0] = append(dp[0], grid[0][i]+dp[0][i-1])\n\t}\n\n\tfor i := 1; i < m; i++ {\n\t\tfor j := 1; j < n; j++ {\n\t\t\tif dp[i-1][j] < dp[i][j-1] {\n\t\t\t\tdp[i] = append(dp[i], grid[i][j]+dp[i-1][j])\n\t\t\t} else {\n\t\t\t\tdp[i] = append(dp[i], grid[i][j]+dp[i][j-1])\n\t\t\t}\n\t\t}\n\t}\n\treturn dp[m-1][n-1]\n}\n"
  },
  {
    "path": "solutions/0064_minimum_path_sum/minimum_path_sum_test.go",
    "content": "package minimumpathsum\n\nimport \"testing\"\n\nfunc TestMininumPathSum(t *testing.T) {\n\ttestData := [][][]int{\n\t\t{\n\t\t\t{1, 3, 1},\n\t\t\t{1, 5, 1},\n\t\t\t{4, 2, 1},\n\t\t},\n\t\t{\n\t\t\t{3, 8, 6, 0, 5, 9, 9, 6, 3, 4, 0, 5, 7, 3, 9, 3},\n\t\t\t{0, 9, 2, 5, 5, 4, 9, 1, 4, 6, 9, 5, 6, 7, 3, 2},\n\t\t\t{8, 2, 2, 3, 3, 3, 1, 6, 9, 1, 1, 6, 6, 2, 1, 9},\n\t\t\t{1, 3, 6, 9, 9, 5, 0, 3, 4, 9, 1, 0, 9, 6, 2, 7},\n\t\t\t{8, 6, 2, 2, 1, 3, 0, 0, 7, 2, 7, 5, 4, 8, 4, 8},\n\t\t\t{4, 1, 9, 5, 8, 9, 9, 2, 0, 2, 5, 1, 8, 7, 0, 9},\n\t\t\t{6, 2, 1, 7, 8, 1, 8, 5, 5, 7, 0, 2, 5, 7, 2, 1},\n\t\t\t{8, 1, 7, 6, 2, 8, 1, 2, 2, 6, 4, 0, 5, 4, 1, 3},\n\t\t\t{9, 2, 1, 7, 6, 1, 4, 3, 8, 6, 5, 5, 3, 9, 7, 3},\n\t\t\t{0, 6, 0, 2, 4, 3, 7, 6, 1, 3, 8, 6, 9, 0, 0, 8},\n\t\t\t{4, 3, 7, 2, 4, 3, 6, 4, 0, 3, 9, 5, 3, 6, 9, 3},\n\t\t\t{2, 1, 8, 8, 4, 5, 6, 5, 8, 7, 3, 7, 7, 5, 8, 3},\n\t\t\t{0, 7, 6, 6, 1, 2, 0, 3, 5, 0, 8, 0, 8, 7, 4, 3},\n\t\t\t{0, 4, 3, 4, 9, 0, 1, 9, 7, 7, 8, 6, 4, 6, 9, 5},\n\t\t\t{6, 5, 1, 9, 9, 2, 2, 7, 4, 2, 7, 2, 2, 3, 7, 2},\n\t\t\t{7, 1, 9, 6, 1, 2, 7, 0, 9, 6, 6, 4, 4, 5, 1, 0},\n\t\t\t{3, 4, 9, 2, 8, 3, 1, 2, 6, 9, 7, 0, 2, 4, 2, 0},\n\t\t\t{5, 1, 8, 8, 4, 6, 8, 5, 2, 4, 1, 6, 2, 2, 9, 7},\n\t\t},\n\t\t{},\n\t}\n\n\texpectedData := []int{7, 83, 0}\n\n\tfor index, data := range testData {\n\t\tif mininumPathSum := minPathSum(data); expectedData[index] != mininumPathSum {\n\t\t\tt.Errorf(\"expected %d, got %d\", expectedData[index], mininumPathSum)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0066_plus_one/plus_one.go",
    "content": "/*\n66. Plus One\nhttps://leetcode.com/problems/plus-one/\n\nGiven a non-empty array of digits representing a non-negative integer,\nplus one to the integer.\n\nThe digits are stored such that the most significant digit is at the head of the list,\nand each element in the array contain a single digit.\n\nYou may assume the integer does not contain any leading zero, except the number 0 itself.\n*/\n// time: 2019-01-02\n\npackage plusone\n\n// time complexity: O(n)\n// space complexity: O(1)\nfunc plusOne(digits []int) []int {\n\tvar (\n\t\tcarry = 1\n\t\tindex = len(digits) - 1\n\t)\n\tfor index >= 0 {\n\t\tsum := carry + digits[index]\n\t\tdigits[index] = sum % 10\n\t\tcarry = sum / 10\n\t\tindex--\n\t}\n\n\tif carry > 0 {\n\t\tdigits = append(digits, 0)\n\t\tcopy(digits[1:], digits[0:])\n\t\tdigits[0] = carry\n\t}\n\treturn digits\n}\n"
  },
  {
    "path": "solutions/0066_plus_one/plus_one_test.go",
    "content": "package plusone\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestPlusOne(t *testing.T) {\n\tdigits := []int{9, 9, 9, 9}\n\texpected := []int{1, 0, 0, 0, 0}\n\tif res := plusOne(digits); !reflect.DeepEqual(expected, res) {\n\t\tt.Errorf(\"expected %v, got %v\", expected, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0067_add_binary/add_binary.go",
    "content": "/*\n67. Add Binary\nhttps://leetcode.com/problems/add-binary/\n\nGiven two binary strings, return their sum (also a binary string).\nThe input strings are both non-empty and contains only characters 1 or 0.\n\nExample:\n\tInput: a = \"11\", b = \"1\"\n\tOutput: \"100\"\n*/\n// time: 2018-12-19\n\npackage addbinary\n\nimport (\n\t\"strconv\"\n)\n\n// Time complexity: O(\tmax(\tlen(a),    len(b)\t)\t)\n// Space complexity: O(1)\nfunc addBinary(a string, b string) string {\n\tvar (\n\t\tlenA  = len(a)\n\t\tlenB  = len(b)\n\t\tcarry int\n\t\tres   = \"\"\n\t)\n\tfor lenA > 0 && lenB > 0 {\n\t\ttmp := int(a[lenA-1]-'0') + int(b[lenB-1]-'0') + carry\n\t\tres = strconv.Itoa(tmp%2) + res\n\t\tcarry = tmp / 2\n\t\tlenA--\n\t\tlenB--\n\t}\n\n\tif lenA == 0 {\n\t\tfor lenB > 0 {\n\t\t\ttmp := int(b[lenB-1]-'0') + carry\n\t\t\tres = strconv.Itoa(tmp%2) + res\n\t\t\tcarry = tmp / 2\n\t\t\tlenB--\n\t\t}\n\t}\n\n\tif lenB == 0 {\n\t\tfor lenA > 0 {\n\t\t\ttmp := int(a[lenA-1]-'0') + carry\n\t\t\tres = strconv.Itoa(tmp%2) + res\n\t\t\tcarry = tmp / 2\n\t\t\tlenA--\n\t\t}\n\t}\n\n\tif carry == 1 {\n\t\tres = strconv.Itoa(carry) + res\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0067_add_binary/add_binary_test.go",
    "content": "package addbinary\n\nimport (\n\t\"testing\"\n)\n\nfunc TestAddBinary(t *testing.T) {\n\ttype arg struct {\n\t\ta string\n\t\tb string\n\t}\n\n\ttestCases := []arg{\n\t\t{\n\t\t\ta: \"11\",\n\t\t\tb: \"1\",\n\t\t},\n\t\t{\n\t\t\tb: \"11\",\n\t\t\ta: \"1\",\n\t\t},\n\t}\n\n\texpected := []string{\n\t\t\"100\", \"100\",\n\t}\n\n\tfor index, data := range testCases {\n\t\tif res := addBinary(data.a, data.b); res != expected[index] {\n\t\t\tt.Errorf(\"expected %s, got %s\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0069_sqrtx/sqrtx.go",
    "content": "/*\n69. Sqrt(x)\nhttps://leetcode.com/problems/sqrtx/\n\nImplement int sqrt(int x).\n\nCompute and return the square root of x, where x is guaranteed to be a non-negative integer.\n\nSince the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.\n*/\n\n// time: 2018-12-20\n\npackage sqrtx\n\n// binary search\n// time complexity: O(logn)\n// space complexity: O(1)\nfunc mySqrt(x int) int {\n\tvar (\n\t\tl int\n\t\tr = x // 在0～x范围内寻找平凡根\n\t)\n\n\tfor l <= r {\n\t\tmid := l + (r-l)>>1 //位运算更高效\n\t\tif mid*mid == x {\n\t\t\treturn mid\n\t\t}\n\n\t\tif mid*mid < x {\n\t\t\tif (mid+1)*(mid+1) > x {\n\t\t\t\treturn mid // 返回整数部分\n\t\t\t}\n\t\t\tl = mid + 1\n\t\t} else {\n\t\t\tr = mid - 1\n\t\t}\n\t}\n\treturn x // 不会执行，如果x为负数，会执行到此处，但是不符合题目要求。\n}\n"
  },
  {
    "path": "solutions/0069_sqrtx/sqrtx_test.go",
    "content": "package sqrtx\n\nimport \"testing\"\n\nfunc TestMySqrt(t *testing.T) {\n\ttestCases := []int{66, 99, 9}\n\texpected := []int{8, 9, 3}\n\n\tfor index, data := range testCases {\n\t\tif res := mySqrt(data); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0070_climbing_stairs/climbing_stairs.go",
    "content": "/*\n70. Climbing Stairs\n\nsource:https://leetcode.com/problems/climbing-stairs/\n\nYou are climbing a stair case. It takes n steps to reach to the top.\n\nEach time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?\n\nNote: Given n will be a positive integer.\n\nExample 1:\n\nInput: 2\nOutput: 2\nExplanation: There are two ways to climb to the top.\n1. 1 step + 1 step\n2. 2 steps\nExample 2:\n\nInput: 3\nOutput: 3\nExplanation: There are three ways to climb to the top.\n1. 1 step + 1 step + 1 step\n2. 1 step + 2 steps\n3. 2 steps + 1 step\n*/\n\npackage climbingstairs\n\n// recursion\n/*\nfunc climbStairs(n int) int {\n\t// if n == 1 {\n\t// \treturn 1\n\t// }\n\t// if n == 2 {\n\t// \treturn 2\n\t// }\n\t//\n\n\tif n == 1 || n == 0 {\n\t\treturn 1\n\t}\n\n\treturn climbStairs(n-1) + climbStairs(n-2)\n}\n*/\n\n// memory search\n/*\nfunc climbStairs(n int) int {\n\tvar memo []int\n\tfor i := 0; i <= n; i++ {\n\t\tmemo = append(memo, -1)\n\t}\n\treturn calcWays(n, memo)\n}\n\nfunc calcWays(n int, memo []int) int {\n\tif n == 0 || n == 1 {\n\t\treturn 1\n\t}\n\n\tif memo[n] == -1 {\n\t\tmemo[n] = calcWays(n-1, memo) + calcWays(n-2, memo)\n\t}\n\n\treturn memo[n]\n}\n*/\n\n// dynamic programming\nfunc climbStairs(n int) int {\n\tvar memo = []int{1, 1}\n\tfor i := 2; i <= n; i++ {\n\t\tmemo = append(memo, memo[i-1]+memo[i-2])\n\t}\n\treturn memo[n]\n}\n"
  },
  {
    "path": "solutions/0070_climbing_stairs/climbing_stairs_test.go",
    "content": "package climbingstairs\n\nimport \"testing\"\n\nfunc TestClimbStairs(t *testing.T) {\n\ttestDatas := []int{0, 1, 2, 3, 34}\n\texpected := []int{1, 1, 2, 3, 9227465}\n\n\tfor i, data := range testDatas {\n\t\tif steps := climbStairs(data); steps != expected[i] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[i], steps)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0075_sort_colors/sort_colors.go",
    "content": "/*\n75. Sort Colors\nhttps://leetcode.com/problems/sort-colors/\n\nGiven an array with n objects colored red, white or blue,\nsort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.\n\nHere, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.\n\nNote: You are not suppose to use the library's sort function for this problem.\n*/\n\n// time: 2018-12-21\n\npackage sortcolors\n\n// 计数排序\n// time complexity: O(n)\n// space complexity: O(1)\nfunc sortColorsCountSort(nums []int) {\n\tvar count = [3]int{}\n\n\tfor _, num := range nums {\n\t\tcount[num]++\n\t}\n\n\tindex := 0\n\n\tfor i, j := range count {\n\t\tfor k := 0; k < j; k++ {\n\t\t\tnums[index] = i\n\t\t\tindex++\n\t\t}\n\t}\n}\n\n// quick sort 3 ways\n// time complexity: O(n)\n// space complexity: O(1)\nfunc sortColorsQuickSort3Ways(nums []int) {\n\tvar (\n\t\tzero = -1\n\t\ttwo  = len(nums)\n\t)\n\n\tfor i := 0; i < two; {\n\t\tif 1 == nums[i] {\n\t\t\ti++\n\t\t} else if 2 == nums[i] {\n\t\t\ttwo--\n\t\t\tnums[i], nums[two] = nums[two], nums[i]\n\t\t} else { // nums[i] == 0\n\t\t\tzero++\n\t\t\tnums[i], nums[zero] = nums[zero], nums[i]\n\t\t\ti++\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0075_sort_colors/sort_colors_test.go",
    "content": "package sortcolors\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestSortColorsCountSort(t *testing.T) {\n\tnums := []int{2, 0, 2, 1, 1, 0}\n\texpected := []int{0, 0, 1, 1, 2, 2}\n\n\tsortColorsCountSort(nums)\n\n\tif !reflect.DeepEqual(nums, expected) {\n\t\tt.Errorf(\"expected %v, got %v\", expected, nums)\n\t}\n}\nfunc TestSortColorsQuickSort3Ways(t *testing.T) {\n\tnums := []int{2, 0, 2, 1, 1, 0}\n\texpected := []int{0, 0, 1, 1, 2, 2}\n\n\tsortColorsQuickSort3Ways(nums)\n\n\tif !reflect.DeepEqual(nums, expected) {\n\t\tt.Errorf(\"expected %v, got %v\", expected, nums)\n\t}\n}\n"
  },
  {
    "path": "solutions/0076_minimum_window_substring/minimum_window_substring.go",
    "content": "/*\n76. Minimum Window Substring\nhttps://leetcode.com/problems/minimum-window-substring/\n\nGiven a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).\n\nExample:\n\tInput: S = \"ADOBECODEBANC\", T = \"ABC\"\n\tOutput: \"BANC\"\n\nNote:\n\t1. If there is no such window in S that covers all characters in T, return the empty string \"\".\n\t2. If there is such window, you are guaranteed that there will always be only one unique minimum window in S.\n*/\n\npackage minimumwindowsubstring\n\nimport \"leetcode/utils\"\n\n/*\n使用滑动窗口解决这一问题，使用map或者slice统计T字符串中的字母的个数，之后，开始遍历S字符串，对于S中遍历到\n的每一个字母，都在map或者slice中的对应个数减一，如果减一后仍然大于等于0，说明当前遍历到的字母是T中的字母，\n使用计数器count，使其加一，当count和T串字母个数相等时，说明此时的窗口已经包含了T串中的所有字母，此时记录\n一下字串和字串长度，\n然后开始收缩左边界，由于我们遍历的时候，对应值减了1，所以此时去除字母的时候，就要把减去的1加回来，\n此时如果加1后的值大于0了，说明此时我们少了一个T中的字母，那么count值就要减1了，然后移动左边界left。\n*/\n\n// sliding window\n// Time complexity: O(n)\n// Space complexity: O(128) = O(1)\nfunc minWindow(s string, t string) string {\n\tvar (\n\t\tres         string\n\t\tletterCount = make([]int, 128)\n\t\tleft        int\n\t\tcount       int\n\t\tminLen      = utils.MaxInt\n\t)\n\n\tfor i := 0; i < len(t); i++ {\n\t\tletterCount[t[i]]++\n\t}\n\n\tfor i := 0; i < len(s); i++ {\n\t\tif letterCount[s[i]]--; letterCount[s[i]] >= 0 {\n\t\t\tcount++\n\t\t}\n\n\t\tfor count == len(t) {\n\t\t\tif minLen > i-left+1 {\n\t\t\t\tminLen = i - left + 1\n\t\t\t\tres = s[left : minLen+left]\n\t\t\t}\n\t\t\tif letterCount[s[left]]++; letterCount[s[left]] > 0 {\n\t\t\t\tcount--\n\t\t\t}\n\t\t\tleft++\n\t\t}\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0076_minimum_window_substring/minimum_window_substring_test.go",
    "content": "package minimumwindowsubstring\n\nimport \"testing\"\n\nfunc TestMinWindow(t *testing.T) {\n\ts := \"ADOBECODEBANC\"\n\tl := \"ABC\"\n\texpected := \"BANC\"\n\n\tif res := minWindow(s, l); res != expected {\n\t\tt.Errorf(\"expected %s, got %s\", expected, res)\n\t}\n\n}\n"
  },
  {
    "path": "solutions/0077_combinations/combinations.go",
    "content": "/*\n77. Combinations\nhttps://leetcode.com/problems/combinations/\n\nGiven two integers n and k, return all possible combinations of k numbers out of 1 ... n.\n*/\n// time: 2019-01-04\n\npackage combinations\n\n// backtracking\n// time complexity: O(n^k)\n// space complexity: O(k)\nfunc combine(n int, k int) [][]int {\n\tif n <= 0 || k <= 0 || k > n {\n\t\treturn [][]int{}\n\t}\n\tres := make([][]int, 0)\n\tc := make([]int, 0, k)\n\tgenerateCombinations(n, k, 1, c, &res)\n\treturn res\n}\n\n// 求解C(n,k), 当前已经找到的组合存储在c中，需要从start开始搜索新元素。\nfunc generateCombinations(n, k, start int, c []int, res *[][]int) {\n\tif len(c) == k {\n\t\tcpy := make([]int, k)\n\t\tcopy(cpy, c)\n\t\t*res = append(*res, cpy)\n\t\treturn\n\t}\n\t// 回朔法剪枝。\n\t// 还有k - len(c)个空位，所以， [i...n]中至少要有k - len(c)个元素\n\t// i最多为n-(k-len(c))+1\n\tfor i := start; i <= n-(k-len(c))+1; i++ {\n\t\tc = append(c, i)\n\t\tgenerateCombinations(n, k, i+1, c, res)\n\t\tc = c[:len(c)-1]\n\t}\n}\n"
  },
  {
    "path": "solutions/0077_combinations/combinations_test.go",
    "content": "package combinations\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestCombine(t *testing.T) {\n\ttype arg struct {\n\t\tn, k int\n\t}\n\n\ttestCases := []arg{\n\t\t{n: 0, k: 2},\n\t\t{n: 2, k: 0},\n\t\t{n: 1, k: 5},\n\t\t{n: 4, k: 2},\n\t}\n\texpected := [][][]int{\n\t\t{},\n\t\t{},\n\t\t{},\n\t\t{{1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}},\n\t}\n\n\tfor index, data := range testCases {\n\t\tif res := combine(data.n, data.k); !reflect.DeepEqual(res, expected[index]) {\n\t\t\tt.Errorf(\"expected %v, got %v\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0079_word_search/word_search.go",
    "content": "/*\n79. Word Search\nhttps://leetcode.com/problems/word-search/\n\nGiven a 2D board and a word, find if the word exists in the grid.\nThe word can be constructed from letters of sequentially adjacent cell,\nwhere \"adjacent\" cells are those horizontally or vertically neighboring.\nThe same letter cell may not be used more than once.\n*/\n// 2019-01-04\n\npackage wordsearch\n\nvar (\n\td       = [4][2]int{{-1, 0}, {0, 1}, {1, 0}, {0, -1}}\n\tm, n    int\n\tvisited [][]bool\n)\n\n// backtracking\n// time complexity: O(m*n*m*n)\n// space complexity: O(m*n)\nfunc exist(board [][]byte, word string) bool {\n\tm = len(board)\n\tn = len(board[0])\n\tvisited = make([][]bool, m)\n\tfor i := 0; i < m; i++ {\n\t\tvisited[i] = make([]bool, n)\n\t}\n\tfor i := 0; i < len(board); i++ {\n\t\tfor j := 0; j < len(board[i]); j++ {\n\t\t\tif searchWord(board, word, 0, i, j) {\n\t\t\t\treturn true\n\t\t\t}\n\t\t}\n\t}\n\treturn false\n}\n\nfunc searchWord(board [][]byte, word string, index, startX, startY int) bool {\n\tif len(word)-1 == index {\n\t\treturn board[startX][startY] == word[index]\n\t}\n\tif board[startX][startY] == word[index] {\n\t\tvisited[startX][startY] = true\n\t\t// 从startX， startY开始，向四个方向寻找\n\t\tfor i := 0; i < 4; i++ {\n\t\t\tnewX := startX + d[i][0]\n\t\t\tnewY := startY + d[i][1]\n\t\t\tif inArea(newX, newY) && !visited[newX][newY] && searchWord(board, word, index+1, newX, newY) {\n\t\t\t\treturn true\n\t\t\t}\n\t\t}\n\t\tvisited[startX][startY] = false\n\t}\n\treturn false\n}\n\nfunc inArea(x, y int) bool {\n\treturn x >= 0 && x < m && y >= 0 && y < n\n}\n"
  },
  {
    "path": "solutions/0079_word_search/word_search_test.go",
    "content": "package wordsearch\n\nimport \"testing\"\n\nfunc TestExist(t *testing.T) {\n\tboard := [][]byte{\n\t\t{'A', 'B', 'C', 'E'},\n\t\t{'S', 'F', 'C', 'S'},\n\t\t{'A', 'D', 'E', 'E'},\n\t}\n\ttestCases := []string{\n\t\t\"ABCCED\",\n\t\t\"SEE\",\n\t\t\"ABCB\",\n\t}\n\texpected := []bool{true, true, false}\n\tfor index, word := range testCases {\n\t\tif res := exist(board, word); res != expected[index] {\n\t\t\tt.Errorf(\"expected %t, got %t\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0080_remove_duplicates_from_sorted_array2/rdfsa2.go",
    "content": "/*\n80. Remove Duplicates from Sorted Array II\nhttps://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/\n\nGiven a sorted array nums, remove the duplicates in-place such that\nduplicates appeared at most twice and return the new length.\n\nDo not allocate extra space for another array, you must do this\n by modifying the input array in-place with O(1) extra memory.\n*/\n// time: 2018-12-21\n\npackage rdfsa2\n\n// double index\n// time complexity: O(n)\n// space complexity: O(1)\nfunc removeDuplicates(nums []int) int {\n\tn := len(nums)\n\tif 0 == n || 1 == n {\n\t\treturn n\n\t}\n\n\tvar (\n\t\tres   = 2\n\t\ti     = 2\n\t\tindex = nextDifferentCharacterIndex(nums, i, 2) // 寻找下一个满足条件的下标\n\t)\n\n\tfor index < n {\n\t\tres++\n\t\tnums[i] = nums[index]\n\t\ti++\n\t\tindex = nextDifferentCharacterIndex(nums, i, index+1)\n\t}\n\treturn res\n}\n\nfunc nextDifferentCharacterIndex(nums []int, j int, p int) int {\n\tfor ; p < len(nums); p++ {\n\t\tif nums[j-1] == nums[j-2] && nums[p] != nums[j-1] || nums[j-1] != nums[j-2] {\n\t\t\tbreak\n\t\t}\n\t}\n\treturn p\n}\n"
  },
  {
    "path": "solutions/0080_remove_duplicates_from_sorted_array2/rdfsa2_test.go",
    "content": "package rdfsa2\n\nimport \"testing\"\n\nfunc TestRemoveDuplicates(t *testing.T) {\n\ttestCases := [][]int{\n\t\t{1, 1, 1, 2, 2, 3},\n\t\t{1, 2, 2},\n\t\t{0, 0, 1, 1, 1, 1, 2, 3, 3},\n\t\t{},\n\t\t{1},\n\t\t{2, 2},\n\t\t{2, 2, 2},\n\t}\n\texpected := []int{5, 3, 7, 0, 1, 2, 2}\n\n\tfor index, data := range testCases {\n\t\tif res := removeDuplicates(data); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0082_remove_duplicates_from_sorted_list_2/rdfsl.go",
    "content": "/*\n82. Remove Duplicates from Sorted List II\nhttps://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/\n\nGiven a sorted linked list, delete all nodes that have duplicate numbers,\nleaving only distinct numbers from the original list.\n*/\n// time: 2019-01-04\n\npackage rdfsl\n\n// ListNode Definition for singly-linked list.\ntype ListNode struct {\n\tVal  int\n\tNext *ListNode\n}\n\n// time complexity: O(n)\n// space complexity: O(1)\nfunc deleteDuplicates(head *ListNode) *ListNode {\n\tdummyHead := &ListNode{}\n\tdummyHead.Next = head\n\n\tpre := dummyHead\n\tcur := head\n\n\tfor cur != nil && cur.Next != nil {\n\t\tif cur.Val == cur.Next.Val {\n\t\t\tnum := cur.Val\n\t\t\tfor cur != nil && cur.Val == num {\n\t\t\t\tcur = cur.Next\n\t\t\t}\n\t\t\tpre.Next = cur\n\t\t} else {\n\t\t\tpre = cur\n\t\t\tcur = cur.Next\n\t\t}\n\t}\n\treturn dummyHead.Next\n}\n"
  },
  {
    "path": "solutions/0082_remove_duplicates_from_sorted_list_2/rdfsl_test.go",
    "content": "package rdfsl\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestDeleteDuplicates(t *testing.T) {\n\ttestCases := []*ListNode{\n\t\tcreateSingleLinkedList([]int{1, 2, 3, 3, 4, 4, 5}),\n\t\tcreateSingleLinkedList([]int{1, 1, 1, 2, 3}),\n\t}\n\n\texpected := []*ListNode{\n\t\tcreateSingleLinkedList([]int{1, 2, 5}),\n\t\tcreateSingleLinkedList([]int{2, 3}),\n\t}\n\n\tfor index, head := range testCases {\n\t\tif res := deleteDuplicates(head); !reflect.DeepEqual(res, expected[index]) {\n\t\t\tt.Errorf(\"expected %v, got %v\", expected[index], res)\n\t\t}\n\t}\n}\n\nfunc createSingleLinkedList(nums []int) *ListNode {\n\thead := &ListNode{}\n\tcur := head\n\tfor _, num := range nums {\n\t\tcur.Next = &ListNode{Val: num}\n\t\tcur = cur.Next\n\t}\n\treturn head.Next\n}\n"
  },
  {
    "path": "solutions/0083_remove_duplicates_from_sorted_list/rdfsl.go",
    "content": "/*\n83. Remove Duplicates from Sorted List\nhttps://leetcode.com/problems/remove-duplicates-from-sorted-list/\n\nGiven a sorted linked list, delete all duplicates such that each element appear only once.\n*/\n\npackage rdfsl\n\n// ListNode Definition for singly-linked list.\ntype ListNode struct {\n\tVal  int\n\tNext *ListNode\n}\n\n// time complexity: O(n)\n// space complexity: O(1)\nfunc deleteDuplicates(head *ListNode) *ListNode {\n\tif head == nil || head.Next == nil {\n\t\treturn head\n\t}\n\n\tvar (\n\t\tpre = head\n\t\tcur = head.Next\n\t)\n\n\tfor cur != nil {\n\t\tif cur.Val == pre.Val {\n\t\t\tpre.Next = cur.Next\n\t\t\tcur = cur.Next\n\t\t} else {\n\t\t\tpre = cur\n\t\t\tcur = cur.Next\n\t\t}\n\t}\n\treturn head\n}\n"
  },
  {
    "path": "solutions/0083_remove_duplicates_from_sorted_list/rdfsl_test.go",
    "content": "package rdfsl\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc createSingleLinkedList(nums []int) *ListNode {\n\thead := &ListNode{}\n\tcur := head\n\n\tfor _, num := range nums {\n\t\tcur.Next = &ListNode{Val: num}\n\t\tcur = cur.Next\n\t}\n\treturn head.Next\n}\n\nfunc TestDeleteDuplicates(t *testing.T) {\n\ttestCases := []*ListNode{\n\t\tcreateSingleLinkedList([]int{1, 1, 2}),\n\t\tcreateSingleLinkedList([]int{1, 1, 2, 3, 3}),\n\t\tnil,\n\t}\n\texpected := []*ListNode{\n\t\tcreateSingleLinkedList([]int{1, 2}),\n\t\tcreateSingleLinkedList([]int{1, 2, 3}),\n\t\tnil,\n\t}\n\tfor index, head := range testCases {\n\t\tif res := deleteDuplicates(head); !reflect.DeepEqual(res, expected[index]) {\n\t\t\tt.Errorf(\"expected %v, got %v\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0086_partition_list/partition_list.go",
    "content": "/*\n86. Partition List\nhttps://leetcode.com/problems/partition-list/\n\nGiven a linked list and a value x,\npartition it such that all nodes less than x come before nodes greater than or equal to x.\n\nYou should preserve the original relative order of the nodes in each of the two partitions.\n*/\n// time: 2019-01-04\n\npackage partitionlist\n\n// ListNode Definition for singly-linked list.\ntype ListNode struct {\n\tVal  int\n\tNext *ListNode\n}\n\n// time complexity: O(n)\n// space complexity: O(1)\nfunc partition(head *ListNode, x int) *ListNode {\n\tvar (\n\t\tdummyHead1 = &ListNode{}\n\t\tdummyHead2 = &ListNode{}\n\t\tcur1       = dummyHead1\n\t\tcur2       = dummyHead2\n\t)\n\n\tfor cur := head; cur != nil; {\n\t\tif cur.Val < x {\n\t\t\tcur1.Next = cur\n\t\t\tcur1 = cur1.Next\n\t\t\tcur = cur.Next\n\t\t\tcur1.Next = nil\n\t\t} else {\n\t\t\tcur2.Next = cur\n\t\t\tcur2 = cur2.Next\n\t\t\tcur = cur.Next\n\t\t\tcur2.Next = nil\n\t\t}\n\t}\n\tcur1.Next = dummyHead2.Next\n\treturn dummyHead1.Next\n}\n"
  },
  {
    "path": "solutions/0086_partition_list/partition_list_test.go",
    "content": "package partitionlist\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc createSingleLinkedList(nums []int) *ListNode {\n\thead := &ListNode{}\n\tcur := head\n\n\tfor _, num := range nums {\n\t\tcur.Next = &ListNode{Val: num}\n\t\tcur = cur.Next\n\t}\n\treturn head.Next\n}\n\nfunc TestDeleteDuplicates(t *testing.T) {\n\ttestCase := createSingleLinkedList([]int{1, 4, 3, 2, 5, 2})\n\n\texpected := createSingleLinkedList([]int{1, 2, 2, 4, 3, 5})\n\tx := 3\n\tif res := partition(testCase, x); !reflect.DeepEqual(res, expected) {\n\t\tt.Errorf(\"expected %v, got %v\", expected, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0088_merge_sorted_array/msa.go",
    "content": "/*\n88. Merge Sorted Array\nhttps://leetcode.com/problems/merge-sorted-array/\n\nGiven two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.\n\nNote:\n\t1. The number of elements initialized in nums1 and nums2 are m and n respectively.\n\t2. You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2.\n*/\n\n// time: 2018-12-21\n\npackage msa\n\n// standard merge process in merge sort\n// time complexity: O(n+m)\n// space complexity: O(1)\nfunc merge(nums1 []int, m int, nums2 []int, n int) {\n\tfor i := n + m - 1; i >= n; i-- {\n\t\tnums1[i] = nums1[i-n]\n\t}\n\n\tvar (\n\t\ti = n // pointer for nums1 [n, n+m)\n\t\tj = 0 // pointer for nums2 [0, n)\n\t\tk = 0 // pointer for merged nums1 [0, n+m)\n\t)\n\n\tfor k < n+m {\n\t\tif i >= n+m {\n\t\t\tnums1[k] = nums2[j]\n\t\t\tk++\n\t\t\tj++\n\t\t} else if j >= n {\n\t\t\tbreak\n\t\t\t// nums1[k] = nums1[i]\n\t\t\t// k++\n\t\t\t// i++\n\t\t} else if nums1[i] < nums2[j] {\n\t\t\tnums1[k] = nums1[i]\n\t\t\ti++\n\t\t\tk++\n\t\t} else { //nums1[i] >= nums2[j]\n\t\t\tnums1[k] = nums2[j]\n\t\t\tk++\n\t\t\tj++\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0088_merge_sorted_array/msa_test.go",
    "content": "package msa\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestMerge(t *testing.T) {\n\ttype arg struct {\n\t\tnums1 []int\n\t\tm     int\n\t\tnums2 []int\n\t\tn     int\n\t}\n\ttestCases := []arg{\n\t\t{nums1: []int{1, 2, 3, 7, 0, 0, 0}, m: 4, nums2: []int{2, 5, 6}, n: 3},\n\t\t{nums1: []int{2, 5, 6, 0, 0, 0}, m: 3, nums2: []int{7, 8, 9}, n: 3},\n\t}\n\n\texpected := [][]int{{1, 2, 2, 3, 5, 6, 7}, {2, 5, 6, 7, 8, 9}}\n\n\tfor index, data := range testCases {\n\t\tif merge(data.nums1, data.m, data.nums2, data.n); !reflect.DeepEqual(expected[index], data.nums1) {\n\t\t\tt.Errorf(\"expected %v, got %v\", expected[index], data.nums1)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0092_reverse_linked_list_2/reverse_linked_list2.go",
    "content": "/*\n92. Reverse Linked List II\nhttps://leetcode.com/problems/reverse-linked-list-ii/\n\nReverse a linked list from position m to n. Do it in one-pass.\nNote: 1 ≤ m ≤ n ≤ length of list.\n*/\n// time: 2019-01-04\n\npackage reverselinkedlist2\n\n// ListNode Definition for singly-linked list.\ntype ListNode struct {\n\tVal  int\n\tNext *ListNode\n}\n\n// time complexity: O(n-m)\n// space complexity: O(1)\nfunc reverseBetween(head *ListNode, m, n int) *ListNode {\n\tvar (\n\t\tdummy = &ListNode{}\n\t\tp     = dummy\n\t\ts     = p\n\t)\n\tdummy.Next = head\n\n\t// 1 ≤ m ≤ n ≤ length of list.\n\tfor s != nil && n > 0 {\n\t\ts = s.Next\n\t\tn--\n\t}\n\tfor s != nil && m > 1 {\n\t\tp = p.Next\n\t\tm--\n\t}\n\treverse(&p, &s)\n\treturn dummy.Next\n}\n\nfunc reverse(p, s **ListNode) {\n\tvar (\n\t\ttmp  *ListNode\n\t\tprev = (*p).Next\n\t\ttail = (*p).Next\n\t\tflag = (*s).Next\n\t)\n\t(*p).Next = nil\n\tfor prev != flag {\n\t\ttmp = (*p).Next\n\t\t(*p).Next = prev\n\t\tprev = prev.Next\n\t\t(*p).Next.Next = tmp\n\t}\n\ttail.Next = prev\n\t/*\n\t\t1->2->3->4->5\n\t\t1->3->2->4->5\n\t\t1->4->3->2->5\n\t\t1->5->4->3->2\n\t*/\n}\n"
  },
  {
    "path": "solutions/0092_reverse_linked_list_2/reverse_linked_list2_test.go",
    "content": "package reverselinkedlist2\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc createSingleList(nums []int) *ListNode {\n\thead := &ListNode{}\n\tcur := head\n\tfor _, num := range nums {\n\t\tcur.Next = &ListNode{Val: num}\n\t\tcur = cur.Next\n\t}\n\treturn head.Next\n}\n\nfunc TestReverseBetween(t *testing.T) {\n\thead := createSingleList([]int{1, 2, 3, 4, 5})\n\tm := 2\n\tn := 4\n\texpected := createSingleList([]int{1, 4, 3, 2, 5})\n\n\tif res := reverseBetween(head, m, n); !reflect.DeepEqual(res, expected) {\n\t\tt.Errorf(\"expected %v, got %v\", expected, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0094_binary_tree_inorder_traversal/binary_tree_inorder_traversal.go",
    "content": "/*\n94. Binary Tree Inorder Traversal\nsource: https://leetcode.com/problems/binary-tree-inorder-traversal/\n\nGiven a binary tree, return the inorder traversal of its nodes' values.\n\nExample:\n\nInput: [1,null,2,3]\n   1\n    \\\n     2\n    /\n   3\n\nOutput: [1,3,2]\nFollow up: Recursive solution is trivial, could you do it iteratively?\n*/\n\npackage binarytreeinordertraversal\n\n// TreeNode binary tree node.\ntype TreeNode struct {\n\tVal   int\n\tLeft  *TreeNode\n\tRight *TreeNode\n}\n\nfunc inorderTraversal(root *TreeNode) []int {\n\tres := make([]int, 0, 1)\n\tinorderTraversalHelp(root, &res)\n\treturn res\n}\n\nfunc inorderTraversalHelp(root *TreeNode, res *[]int) {\n\tif root == nil {\n\t\treturn\n\t}\n\tinorderTraversalHelp(root.Left, res)\n\t*res = append(*res, root.Val)\n\tinorderTraversalHelp(root.Right, res)\n}\n"
  },
  {
    "path": "solutions/0094_binary_tree_inorder_traversal/binary_tree_inorder_traversal_test.go",
    "content": "package binarytreeinordertraversal\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc createBinaryTree(nums []int) *TreeNode {\n\treturn performCreate(nums, 0)\n}\n\nfunc performCreate(nums []int, index int) *TreeNode {\n\tif index >= len(nums) {\n\t\treturn nil\n\t}\n\n\ttree := TreeNode{Val: nums[index]}\n\ttree.Left = performCreate(nums, 2*index+1)\n\ttree.Right = performCreate(nums, 2*index+2)\n\treturn &tree\n}\n\nfunc TestInorderTraversal(t *testing.T) {\n\ttestData := [][]int{\n\t\t{1, 2, 3},\n\t}\n\texpectedData := [][]int{\n\t\t{2, 1, 3},\n\t}\n\n\tfor index, data := range testData {\n\t\tif res := inorderTraversal(createBinaryTree(data)); !reflect.DeepEqual(res, expectedData[index]) {\n\t\t\tt.Errorf(\"expected %v, got %v\", expectedData[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0100_same_tree/same_tree.go",
    "content": "/*\n100. Same Tree\nhttps://leetcode.com/problems/same-tree/\n\nGiven two binary trees, write a function to check if they are the same or not.\n\nTwo binary trees are considered the same if they are structurally identical and the nodes have the same value.\n\n*/\n\npackage sametree\n\n// TreeNode binary tree node.\ntype TreeNode struct {\n\tVal   int\n\tLeft  *TreeNode\n\tRight *TreeNode\n}\n\nfunc isSameTree(p *TreeNode, q *TreeNode) bool {\n\tif p == nil {\n\t\treturn q == nil\n\t}\n\tif q == nil {\n\t\treturn p == nil\n\t}\n\tif p.Val == q.Val {\n\t\treturn isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)\n\t}\n\treturn false\n}\n"
  },
  {
    "path": "solutions/0100_same_tree/same_tree_test.go",
    "content": "package sametree\n\nimport \"testing\"\n\ntype arg struct {\n\tp []int\n\tq []int\n}\n\nfunc createBinaryTree(nums []int) *TreeNode {\n\treturn performCreate(nums, 0)\n}\n\nfunc performCreate(nums []int, index int) *TreeNode {\n\tif index >= len(nums) {\n\t\treturn nil\n\t}\n\n\ttree := TreeNode{Val: nums[index]}\n\ttree.Left = performCreate(nums, 2*index+1)\n\ttree.Right = performCreate(nums, 2*index+2)\n\treturn &tree\n}\n\nfunc TestSameTree(t *testing.T) {\n\ttestData := []arg{\n\t\t{p: []int{1, 2, 3}, q: []int{1, 2, 3}},\n\t\t{p: []int{1, 2, 3}, q: []int{1, 2}},\n\t\t{p: []int{1, 2, 3}, q: []int{1, 2, 4}},\n\t}\n\n\texpectedData := []bool{true, false, false}\n\n\tfor index, data := range testData {\n\t\tif res := isSameTree(createBinaryTree(data.p), createBinaryTree(data.q)); res != expectedData[index] {\n\t\t\tt.Errorf(\"expected %t, got %t\", expectedData[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0101_symmetric_tree/symmetric_tree.go",
    "content": "/*\n101. Symmetric Tree\nhttps://leetcode.com/problems/symmetric-tree/\n\nGiven a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).\n*/\n\npackage symmetrictree\n\n// TreeNode binary tree node.\ntype TreeNode struct {\n\tVal   int\n\tLeft  *TreeNode\n\tRight *TreeNode\n}\n\nfunc isSymmetric(root *TreeNode) bool {\n\tif root == nil {\n\t\treturn true\n\t}\n\treturn symmetric(root.Left, root.Right)\n}\n\nfunc symmetric(left *TreeNode, right *TreeNode) bool {\n\tif left == nil && right == nil {\n\t\treturn true\n\t}\n\n\tif left == nil || right == nil {\n\t\treturn false\n\t}\n\treturn left.Val == right.Val && symmetric(left.Left, right.Right) && symmetric(left.Right, right.Left)\n}\n"
  },
  {
    "path": "solutions/0101_symmetric_tree/symmetric_tree_test.go",
    "content": "package symmetrictree\n\nimport (\n\t\"testing\"\n)\n\nfunc createBinaryTree(nums []int) *TreeNode {\n\treturn performCreate(nums, 0)\n}\n\nfunc performCreate(nums []int, index int) *TreeNode {\n\tif index >= len(nums) {\n\t\treturn nil\n\t}\n\n\ttree := TreeNode{Val: nums[index]}\n\ttree.Left = performCreate(nums, 2*index+1)\n\ttree.Right = performCreate(nums, 2*index+2)\n\treturn &tree\n}\n\nfunc TestIsSymmetric(t *testing.T) {\n\ttestData := [][]int{\n\t\t{1, 2, 2, 3, 4, 4, 3},\n\t\t{1, 2, 2, 3, 3},\n\t\t{},\n\t}\n\texpectedData := []bool{true, false, true}\n\n\tfor index, data := range testData {\n\t\tif res := isSymmetric(createBinaryTree(data)); res != expectedData[index] {\n\t\t\tt.Errorf(\"expected %t, got %t\", expectedData[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0102_binary_tree_level_order_traversal/binary_tree_level_order_traversal.go",
    "content": "/*\n102. Binary Tree Level Order Traversal\nhttps://leetcode.com/problems/binary-tree-level-order-traversal/\n\nGiven a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).\n*/\n// time: 2019-01-04\n\npackage btlot\n\n// TreeNode Definition for a binary tree node.\ntype TreeNode struct {\n\tVal   int\n\tLeft  *TreeNode\n\tRight *TreeNode\n}\n\ntype queueEle struct {\n\tLevel int\n\tEle   *TreeNode\n}\n\n// bfs\n// time complexity: O(n), where n is number of tree nodes.\n// space complexity: O(n)\nfunc levelOrder(root *TreeNode) [][]int {\n\tif root == nil {\n\t\treturn [][]int{}\n\t}\n\n\tvar (\n\t\tqueue []queueEle\n\t\tres   = make([][]int, 0)\n\t)\n\n\tqueue = append(queue, queueEle{Level: 0, Ele: root})\n\n\tfor len(queue) > 0 {\n\t\tnode := queue[0].Ele\n\t\tlevel := queue[0].Level\n\t\tqueue = queue[1:]\n\n\t\tif len(res) > level {\n\t\t\tres[level] = append(res[level], node.Val)\n\t\t} else {\n\t\t\tres = append(res, []int{node.Val})\n\t\t}\n\n\t\tif node.Left != nil {\n\t\t\tqueue = append(queue, queueEle{Level: level + 1, Ele: node.Left})\n\t\t}\n\t\tif node.Right != nil {\n\t\t\tqueue = append(queue, queueEle{Level: level + 1, Ele: node.Right})\n\t\t}\n\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0102_binary_tree_level_order_traversal/binary_tree_level_order_traversal_test.go",
    "content": "package btlot\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestLevelOrder(t *testing.T) {\n\ttestCases := []*TreeNode{\n\t\tcreateBinaryTree([]int{1, 2, 3, 4, 5}),\n\t\tnil,\n\t}\n\texpected := [][][]int{\n\t\t{{1}, {2, 3}, {4, 5}},\n\t\t{},\n\t}\n\n\tfor index, root := range testCases {\n\t\tif res := levelOrder(root); !reflect.DeepEqual(res, expected[index]) {\n\t\t\tt.Errorf(\"expected %v, got %v\", expected[index], res)\n\t\t}\n\t}\n}\n\nfunc createBinaryTree(nums []int) *TreeNode {\n\treturn performCreate(nums, 0)\n}\n\nfunc performCreate(nums []int, index int) *TreeNode {\n\tif index >= len(nums) {\n\t\treturn nil\n\t}\n\ttree := TreeNode{Val: nums[index]}\n\ttree.Left = performCreate(nums, 2*index+1)\n\ttree.Right = performCreate(nums, 2*index+2)\n\treturn &tree\n}\n"
  },
  {
    "path": "solutions/0104_maximun_depth_of_binary_tree/maxdobt.go",
    "content": "/*\n104. Maximum Depth of Binary Tree\nhttps://leetcode.com/problems/maximum-depth-of-binary-tree/\n\nGiven a binary tree, find its maximum depth.\n\nThe maximum depth is the number of nodes along\nthe longest path from the root node down to the farthest leaf node.\n\nNote: A leaf is a node with no children.\n*/\n// time: 2019-01-06\n\npackage maxdobt\n\n// TreeNode Definition for a binary tree node.\ntype TreeNode struct {\n\tVal   int\n\tLeft  *TreeNode\n\tRight *TreeNode\n}\n\n// Recursive\n// time complexity: O(n), where n is the nodes number in the tree.\n// space complexity: O(h), where h is the height of the tree.\nfunc maxDepth(root *TreeNode) int {\n\tif root == nil {\n\t\treturn 0\n\t}\n\tleft := maxDepth(root.Left)\n\tright := maxDepth(root.Right)\n\n\tvar max int\n\tif left > right {\n\t\tmax = left\n\t} else {\n\t\tmax = right\n\t}\n\treturn max + 1\n}\n"
  },
  {
    "path": "solutions/0104_maximun_depth_of_binary_tree/maxdobt_test.go",
    "content": "package maxdobt\n\nimport \"testing\"\n\nfunc TestMaxDepth(t *testing.T) {\n\troot := createBinaryTree([]int{1, 2, 3, 4, 5})\n\texpected := 3\n\n\tif res := maxDepth(root); res != expected {\n\t\tt.Errorf(\"expected %d, got %d\", expected, res)\n\t}\n\n}\n\nfunc createBinaryTree(nums []int) *TreeNode {\n\treturn performCreate(nums, 0)\n}\n\nfunc performCreate(nums []int, index int) *TreeNode {\n\tif index >= len(nums) {\n\t\treturn nil\n\t}\n\ttree := &TreeNode{Val: nums[index]}\n\ttree.Left = performCreate(nums, 2*index+1)\n\ttree.Right = performCreate(nums, 2*index+2)\n\treturn tree\n}\n"
  },
  {
    "path": "solutions/0107_binary_tree_level_order_traversal_2/binary_tree_level_order_traversal2.go",
    "content": "/*\n107. Binary Tree Level Order Traversal II\nhttps://leetcode.com/problems/binary-tree-level-order-traversal-ii/\n\nGiven a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).\n*/\n\npackage binarytreelevelordertraversal2\n\n// TreeNode binary tree node.\ntype TreeNode struct {\n\tVal   int\n\tLeft  *TreeNode\n\tRight *TreeNode\n}\n\nfunc levelOrderBottom(root *TreeNode) [][]int {\n\tvar record [][]int\n\ttraversal(root, 0, &record)\n\tvar reversed [][]int\n\tfor i := len(record) - 1; i >= 0; i-- {\n\t\treversed = append(reversed, record[i])\n\t}\n\treturn reversed\n}\n\nfunc traversal(root *TreeNode, index int, record *[][]int) {\n\tif root == nil {\n\t\treturn\n\t}\n\tif len(*record) == index {\n\t\t*record = append(*record, make([]int, 0))\n\t}\n\t(*record)[index] = append((*record)[index], root.Val)\n\ttraversal(root.Left, index+1, record)\n\ttraversal(root.Right, index+1, record)\n\t// (*record)[len(*record)-1-index] = append((*record)[len(*record)-1-index], root.Val)\n\t// 这个方法用java可以实现，go不行，猜测可能跟java的递归实现有关。\n}\n"
  },
  {
    "path": "solutions/0107_binary_tree_level_order_traversal_2/binary_tree_level_order_traversal2_test.go",
    "content": "package binarytreelevelordertraversal2\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc createBinaryTree(nums []int) *TreeNode {\n\treturn performCreate(nums, 0)\n}\n\nfunc performCreate(nums []int, index int) *TreeNode {\n\tif index >= len(nums) {\n\t\treturn nil\n\t}\n\n\ttree := TreeNode{Val: nums[index]}\n\ttree.Left = performCreate(nums, 2*index+1)\n\ttree.Right = performCreate(nums, 2*index+2)\n\treturn &tree\n}\n\nfunc TestBinaryTreeLevelOrderTraversal2(t *testing.T) {\n\ttestData := [][]int{\n\t\t{3, 9, 20, 15, 17},\n\t}\n\texpectedData := [][][]int{\n\t\t{\n\t\t\t{15, 17},\n\t\t\t{9, 20},\n\t\t\t{3},\n\t\t},\n\t}\n\n\tfor index, data := range testData {\n\t\tif res := levelOrderBottom(createBinaryTree(data)); !reflect.DeepEqual(res, expectedData[index]) {\n\t\t\tt.Errorf(\"expected %v, got %v\", expectedData[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0111_minimum_depth_of_binary_tree/minimum_depth_of_binary_tree.go",
    "content": "/*\n111. Minimum Depth of Binary Tree\nhttps://leetcode.com/problems/minimum-depth-of-binary-tree/\n\nGiven a binary tree, find its minimum depth.\n\nThe minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.\n\nNote: A leaf is a node with no children.\n\n*/\n\npackage minimumdepthofbinarytree\n\nimport \"leetcode/utils\"\n\n// TreeNode binary tree node.\ntype TreeNode struct {\n\tVal   int\n\tLeft  *TreeNode\n\tRight *TreeNode\n}\n\nfunc minDepth(root *TreeNode) int {\n\tif root == nil {\n\t\treturn 0\n\t}\n\tleft := minDepth(root.Left)\n\tright := minDepth(root.Right)\n\n\tif left == 0 || right == 0 {\n\t\treturn utils.CalcMaxInt(left, right) + 1\n\t}\n\treturn utils.CalcMinInt(left, right) + 1\n}\n"
  },
  {
    "path": "solutions/0111_minimum_depth_of_binary_tree/minimum_depth_of_binary_tree_test.go",
    "content": "package minimumdepthofbinarytree\n\nimport \"testing\"\n\nfunc createBinaryTree(nums []int) *TreeNode {\n\treturn performCreate(nums, 0)\n}\n\nfunc performCreate(nums []int, index int) *TreeNode {\n\tif index >= len(nums) {\n\t\treturn nil\n\t}\n\n\ttree := TreeNode{Val: nums[index]}\n\ttree.Left = performCreate(nums, 2*index+1)\n\ttree.Right = performCreate(nums, 2*index+2)\n\treturn &tree\n}\nfunc TestMinDepth(t *testing.T) {\n\ttestData := [][]int{\n\t\t{3, 9, 20, 15, 7},\n\t}\n\n\texpectedData := []int{2}\n\n\tfor index, data := range testData {\n\t\tif res := minDepth(createBinaryTree(data)); res != expectedData[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expectedData[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0112_path_sum/path_sum.go",
    "content": "/*\n112. Path Sum\nhttps://leetcode.com/problems/path-sum/\n\nGiven a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.\n\nNote: A leaf is a node with no children.\n*/\n\npackage pathsum\n\n// TreeNode binary tree node.\ntype TreeNode struct {\n\tVal   int\n\tLeft  *TreeNode\n\tRight *TreeNode\n}\n\nfunc hasPathSum(root *TreeNode, sum int) bool {\n\tif root == nil {\n\t\treturn false\n\t}\n\n\tif root.Left == nil && root.Right == nil {\n\t\treturn root.Val == sum\n\t}\n\treturn hasPathSum(root.Left, sum-root.Val) || hasPathSum(root.Right, sum-root.Val)\n}\n"
  },
  {
    "path": "solutions/0112_path_sum/path_sum_test.go",
    "content": "package pathsum\n\nimport (\n\t\"os\"\n\t\"testing\"\n)\n\nfunc createBinaryTree(nums []interface{}) *TreeNode {\n\treturn performCreate(nums, 0)\n}\n\nfunc performCreate(nums []interface{}, index int) *TreeNode {\n\tif !(index < len(nums)) || nums[index] == nil {\n\t\treturn nil\n\t}\n\n\ttree := TreeNode{}\n\tif num, ok := nums[index].(int); ok { // type assertion\n\t\ttree.Val = num\n\t}\n\ttree.Left = performCreate(nums, 2*index+1)\n\ttree.Right = performCreate(nums, 2*index+2)\n\treturn &tree\n}\n\ntype arg struct {\n\tnums []interface{}\n\tsum  int\n}\n\nfunc TestHasPathSum(t *testing.T) {\n\ttestData := []arg{\n\t\t{nums: []interface{}{5, 4, 8, 11, nil, 13, 4, 7, 2, nil, nil, nil, 1}, sum: 22},\n\t\t{nums: []interface{}{}, sum: 0},\n\t}\n\texpectedData := []bool{true, false}\n\tfor index, data := range testData {\n\t\tif res := hasPathSum(createBinaryTree(data.nums), data.sum); res != expectedData[index] {\n\t\t\tt.Errorf(\"expected %t, got %t\", expectedData[index], res)\n\t\t}\n\t}\n}\n\nfunc TestMain(m *testing.M) {\n\tos.Exit(m.Run())\n}\n"
  },
  {
    "path": "solutions/0120_triangle/triangle.go",
    "content": "/*\n120. Triangle\nhttps://leetcode.com/problems/triangle/\n\nGiven a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.\n\n*/\n\npackage triangle\n\nimport \"leetcode/utils\"\n\n// dfs\n/*\nfunc minimumTotal(triangle [][]int) int {\n\tif len(triangle) == 0 {\n\t\treturn 0\n\t}\n\tresult := utils.MaxInt\n\tdfs(0, 0, 0, triangle, &result)\n\treturn result\n}\n\nfunc dfs(x int, y int, sum int, triangle [][]int, result *int) {\n\tif len(triangle) == x {\n\t\tif sum < *result {\n\t\t\t*result = sum\n\t\t}\n\t\treturn\n\t}\n\tsum += triangle[x][y]\n\n\tdfs(x+1, y, sum, triangle, result)\n\tdfs(x+1, y+1, sum, triangle, result)\n}\n*/\n\n// dynamic programming\nfunc minimumTotal(triangle [][]int) int {\n\tm := len(triangle)\n\tif m == 0 {\n\t\treturn 0\n\t}\n\n\tdp := make([][]int, m)\n\n\tdp[0] = append(dp[0], triangle[0][0])\n\n\tfor i := 1; i < m; i++ {\n\t\tfor j, num := range triangle[i] {\n\t\t\tif j >= len(dp[i-1]) {\n\t\t\t\tdp[i] = append(dp[i], num+dp[i-1][j-1])\n\t\t\t} else {\n\t\t\t\tif j-1 >= 0 && dp[i-1][j-1] < dp[i-1][j] {\n\t\t\t\t\tdp[i] = append(dp[i], num+dp[i-1][j-1])\n\t\t\t\t} else {\n\t\t\t\t\tdp[i] = append(dp[i], num+dp[i-1][j])\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\tvar mininum = utils.MaxInt\n\tfor _, num := range dp[m-1] {\n\t\tif num < mininum {\n\t\t\tmininum = num\n\t\t}\n\t}\n\treturn mininum\n}\n"
  },
  {
    "path": "solutions/0120_triangle/triangle_test.go",
    "content": "package triangle\n\nimport (\n\t\"testing\"\n)\n\nfunc TestTriangle(t *testing.T) {\n\ttestData := [][][]int{\n\t\t{{2}, {3, 4}, {6, 5, 7}, {4, 1, 8, 3}},\n\t\t{},\n\t\t{{-1}, {2, 3}, {1, -1, -3}},\n\t}\n\n\texpectedData := []int{11, 0, -1}\n\n\tfor index, data := range testData {\n\t\tif mininum := minimumTotal(data); mininum != expectedData[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expectedData[index], mininum)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0121_best_time_to_buy_and_sell_stock/maxprofit.go",
    "content": "/*\n121. Best Time to Buy and Sell Stock\nhttps://leetcode.com/problems/best-time-to-buy-and-sell-stock/\n\nSay you have an array for which the ith element is the price of a given stock on day i.\n\nIf you were only permitted to complete at most one transaction\n(i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.\n\nNote that you cannot sell a stock before you buy one.\n*/\n// time: 2018-12-28\n\npackage maxprofit\n\n// dynamic programming\n// for every price, find the max profit, then record the current minimum price.\n// time complexity: O(n)\n// space complexity: O(1)\nfunc maxProfit(prices []int) int {\n\tn := len(prices)\n\tif 0 == n || 1 == n {\n\t\treturn 0\n\t}\n\n\tvar (\n\t\tres      int\n\t\tminPrice = prices[0]\n\t)\n\n\tfor _, price := range prices {\n\t\tif price-minPrice > res {\n\t\t\tres = price - minPrice\n\t\t}\n\t\tif price < minPrice {\n\t\t\tminPrice = price\n\t\t}\n\t}\n\treturn res\n}\n\n// brute force\n// time complexity: O(n^2)\n// space complexity: O(1)\nfunc maxProfit1(prices []int) int {\n\tn := len(prices)\n\tif 0 == n || 1 == n {\n\t\treturn 0\n\t}\n\tres := 0\n\tfor i := 1; i < n; i++ {\n\t\tfor j := 0; j < i; j++ {\n\t\t\tif prices[i]-prices[j] > res {\n\t\t\t\tres = prices[i] - prices[j]\n\t\t\t}\n\t\t}\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0121_best_time_to_buy_and_sell_stock/maxprofit_test.go",
    "content": "package maxprofit\n\nimport \"testing\"\n\nfunc TestMaxProfit(t *testing.T) {\n\ttestCases := [][]int{\n\t\t{7, 1, 5, 3, 6, 4},\n\t\t{5, 3, 2, 2, 5, 7, 9, 4},\n\t\t{},\n\t\t{3},\n\t\t{5, 3, 2, 2, 5, 7, 9, 4, 5, 3, 2, 2, 5, 7, 9, 4},\n\t\t{2, 4, 1, 11, 7},\n\t}\n\n\texpected := []int{5, 7, 0, 0, 7, 10}\n\n\tfor index, data := range testCases {\n\t\tif res := maxProfit(data); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n\nfunc TestMaxProfit1(t *testing.T) {\n\ttestCases := [][]int{\n\t\t{7, 1, 5, 3, 6, 4},\n\t\t{5, 3, 2, 2, 5, 7, 9, 4},\n\t\t{},\n\t\t{3},\n\t}\n\n\texpected := []int{5, 7, 0, 0}\n\n\tfor index, data := range testCases {\n\t\tif res := maxProfit1(data); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0122_best_time_to_buy_and_sell_stock_2/maxprofit.go",
    "content": "/*\n122. Best Time to Buy and Sell Stock II\nhttps://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/\n\nSay you have an array for which the ith element is the price of a given stock on day i.\n\nDesign an algorithm to find the maximum profit.\nYou may complete as many transactions as you like\n(i.e., buy one and sell one share of the stock multiple times).\n\nNote: You may not engage in multiple transactions at the same time\n(i.e., you must sell the stock before you buy again).\n*/\n// time: 2018-12-28\n\npackage maxprofit\n\n// greedy\n// time complexity: O(n)\n// space complexity: O(1)\nfunc maxProfit(prices []int) int {\n\tn := len(prices)\n\n\tif 0 == n || 1 == n {\n\t\treturn 0\n\t}\n\n\tvar (\n\t\tres      int\n\t\tminPrice = prices[0]\n\t)\n\tfor i := 1; i < n; i++ {\n\t\tif prices[i] < prices[i-1] {\n\t\t\tres += prices[i-1] - minPrice\n\t\t\tminPrice = prices[i]\n\t\t}\n\t\tif i == n-1 {\n\t\t\tres += prices[i] - minPrice\n\t\t}\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0122_best_time_to_buy_and_sell_stock_2/maxprofit_test.go",
    "content": "package maxprofit\n\nimport \"testing\"\n\nfunc TestMaxProfit(t *testing.T) {\n\ttestCases := [][]int{\n\t\t{7, 1, 5, 3, 6, 4},\n\t\t{1, 2, 3, 4, 5},\n\t\t{7, 6, 4, 3, 1},\n\t\t{}, {1},\n\t}\n\texpected := []int{7, 4, 0, 0, 0}\n\n\tfor index, data := range testCases {\n\t\tif res := maxProfit(data); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0125_valid_palindrome/valid_palindrome.go",
    "content": "/*\n125. Valid Palindrome\nhttps://leetcode.com/problems/valid-palindrome/\n\nGiven a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.\n\nNote: For the purpose of this problem, we define empty string as valid palindrome.\n*/\n\n// time: 2018-12-26\n\npackage validpalindrome\n\n// double index\n// time complexity: O(n)\n// space complexity: O(n)\nfunc isPalindrome(s string) bool {\n\tchars := make([]uint8, 0)\n\n\tfor i := 0; i < len(s); i++ {\n\t\tif s[i] >= 65 && s[i] <= 90 {\n\t\t\tchars = append(chars, s[i])\n\t\t}\n\t\tif s[i] >= 97 && s[i] <= 122 {\n\t\t\tchars = append(chars, s[i]-32)\n\t\t}\n\t\tif s[i] >= 48 && s[i] <= 57 {\n\t\t\tchars = append(chars, s[i])\n\t\t}\n\t}\n\n\tvar (\n\t\tl int\n\t\tr = len(chars) - 1\n\t)\n\n\tfor r >= l {\n\t\tif chars[r] != chars[l] {\n\t\t\treturn false\n\t\t}\n\t\tr--\n\t\tl++\n\t}\n\treturn true\n}\n"
  },
  {
    "path": "solutions/0125_valid_palindrome/valid_palindrome_test.go",
    "content": "package validpalindrome\n\nimport \"testing\"\n\nfunc TestIsPalindrome(t *testing.T) {\n\ttestCases := []string{\n\t\t\"A man, a plan, a canal: Panama\",\n\t\t\"race a car\",\n\t\t\"0P\",\n\t\t\"\",\n\t}\n\n\texpected := []bool{true, false, false, true}\n\n\tfor index, data := range testCases {\n\t\tif res := isPalindrome(data); res != expected[index] {\n\t\t\tt.Errorf(\"expected %t, got %t\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0136_single_number/single_number.go",
    "content": "/*\n136. Single Number\nhttps://leetcode.com/problems/single-number/\n\nGiven a non-empty array of integers, every element appears twice except for one. Find that single one.\n*/\n// time: 2019-02-01\n\npackage sn\n\n// time complexity: O(n)\n// space complexity: O(n)\nfunc singleNumber(nums []int) int {\n\trecord := make(map[int]int)\n\tfor _, num := range nums {\n\t\tif _, ok := record[num]; ok {\n\t\t\tdelete(record, num)\n\t\t} else {\n\t\t\trecord[num] = 1\n\t\t}\n\t}\n\tvar res int\n\tfor key := range record {\n\t\tres = key\n\t}\n\treturn res\n}\n\n// time complexity: O(n)\n// space complexity: O(1)\nfunc singleNumber1(nums []int) int {\n\tres := 0\n\tfor _, num := range nums {\n\t\tres ^= num\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0136_single_number/single_number_test.go",
    "content": "package sn\n\nimport \"testing\"\n\nfunc TestSingleNumber(t *testing.T) {\n\ttestCases := [][]int{\n\t\t{2, 2, 1},\n\t\t{4, 1, 2, 1, 2},\n\t}\n\texpected := []int{1, 4}\n\ttestFuncs := []func([]int) int{\n\t\tsingleNumber, singleNumber1,\n\t}\n\n\tfor _, testFunc := range testFuncs {\n\t\tfor index, nums := range testCases {\n\t\t\tif res := testFunc(nums); res != expected[index] {\n\t\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t\t}\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0144_binary_tree_preorder_traversal/binary_tree_preorder_traversal.go",
    "content": "/*\n144. Binary Tree Preorder Traversal\nhttps://leetcode.com/problems/binary-tree-preorder-traversal/\n\nGiven a binary tree, return the preorder traversal of its nodes' values.\n*/\n// time: 2019-01-06\n\npackage btpot\n\n// TreeNode Definition for a binary tree node.\ntype TreeNode struct {\n\tVal   int\n\tLeft  *TreeNode\n\tRight *TreeNode\n}\n\n// recursive\n// time complexity: O(n), where n is nodes numbers in the tree.\n// space complexity: O(h), where h is the height of the tree.\nfunc preorderTraversal(root *TreeNode) []int {\n\tif root == nil {\n\t\treturn []int{}\n\t}\n\n\tif root.Left == nil && root.Right == nil {\n\t\treturn []int{root.Val}\n\t}\n\n\tleft := preorderTraversal(root.Left)\n\tright := preorderTraversal(root.Right)\n\n\tres := []int{root.Val}\n\tres = append(res, left...)\n\tres = append(res, right...)\n\treturn res\n}\n\n// iterative\n// time complexity: O(n), where n is nodes numbers in the tree.\n// space complexity: O(h), where h is the height of the tree.\nfunc preorderTraversal1(root *TreeNode) []int {\n\tif root == nil {\n\t\treturn []int{}\n\t}\n\n\tvar stack []*TreeNode\n\tvar res []int\n\tstack = append(stack, root)\n\n\tfor len(stack) > 0 {\n\t\tnode := stack[len(stack)-1]\n\t\tstack = stack[:len(stack)-1]\n\n\t\tres = append(res, node.Val)\n\n\t\tif node.Right != nil {\n\t\t\tstack = append(stack, node.Right)\n\t\t}\n\n\t\tif node.Left != nil {\n\t\t\tstack = append(stack, node.Left)\n\t\t}\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0144_binary_tree_preorder_traversal/binary_tree_preorder_traversal_test.go",
    "content": "package btpot\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc createBinaryTree(nums []int) *TreeNode {\n\treturn performCreate(nums, 0)\n}\n\nfunc performCreate(nums []int, index int) *TreeNode {\n\tif index >= len(nums) {\n\t\treturn nil\n\t}\n\n\ttree := TreeNode{Val: nums[index]}\n\ttree.Left = performCreate(nums, 2*index+1)\n\ttree.Right = performCreate(nums, 2*index+2)\n\treturn &tree\n}\n\nfunc TestPreorderTraversal(t *testing.T) {\n\ttestCases := []*TreeNode{\n\t\tnil,\n\t\tcreateBinaryTree([]int{1, 2, 3, 4, 5, 6, 7, 8}),\n\t}\n\texpected := [][]int{\n\t\t{},\n\t\t{1, 2, 4, 8, 5, 3, 6, 7},\n\t}\n\ttestFuncs := []func(node *TreeNode) []int{\n\t\tpreorderTraversal,\n\t\tpreorderTraversal1,\n\t}\n\n\tfor _, testFunc := range testFuncs {\n\t\tfor index, root := range testCases {\n\t\t\tif res := testFunc(root); !reflect.DeepEqual(res, expected[index]) {\n\t\t\t\tt.Errorf(\"expected %v, got %v\", expected[index], res)\n\t\t\t}\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0150_evaluate_reverse_polish_notation/evaluate_reverse_polish_notation.go",
    "content": "/*\n150. Evaluate Reverse Polish Notation\nhttps://leetcode.com/problems/evaluate-reverse-polish-notation/\n\nEvaluate the value of an arithmetic expression in Reverse Polish Notation.\n\nValid operators are +, -, *, /. Each operand may be an integer or another expression.\n\nNote:\n\nDivision between two integers should truncate toward zero.\nThe given RPN expression is always valid.\nThat means the expression would always evaluate to a result and there won't be any divide by zero operation.\n*/\n// time: 2019-01-07\n\npackage evaluatereversepolishnotation\n\nimport \"strconv\"\n\n// stack\n// time complexity: O(n)\n// space complexity: O(n)\nfunc evalRPN(tokens []string) int {\n\tstack := make([]int, len(tokens))\n\ttop := -1\n\tfor i := 0; i < len(tokens); i++ {\n\t\tswitch ch := tokens[i]; ch {\n\t\tcase \"+\":\n\t\t\tstack[top-1] += stack[top]\n\t\t\ttop--\n\t\tcase \"-\":\n\t\t\tstack[top-1] -= stack[top]\n\t\t\ttop--\n\t\tcase \"*\":\n\t\t\tstack[top-1] *= stack[top]\n\t\t\ttop--\n\t\tcase \"/\":\n\t\t\tstack[top-1] /= stack[top]\n\t\t\ttop--\n\t\tdefault:\n\t\t\ttop++\n\t\t\tstack[top], _ = strconv.Atoi(ch)\n\t\t}\n\t}\n\treturn stack[0]\n}\n"
  },
  {
    "path": "solutions/0150_evaluate_reverse_polish_notation/evaluate_reverse_polish_notation_test.go",
    "content": "package evaluatereversepolishnotation\n\nimport \"testing\"\n\nfunc TestEvalRPN(t *testing.T) {\n\ttestCases := [][]string{\n\t\t{\"2\", \"1\", \"+\", \"3\", \"*\"},\n\t\t{\"4\", \"13\", \"5\", \"/\", \"+\"},\n\t\t{\"10\", \"6\", \"9\", \"3\", \"+\", \"-11\", \"*\", \"/\", \"*\", \"17\", \"+\", \"5\", \"+\"},\n\t\t{\"4\", \"3\", \"-\"},\n\t}\n\n\texpected := []int{9, 6, 22, 1}\n\n\tfor index, tokens := range testCases {\n\t\tif res := evalRPN(tokens); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0153_find_minimum_in_rotated_sorted_array/fmirsa.go",
    "content": "/*\n153. Find Minimum in Rotated Sorted Array\nhttps://leetcode.com/problems/find-minimum-in-rotated-sorted-array/\n\nSuppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.\n\n(i.e.,  [0,1,2,4,5,6,7] might become  [4,5,6,7,0,1,2]).\n\nFind the minimum element.\n\nYou may assume no duplicate exists in the array.\n*/\n// time: 2019-01-07\n\npackage fmirsa\n\n// binary search\n// time complexity: O( log n)\n// space complexity: O(1)\nfunc findMin(nums []int) int {\n\tvar (\n\t\tlow  int\n\t\thigh = len(nums) - 1\n\t\tmid  int\n\t)\n\tfor low < high {\n\t\tmid = low + (high-low)>>1\n\t\tif nums[high] < nums[mid] {\n\t\t\tlow = mid + 1\n\t\t} else {\n\t\t\thigh = mid\n\t\t}\n\t}\n\treturn nums[low]\n}\n"
  },
  {
    "path": "solutions/0153_find_minimum_in_rotated_sorted_array/fmirsa_test.go",
    "content": "package fmirsa\n\nimport \"testing\"\n\nfunc TestFindMin(t *testing.T) {\n\tnums := []int{3, 4, 5, 1, 2}\n\texpected := 1\n\tif res := findMin(nums); res != expected {\n\t\tt.Errorf(\"expected %d, got %d\", expected, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0155_min_stack/min_stack.go",
    "content": "/*\n155. Min Stack\nhttps://leetcode.com/problems/min-stack/\n\nDesign a stack that supports push, pop, top, and retrieving the minimum element in constant time.\n\npush(x) -- Push element x onto stack.\npop() -- Removes the element on top of the stack.\ntop() -- Get the top element.\ngetMin() -- Retrieve the minimum element in the stack.\n*/\n// time: 2019-01-10\n\npackage minstack\n\n// MinStack a stack that supports push, pop, top, and retrieving the minimum element in constant time.\ntype MinStack struct {\n\tdata []int\n}\n\n// Constructor initialize your data structure here.\nfunc Constructor() MinStack {\n\treturn MinStack{}\n}\n\n// Push element x onto stack.\nfunc (s *MinStack) Push(x int) {\n\ts.data = append(s.data, x)\n}\n\n// Pop Removes the element on top of the stack.\nfunc (s *MinStack) Pop() {\n\tif !s.IsEmpty() {\n\t\ts.data = s.data[:s.GetSize()-1]\n\t}\n}\n\n// Top  Get the top element.\nfunc (s *MinStack) Top() int {\n\t//if s.IsEmpty(){\n\t//\treturn 0\n\t//}\n\t// stack must have element.\n\treturn s.data[s.GetSize()-1]\n}\n\n// GetMin retrieving the minimum element in constant time.\n// time complexity: O(n)\nfunc (s *MinStack) GetMin() int {\n\t//if s.IsEmpty() {\n\t//\treturn 0\n\t//}\n\t// stack must have element.\n\tstackSize := s.GetSize()\n\tret := s.data[stackSize-1]\n\n\tfor i := stackSize - 2; i >= 0; i-- {\n\t\tif s.data[i] < ret {\n\t\t\tret = s.data[i]\n\t\t}\n\t}\n\treturn ret\n}\n\n// GetSize get size of the stack.\nfunc (s MinStack) GetSize() int {\n\treturn len(s.data)\n}\n\nfunc (s MinStack) IsEmpty() bool {\n\treturn s.GetSize() == 0\n}\n"
  },
  {
    "path": "solutions/0155_min_stack/min_stack_test.go",
    "content": "package minstack\n\nimport \"testing\"\n\nfunc TestMinStack(t *testing.T) {\n\tobj := Constructor()\n\tobj.Push(-2)\n\tobj.Push(0)\n\tobj.Push(-3)\n\tif res := obj.GetMin(); res != -3 {\n\t\tt.Errorf(\"expected %d, got %d\", -3, res)\n\t}\n\n\tobj.Pop()\n\tif res := obj.Top(); res != 0 {\n\t\tt.Errorf(\"expected %d, got %d\", 0, res)\n\t}\n\tif res := obj.GetMin(); res != -2 {\n\t\tt.Errorf(\"expected %d, got %d\", -2, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0165_compare_version_numbers/compare_version_numbers.go",
    "content": "/*\n165. Compare Version Numbers\nhttps://leetcode.com/problems/compare-version-numbers/\n\nCompare two version numbers version1 and version2.\nIf version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0.\n\nYou may assume that the version strings are non-empty and contain only digits and the . character.\n\nThe . character does not represent a decimal point and is used to separate number sequences.\n\nFor instance, 2.5 is not \"two and a half\" or \"half way to version three\",\nit is the fifth second-level revision of the second first-level revision.\n\nYou may assume the default revision number for each level of a version number to be 0.\nFor example, version number 3.4 has a revision number of 3 and 4 for its first and second level revision number.\nIts third and fourth level revision number are both 0.\n\nNote:\n\nVersion strings are composed of numeric strings separated by dots . and this numeric strings may have leading zeroes.\nVersion strings do not start or end with dots, and they will not be two consecutive dots.\n*/\n// time: 2019-01-07\n\npackage compareversionnumbers\n\nimport (\n\t\"strconv\"\n\t\"strings\"\n)\n\n// time complexity: O( max n, m )\n// space complexity: O(n+m)\nfunc compareVersion(version1 string, version2 string) int {\n\tvar (\n\t\tv1 = strings.Split(version1, \".\")\n\t\tv2 = strings.Split(version2, \".\")\n\t)\n\tfor i := 0; i < len(v1) || i < len(v2); i++ {\n\t\tvar v1N, v2N int\n\t\tif i < len(v1) {\n\t\t\tv1N, _ = strconv.Atoi(v1[i])\n\t\t}\n\t\tif i < len(v2) {\n\t\t\tv2N, _ = strconv.Atoi(v2[i])\n\t\t}\n\n\t\tif v1N > v2N {\n\t\t\treturn 1\n\t\t} else if v1N < v2N {\n\t\t\treturn -1\n\t\t}\n\t}\n\treturn 0\n}\n"
  },
  {
    "path": "solutions/0165_compare_version_numbers/compare_version_numbers_test.go",
    "content": "package compareversionnumbers\n\nimport \"testing\"\n\nfunc TestCompareVersion(t *testing.T) {\n\ttype arg struct {\n\t\tversion1 string\n\t\tversion2 string\n\t}\n\n\ttestCases := []arg{\n\t\t{version1: \"0.1\", version2: \"1.1\"},\n\t\t{version1: \"1.0.1\", version2: \"1\"},\n\t\t{version1: \"1.01\", version2: \"1.001\"},\n\t}\n\texpected := []int{-1, 1, 0}\n\n\tfor index, data := range testCases {\n\t\tif res := compareVersion(data.version1, data.version2); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0167_two_sum2/two_sum2.go",
    "content": "/*\n167. Two Sum II - Input array is sorted\nhttps://leetcode.com/problems/two-sum-ii-input-array-is-sorted/\n\nGiven an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.\n\nThe function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2.\n\nNote:\n\tYour returned answers (both index1 and index2) are not zero-based.\n\tYou may assume that each input would have exactly one solution and you may not use the same element twice.\n\n\tExample:\n\nInput: numbers = [2,7,11,15], target = 9\nOutput: [1,2]\nExplanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.\n*/\n\npackage twosum2\n\nfunc twoSum2(numbers []int, target int) []int {\n\tn := len(numbers)\n\tvar (\n\t\tl = 0\n\t\tr = n - 1\n\t)\n\n\tfor l < r {\n\t\tif numbers[l]+numbers[r] == target {\n\t\t\treturn []int{l + 1, r + 1}\n\t\t} else if numbers[l]+numbers[r] < target {\n\t\t\tl++\n\t\t} else {\n\t\t\tr--\n\t\t}\n\t}\n\tpanic(\"The input has no solution.\")\n}\n"
  },
  {
    "path": "solutions/0167_two_sum2/two_sum2_test.go",
    "content": "package twosum2\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\ntype arg struct {\n\tnumbers []int\n\ttarget  int\n}\n\nfunc TestTwoSum2(t *testing.T) {\n\ttestData := []arg{\n\t\t{\n\t\t\tnumbers: []int{2, 7, 11, 15},\n\t\t\ttarget:  9,\n\t\t},\n\t}\n\n\texpectedData := [][]int{\n\t\t{1, 2},\n\t}\n\n\tfor index, data := range testData {\n\t\tif res := twoSum2(data.numbers, data.target); !reflect.DeepEqual(res, expectedData[index]) {\n\t\t\tt.Errorf(\"expected %v, got %v\", expectedData[index], res)\n\t\t}\n\t}\n\n\tdefer func() {\n\t\tif err := recover(); err == nil {\n\t\t\tt.Errorf(\"there should throw a panic\")\n\t\t}\n\t}()\n\n\ttwoSum2([]int{2, 7, 11, 15}, 90)\n}\n"
  },
  {
    "path": "solutions/0179_largest_number/ln.go",
    "content": "/*\n179. Largest Number\nhttps://leetcode.com/problems/largest-number/\n\nGiven a list of non negative integers, arrange them such that they form the largest number.\nNote: The result may be very large, so you need to return a string instead of an integer.\n*/\n// time: 2019-01-14\n\npackage ln\n\nimport (\n\t\"sort\"\n\t\"strconv\"\n\t\"strings\"\n)\n\ntype sliceString []string\n\n// Len is the number of elements in the collection.\nfunc (s sliceString) Len() int { return len(s) }\n\n// Swap swaps the elements with indexes i and j.\nfunc (s sliceString) Swap(i, j int) { s[i], s[j] = s[j], s[i] }\n\n// Less reports whether the element with\n// index i should sort before the element with index j.\nfunc (s sliceString) Less(i, j int) bool { return (s[i] + s[j]) > (s[j] + s[i]) }\n\n// time complexity: O(n log n), dominated by the complexity of sort.\n// space complexity: O(n)\nfunc largestNumber(nums []int) string {\n\tnumsString := make([]string, 0)\n\tfor _, num := range nums {\n\t\tnumsString = append(numsString, strconv.Itoa(num))\n\t}\n\n\tsort.Sort(sliceString(numsString))\n\tnumStr := strings.Join(numsString, \"\")\n\n\tif strings.HasPrefix(numStr, \"0\") {\n\t\treturn \"0\"\n\t}\n\treturn numStr\n}\n"
  },
  {
    "path": "solutions/0179_largest_number/ln_test.go",
    "content": "package ln\n\nimport \"testing\"\n\nfunc TestLargestNumber(t *testing.T) {\n\ttestCases := [][]int{\n\t\t{10, 2},\n\t\t{3, 30, 34, 5, 9},\n\t\t{0},\n\t}\n\n\texpected := []string{\n\t\t\"210\",\n\t\t\"9534330\",\n\t\t\"0\",\n\t}\n\n\tfor index, nums := range testCases {\n\t\tif res := largestNumber(nums); res != expected[index] {\n\t\t\tt.Errorf(\"expected %s, got %s\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0198_house_robber/house_robber.go",
    "content": "/*\n198. House Robber\nhttps://leetcode.com/problems/house-robber/\n\nYou are a professional robber planning to rob houses along a street. Each house has a certain amount\n of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses\n  have security system connected and it will automatically contact the police if two adjacent houses were\n  broken into on the same night.\n\nGiven a list of non-negative integers representing the amount of money of each house, determine the\nmaximum amount of money you can rob tonight without alerting the police.\n\nExample 1:\n\nInput: [1,2,3,1]\nOutput: 4\nExplanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).\n             Total amount you can rob = 1 + 3 = 4.\nExample 2:\n\nInput: [2,7,9,3,1]\nOutput: 12\nExplanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).\n             Total amount you can rob = 2 + 9 + 1 = 12.\n*/\n\npackage houserobber\n\nimport \"leetcode/utils\"\n\n/*\nfunc rob(nums []int) int {\n\treturn tryRob(nums, 0)\n}\n\nfunc tryRob(nums []int, index int) int {\n\tif index >= len(nums) {\n\t\treturn 0\n\t}\n\n\tvar res int\n\tfor i := index; i < len(nums); i++ {\n\t\tres, _ = utils.CalcMaxInt(res, nums[i]+tryRob(nums, i+2))\n\t}\n\treturn res\n}\n*/\n\n// memory search\n/*\nfunc rob(nums []int) int {\n\tmemo := make([]int, len(nums))\n\tfor i := 0; i < len(nums); i++ {\n\t\tmemo[i] = -1\n\t}\n\treturn tryRob(nums, 0, memo)\n}\n\nfunc tryRob(nums []int, index int, memo []int) int {\n\tif index >= len(nums) {\n\t\treturn 0\n\t}\n\n\tif memo[index] != -1 {\n\t\treturn memo[index]\n\t}\n\n\tvar res int\n\tfor i := index; i < len(nums); i++ {\n\t\tres, _ = utils.CalcMaxInt(res, nums[i]+tryRob(nums, i+2, memo))\n\t}\n\tmemo[index] = res\n\treturn res\n}*/\n\n// dynamic programming\nfunc rob(nums []int) int {\n\tn := len(nums)\n\tif n == 0 {\n\t\treturn 0\n\t}\n\n\tmemo := make([]int, n)\n\n\tfor i := 0; i < n; i++ {\n\t\tmemo[i] = -1\n\t}\n\tmemo[n-1] = nums[n-1]\n\n\tfor i := n - 2; i >= 0; i-- {\n\t\tfor j := i; j < n; j++ {\n\t\t\tif j+2 < n {\n\t\t\t\tmemo[i] = utils.CalcMaxInt(memo[i], nums[j]+memo[j+2])\n\t\t\t} else {\n\t\t\t\tmemo[i] = utils.CalcMaxInt(memo[i], nums[j])\n\t\t\t}\n\t\t}\n\t}\n\treturn memo[0]\n}\n"
  },
  {
    "path": "solutions/0198_house_robber/house_robber_test.go",
    "content": "package houserobber\n\nimport \"testing\"\n\nfunc TestHouseRobber(t *testing.T) {\n\ttestData := [][]int{\n\t\t{1, 2, 3, 1},\n\t\t{9, 1, 293, 5986, 81, 384},\n\t\t{1, 2},\n\t\t{},\n\t}\n\texpectedData := []int{4, 6379, 2, 0}\n\n\tfor index, data := range testData {\n\t\tif res := rob(data); res != expectedData[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expectedData[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0200_number_of_island/number_of_island.go",
    "content": "/*\n200. Number of Islands\nhttps://leetcode.com/problems/number-of-islands/\n\nGiven a 2d grid map of '1's (land) and '0's (water), count the number of islands.\nAn island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically.\nYou may assume all four edges of the grid are all surrounded by water.\n*/\n// time: 2019-01-07\n\npackage noi\n\nvar (\n\td       = [][]int{{0, 1}, {1, 0}, {0, -1}, {-1, 0}}\n\tm, n    int\n\tvisited [][]bool\n)\n\n// flood-fill\n// time complexity: O(n*m)\n// space complexity: O(n*m)\nfunc numIslands(grid [][]byte) int {\n\tm = len(grid)\n\tif m <= 0 {\n\t\treturn 0\n\t}\n\tn = len(grid[0])\n\n\tvisited = make([][]bool, m)\n\tfor i := 0; i < m; i++ {\n\t\tvisited[i] = make([]bool, n)\n\t}\n\n\tvar res int\n\n\tfor i := 0; i < m; i++ {\n\t\tfor j := 0; j < n; j++ {\n\t\t\tif grid[i][j] == '1' && !visited[i][j] {\n\t\t\t\tres++\n\t\t\t\tdfs(grid, i, j)\n\t\t\t}\n\t\t}\n\t}\n\treturn res\n}\n\n// 从grid[x][y]的位置开始，进行floodfill。\nfunc dfs(grid [][]byte, x, y int) {\n\tvisited[x][y] = true\n\tfor i := 0; i < len(d); i++ {\n\t\tnewX := x + d[i][0]\n\t\tnewY := y + d[i][1]\n\t\tif inArea(newX, newY) && !visited[newX][newY] && grid[newX][newY] == '1' {\n\t\t\tdfs(grid, newX, newY)\n\t\t}\n\t}\n}\n\nfunc inArea(x, y int) bool {\n\treturn x >= 0 && x < m && y >= 0 && y < n\n}\n"
  },
  {
    "path": "solutions/0200_number_of_island/number_of_island_test.go",
    "content": "package noi\n\nimport \"testing\"\n\nfunc TestNumIslands(t *testing.T) {\n\tgrids := [][][]byte{\n\t\t{\n\t\t\t{'1', '1', '0', '0', '0'},\n\t\t\t{'1', '1', '0', '0', '0'},\n\t\t\t{'0', '0', '1', '0', '0'},\n\t\t\t{'0', '0', '0', '1', '1'},\n\t\t},\n\t\t{},\n\t}\n\texpected := []int{3, 0}\n\n\tfor index, grid := range grids {\n\t\tif res := numIslands(grid); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n\n}\n"
  },
  {
    "path": "solutions/0203_remove_linked_list_elements/remove_linked_list_elements.go",
    "content": "/*\n203. Remove Linked List Elements\nhttps://leetcode.com/problems/remove-linked-list-elements/\n\nRemove all elements from a linked list of integers that have value val.\n*/\n// time: 2019-01-07\n\npackage rlle\n\n// ListNode Definition for singly-linked list.\ntype ListNode struct {\n\tVal  int\n\tNext *ListNode\n}\n\n// time complexity: O(n)\n// space complexity: O(1)\nfunc removeElements(head *ListNode, val int) *ListNode {\n\tdummyHead := &ListNode{}\n\tdummyHead.Next = head\n\n\tcur := dummyHead\n\n\tfor cur.Next != nil {\n\t\tif val == cur.Next.Val {\n\t\t\tcur.Next = cur.Next.Next\n\t\t} else {\n\t\t\tcur = cur.Next\n\t\t}\n\t}\n\treturn dummyHead.Next\n}\n\n// recursive\n// time complexity: O(n)\n// space complexity: O(n)\nfunc removeElements1(head *ListNode, val int) *ListNode {\n\tif head == nil {\n\t\treturn head\n\t}\n\thead.Next = removeElements1(head.Next, val)\n\tif head.Val == val {\n\t\treturn head.Next\n\t}\n\treturn head\n}\n"
  },
  {
    "path": "solutions/0203_remove_linked_list_elements/remove_linked_list_elements_test.go",
    "content": "package rlle\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestRemoveElements(t *testing.T) {\n\thead := createSinglyLinkedList([]int{1, 2, 6, 3, 4, 5, 6})\n\tval := 6\n\texpected := createSinglyLinkedList([]int{1, 2, 3, 4, 5})\n\n\tif res := removeElements(head, val); !reflect.DeepEqual(res, expected) {\n\t\tt.Errorf(\"expected %v, got %v\", expected, res)\n\t}\n}\n\nfunc TestRemoveElements1(t *testing.T) {\n\thead := createSinglyLinkedList([]int{1, 2, 6, 3, 4, 5, 6})\n\tval := 6\n\texpected := createSinglyLinkedList([]int{1, 2, 3, 4, 5})\n\n\tif res := removeElements1(head, val); !reflect.DeepEqual(res, expected) {\n\t\tt.Errorf(\"expected %v, got %v\", expected, res)\n\t}\n}\n\nfunc createSinglyLinkedList(nums []int) *ListNode {\n\thead := &ListNode{}\n\tcur := head\n\n\tfor _, num := range nums {\n\t\tcur.Next = &ListNode{Val: num}\n\t\tcur = cur.Next\n\t}\n\treturn head.Next\n}\n"
  },
  {
    "path": "solutions/0206_reverse_linked_list/reverse_linked_list.go",
    "content": "/*\n206. Reverse Linked List\nhttps://leetcode.com/problems/reverse-linked-list/\n\nReverse a singly linked list.\n*/\n// time: 2019-01-07\n\npackage rll\n\n// ListNode Definition for singly-linked list.\ntype ListNode struct {\n\tVal  int\n\tNext *ListNode\n}\n\n// recursive\n// time complexity: O(n)\n// space complexity: O(n)\nfunc reverseList(head *ListNode) *ListNode {\n\tif head == nil || head.Next == nil {\n\t\treturn head\n\t}\n\n\ttail := reverseList(head.Next)\n\thead.Next.Next = head\n\thead.Next = nil\n\treturn tail\n}\n\n// iterative\n// time complexity: O(n)\n// space complexity: O(1)\nfunc reverseList1(head *ListNode) *ListNode {\n\tvar (\n\t\tpre *ListNode\n\t\tcur = head\n\t)\n\tfor cur != nil {\n\t\tnext := cur.Next\n\t\tcur.Next = pre\n\t\tpre = cur\n\t\tcur = next\n\t}\n\treturn pre\n}\n"
  },
  {
    "path": "solutions/0206_reverse_linked_list/reverse_linked_list_test.go",
    "content": "package rll\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestReverseList(t *testing.T) {\n\thead := createSinglyLinkedList([]int{1, 2, 3, 4, 5})\n\texpected := createSinglyLinkedList([]int{5, 4, 3, 2, 1})\n\tif res := reverseList(head); !reflect.DeepEqual(res, expected) {\n\t\tt.Errorf(\"expected %v, got %v\", expected, res)\n\t}\n}\n\nfunc TestReverseList1(t *testing.T) {\n\thead := createSinglyLinkedList([]int{1, 2, 3, 4, 5})\n\texpected := createSinglyLinkedList([]int{5, 4, 3, 2, 1})\n\tif res := reverseList1(head); !reflect.DeepEqual(res, expected) {\n\t\tt.Errorf(\"expected %v, got %v\", expected, res)\n\t}\n}\n\nfunc createSinglyLinkedList(nums []int) *ListNode {\n\thead := &ListNode{}\n\tcur := head\n\n\tfor _, num := range nums {\n\t\tcur.Next = &ListNode{Val: num}\n\t\tcur = cur.Next\n\t}\n\treturn head.Next\n}\n"
  },
  {
    "path": "solutions/0208_implement_trie_prefix_tree/impltrie.go",
    "content": "/*\n208. Implement Trie (Prefix Tree)\nhttps://leetcode.com/problems/implement-trie-prefix-tree/\n\nImplement a trie with insert, search, and startsWith methods.\n\nNote:\nYou may assume that all inputs are consist of lowercase letters a-z.\nAll inputs are guaranteed to be non-empty strings.\n*/\n// time: 2019-01-31\n\npackage impltree\n\ntype node struct {\n\tisWord bool\n\tnext   map[byte]*node\n}\n\n// Trie prefix tree\ntype Trie struct {\n\troot *node\n\tsize int\n}\n\n// Constructor initialize data structure here.\nfunc Constructor() Trie {\n\treturn Trie{root: &node{next: make(map[byte]*node)}}\n}\n\n// Insert inserts a word into the trie.\nfunc (trie *Trie) Insert(word string) {\n\tcur := trie.root\n\n\tfor i := 0; i < len(word); i++ {\n\t\tif _, ok := cur.next[word[i]]; !ok {\n\t\t\tcur.next[word[i]] = &node{next: make(map[byte]*node)}\n\t\t}\n\t\tcur = cur.next[word[i]]\n\t}\n\tif !cur.isWord {\n\t\tcur.isWord = true\n\t\ttrie.size++\n\t}\n}\n\n// Search returns if the word is the trie.\nfunc (trie *Trie) Search(word string) bool {\n\tcur := trie.root\n\n\tfor i := 0; i < len(word); i++ {\n\t\tif _, ok := cur.next[word[i]]; !ok {\n\t\t\treturn false\n\t\t}\n\t\tcur = cur.next[word[i]]\n\t}\n\treturn cur.isWord\n}\n\n// StartsWith returns if there is any word in the trie that starts with the given prefix.\nfunc (trie *Trie) StartsWith(prefix string) bool {\n\tcur := trie.root\n\n\tfor i := 0; i < len(prefix); i++ {\n\t\tif _, ok := cur.next[prefix[i]]; !ok {\n\t\t\treturn false\n\t\t}\n\t\tcur = cur.next[prefix[i]]\n\t}\n\treturn true\n}\n"
  },
  {
    "path": "solutions/0208_implement_trie_prefix_tree/impltrie_test.go",
    "content": "package impltree\n\nimport \"testing\"\n\nfunc TestImplTrie(t *testing.T) {\n\ttrie := Constructor()\n\ttrie.Insert(\"apple\")\n\n\tfor i, j := range map[string]bool{\"apple\": true, \"app\": false, \"hello\": false} {\n\t\tif res := trie.Search(i); res != j {\n\t\t\tt.Errorf(\"expected %t, got %t\", j, res)\n\t\t}\n\t}\n\n\tfor i, j := range map[string]bool{\"app\": true, \"as\": false} {\n\t\tif res := trie.StartsWith(i); res != j {\n\t\t\tt.Errorf(\"expected %t, got %t\", j, res)\n\t\t}\n\t}\n\n\ttrie.Insert(\"app\")\n\tif res := trie.Search(\"app\"); res != true {\n\t\tt.Errorf(\"expected %t, got %t\", true, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go",
    "content": "/*\n209. Minimum Size Subarray Sum\nhttps://leetcode.com/problems/minimum-size-subarray-sum/\n\nGiven an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.\n\nExample:\n\nInput: s = 7, nums = [2,3,1,2,4,3]\nOutput: 2\nExplanation: the subarray [4,3] has the minimal length under the problem constraint.\n\nFollow up:\nIf you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).\n*/\n\npackage minimumsizesubarraysum\n\nfunc minSubArrayLen(s int, nums []int) int {\n\tvar (\n\t\tl   = 0\n\t\tr   = -1\n\t\tres = len(nums) + 1\n\t\tsum = 0\n\t)\n\n\tfor l < len(nums) {\n\t\tif sum < s && r+1 < len(nums) {\n\t\t\tr++\n\t\t\tsum += nums[r]\n\t\t} else {\n\t\t\tsum -= nums[l]\n\t\t\tl++\n\t\t}\n\t\tif sum >= s && res > r-l+1 {\n\t\t\tres = r - l + 1\n\t\t}\n\t}\n\tif res == len(nums)+1 {\n\t\treturn 0\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0209_minimum_size_subarray_sum/minimum_size_subarray_sum_test.go",
    "content": "package minimumsizesubarraysum\n\nimport \"testing\"\n\nfunc TestMinSubArrayLen(t *testing.T) {\n\ttype arg struct {\n\t\ts    int\n\t\tnums []int\n\t}\n\ttestData := []arg{\n\t\t{\n\t\t\ts:    7,\n\t\t\tnums: []int{2, 3, 1, 2, 4, 3},\n\t\t},\n\t\t{\n\t\t\ts:    7,\n\t\t\tnums: []int{7, 4, 3, 2},\n\t\t},\n\t\t{\n\t\t\ts:    66,\n\t\t\tnums: []int{7, 4, 3, 2},\n\t\t},\n\t}\n\n\texpectedData := []int{2, 1, 0}\n\n\tfor index, data := range testData {\n\t\tif res := minSubArrayLen(data.s, data.nums); res != expectedData[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expectedData[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0211_add_and_search_word/add_and_search_word.go",
    "content": "/*\n211. Add and Search Word - Data structure design\nhttps://leetcode.com/problems/add-and-search-word-data-structure-design/\n*/\n// time: 2019-01-31\n\npackage aasw\n\ntype node struct {\n\tisWord bool\n\tnext   map[byte]*node\n}\n\n// WordDictionary supports two operations(addWord, search)\ntype WordDictionary struct {\n\troot *node\n}\n\n// Constructor initialize data structure here.\nfunc Constructor() WordDictionary {\n\treturn WordDictionary{&node{next: make(map[byte]*node)}}\n}\n\n// AddWord adds a word into the trie.\nfunc (trie *WordDictionary) AddWord(word string) {\n\tcur := trie.root\n\tfor i := 0; i < len(word); i++ {\n\t\tif _, ok := cur.next[word[i]]; !ok {\n\t\t\tcur.next[word[i]] = &node{next: make(map[byte]*node)}\n\t\t}\n\t\tcur = cur.next[word[i]]\n\t}\n\tif !cur.isWord {\n\t\tcur.isWord = true\n\t}\n}\n\n// Search returns if the word is in the trie.\n// a word could contain the dot character '.' to represent any ont letter.\nfunc (trie *WordDictionary) Search(word string) bool {\n\treturn match(trie.root, word, 0)\n}\n\nfunc match(n *node, word string, index int) bool {\n\tif index == len(word) {\n\t\treturn n.isWord\n\t}\n\tif word[index] != '.' {\n\t\tif _, ok := n.next[word[index]]; !ok {\n\t\t\treturn false\n\t\t}\n\t\treturn match(n.next[word[index]], word, index+1)\n\t} else {\n\t\tfor _, nextNode := range n.next {\n\t\t\tif match(nextNode, word, index+1) {\n\t\t\t\treturn true\n\t\t\t}\n\t\t}\n\t\treturn false\n\t}\n}\n"
  },
  {
    "path": "solutions/0211_add_and_search_word/add_and_search_word_test.go",
    "content": "package aasw\n\nimport \"testing\"\n\nfunc TestAddAndSearchWord(t *testing.T) {\n\tobj := Constructor()\n\n\tfor _, word := range []string{\"bad\", \"dad\", \"mad\"} {\n\t\tobj.AddWord(word)\n\t}\n\n\tfor i, j := range map[string]bool{\"pad\": false, \"bad\": true, \".ad\": true, \"b..\": true, \"b..d\": false} {\n\t\tif res := obj.Search(i); res != j {\n\t\t\tt.Errorf(\"expected %t, got %t\", j, res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0215_kth_largest_element_in_an_array/kthleiaa.go",
    "content": "/*\n215. Kth Largest Element in an Array\nhttps://leetcode.com/problems/kth-largest-element-in-an-array/\n\nFind the kth largest element in an unsorted array.\n Note that it is the kth largest element in the sorted order, not the kth distinct element.\n\nYou may assume k is always valid, 1 ≤ k ≤ array's length.\n\n*/\n\n// time: 2018-12-22\n\npackage kthleiaa\n\n// heap sort\n// time complexity: O(k * logn)\n// sapce complexity: O(1)\nfunc findKthLargest(nums []int, k int) int {\n\t// heapify\n\tfor i := (len(nums) - 1) / 2; i >= 0; i-- {\n\t\tsiftDown(nums, len(nums), i)\n\t}\n\n\tfor i := len(nums) - 1; i >= len(nums)-k; i-- {\n\t\tnums[i], nums[0] = nums[0], nums[i]\n\t\tsiftDown(nums, i, 0)\n\t}\n\treturn nums[len(nums)-k]\n}\n\nfunc siftDown(nums []int, n int, i int) {\n\tfor 2*i+1 < n {\n\t\tj := 2*i + 1\n\t\tif j+1 < n && nums[j+1] > nums[j] {\n\t\t\tj++\n\t\t}\n\n\t\tif nums[i] >= nums[j] {\n\t\t\tbreak\n\t\t}\n\t\tnums[j], nums[i] = nums[i], nums[j]\n\t\ti = j\n\t}\n}\n"
  },
  {
    "path": "solutions/0215_kth_largest_element_in_an_array/kthleiaa_test.go",
    "content": "package kthleiaa\n\nimport \"testing\"\n\nfunc TestFindKthLargest(t *testing.T) {\n\ttype arg struct {\n\t\tnums []int\n\t\tk    int\n\t}\n\n\ttestCases := []arg{\n\t\t{nums: []int{3, 2, 1, 5, 6, 4}, k: 2},\n\t\t{nums: []int{3, 2, 3, 1, 2, 4, 5, 5, 6}, k: 4},\n\t}\n\n\texpected := []int{5, 4}\n\n\tfor index, data := range testCases {\n\t\tif res := findKthLargest(data.nums, data.k); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0217_contains_duplicate/contains_duplicate.go",
    "content": "/*\n217. Contains Duplicate\nhttps://leetcode.com/problems/contains-duplicate/\n\nGiven an array of integers, find if the array contains any duplicates.\n\nYour function should return true if any value appears at least twice in the array,\nand it should return false if every element is distinct.\n*/\n// time: 2019-01-07\n\npackage cd\n\n// using HashTable\n// time complexity: O(n)\n// space complexity: O(n)\nfunc containsDuplicate(nums []int) bool {\n\trecord := make(map[int]int)\n\n\tfor _, num := range nums {\n\t\tif _, ok := record[num]; !ok {\n\t\t\trecord[num] = 1\n\t\t} else {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n"
  },
  {
    "path": "solutions/0217_contains_duplicate/contains_duplicate_test.go",
    "content": "package cd\n\nimport \"testing\"\n\nfunc TestContainsDuplicate(t *testing.T) {\n\ttestCases := [][]int{\n\t\t{1, 2, 3, 1},\n\t\t{1, 2, 3, 4},\n\t}\n\texpected := []bool{true, false}\n\n\tfor index, nums := range testCases {\n\t\tif res := containsDuplicate(nums); res != expected[index] {\n\t\t\tt.Errorf(\"expected %t, got %t\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0219_contains_duplicate_2/contains_duplicate_2.go",
    "content": "/*\n219. Contains Duplicate II\nhttps://leetcode.com/problems/contains-duplicate-ii/\n\nGiven an array of integers and an integer k,\nfind out whether there are two distinct indices i and j in the array\nsuch that nums[i] = nums[j] and the absolute difference between i and j is at most k.\n*/\n// time: 2019-01-07\n\npackage cond\n\n// using hash table\n// time complexity: O(n)\n// space complexity: O(n)\nfunc containsNearbyDuplicate(nums []int, k int) bool {\n\trecord := make(map[int]int, len(nums))\n\tfor i, v := range nums {\n\t\tif j, ok := record[v]; ok {\n\t\t\tif i-j <= k {\n\t\t\t\treturn true\n\t\t\t}\n\t\t}\n\t\trecord[v] = i\n\t}\n\treturn false\n}\n"
  },
  {
    "path": "solutions/0219_contains_duplicate_2/contains_duplicate_2_test.go",
    "content": "package cond\n\nimport \"testing\"\n\nfunc TestContainsNearbyDuplicate(t *testing.T) {\n\ttestCases := [][]int{\n\t\t{1, 2, 3, 1},\n\t\t{1, 0, 1, 1},\n\t\t{1, 2, 3, 1, 2, 3},\n\t}\n\tks := []int{3, 1, 2}\n\texpected := []bool{true, true, false}\n\n\tfor index, nums := range testCases {\n\t\tif res := containsNearbyDuplicate(nums, ks[index]); res != expected[index] {\n\t\t\tt.Errorf(\"expected %t, got %t\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0226_invert_binary_tree/invert_binary_tree.go",
    "content": "/*\n226. Invert Binary Tree\nhttps://leetcode.com/problems/invert-binary-tree/\n\nInvert a binary tree.\n*/\n\npackage invertbinarytree\n\n// TreeNode binary tree node\ntype TreeNode struct {\n\tVal   int\n\tLeft  *TreeNode\n\tRight *TreeNode\n}\n\n// Recursion\n// Time complexity: O(n), where n is the node's number of the tree.\n// Space complexity: O(h), where h is the height of the tree.\nfunc invertTree(root *TreeNode) *TreeNode {\n\tif root == nil {\n\t\treturn nil\n\t}\n\troot.Right, root.Left = root.Left, root.Right\n\tinvertTree(root.Left)\n\tinvertTree(root.Right)\n\treturn root\n}\n"
  },
  {
    "path": "solutions/0226_invert_binary_tree/invert_binary_tree_test.go",
    "content": "package invertbinarytree\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc createBinaryTree(nums []interface{}) *TreeNode {\n\treturn performCreate(nums, 0)\n}\n\nfunc performCreate(nums []interface{}, index int) *TreeNode {\n\tif !(index < len(nums)) || nums[index] == nil {\n\t\treturn nil\n\t}\n\n\ttree := TreeNode{}\n\tif num, ok := nums[index].(int); ok { // type assertion\n\t\ttree.Val = num\n\t}\n\ttree.Left = performCreate(nums, 2*index+1)\n\ttree.Right = performCreate(nums, 2*index+2)\n\treturn &tree\n}\n\nfunc levelOrderTraversal(root *TreeNode) []interface{} {\n\tres := make([]interface{}, 0)\n\tqueue := make([]*TreeNode, 0)\n\tqueue = append(queue, root)\n\tfor len(queue) > 0 {\n\t\tnode := queue[0]\n\t\tqueue = queue[1:]\n\t\tres = append(res, node.Val)\n\t\tif node.Left != nil {\n\t\t\tqueue = append(queue, node.Left)\n\t\t}\n\t\tif node.Right != nil {\n\t\t\tqueue = append(queue, node.Right)\n\t\t}\n\t}\n\treturn res\n}\n\nfunc TestInvertTree(t *testing.T) {\n\ttestData := [][]interface{}{\n\t\t{4, 2, 7, 1, 3, 6, 9},\n\t}\n\n\texpectedData := [][]interface{}{\n\t\t{4, 7, 2, 9, 6, 3, 1},\n\t}\n\n\tfor index, data := range testData {\n\t\ttree := createBinaryTree(data)\n\t\tif res := levelOrderTraversal(invertTree(tree)); !reflect.DeepEqual(res, expectedData[index]) {\n\t\t\tt.Errorf(\"expected %v, got %v\", expectedData[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0235_lowest_common_ancestor_of_a_binary_search_tree/lcaoabst.go",
    "content": "/*\n235. Lowest Common Ancestor of a Binary Search Tree\nhttps://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/\n\nGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.\n\nAccording to the definition of LCA on Wikipedia:\n“The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has\nboth p and q as descendants (where we allow a node to be a descendant of itself).”\n\nNote:\nAll of the nodes' values will be unique.\np and q are different and both values will exist in the BST.\n*/\n// time: 2019-01-07\n\npackage lcaoabst\n\n// TreeNode Definition for TreeNode.\ntype TreeNode struct {\n\tVal   int\n\tLeft  *TreeNode\n\tRight *TreeNode\n}\n\n// recursive\n// time complexity: O(log n), where n is the nodes number of the tree.\n// space complexity: O(h), where h is the height of the tree.\nfunc lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {\n\tif root == nil {\n\t\treturn root\n\t}\n\n\tif root.Val > p.Val && root.Val > q.Val {\n\t\treturn lowestCommonAncestor(root.Left, p, q)\n\t}\n\n\tif root.Val < p.Val && root.Val < q.Val {\n\t\treturn lowestCommonAncestor(root.Right, p, q)\n\t}\n\treturn root\n}\n"
  },
  {
    "path": "solutions/0235_lowest_common_ancestor_of_a_binary_search_tree/lcaoabst_test.go",
    "content": "package lcaoabst\n\nimport (\n\t\"testing\"\n)\n\nfunc TestLowestCommonAncestor(t *testing.T) {\n\ttype arg struct {\n\t\troot, p, q *TreeNode\n\t}\n\n\ttestCases := []arg{\n\t\t{root: createBinaryTree([]int{6, 2, 8, 0, 4, 7, 9, -1, 1, 3, 5}), p: &TreeNode{Val: 3}, q: &TreeNode{Val: 5}},\n\t\t{p: &TreeNode{Val: 2}, q: &TreeNode{Val: 8}},\n\t}\n\texpected := []*TreeNode{{Val: 4}, nil}\n\n\tfor index, data := range testCases {\n\t\tif res := lowestCommonAncestor(data.root, data.p, data.q); res != nil && res.Val != expected[index].Val {\n\t\t\tt.Errorf(\"expected %v, got %v\", expected[index], res)\n\t\t} else if res == nil && res != expected[index] {\n\t\t\tt.Errorf(\"expected %v, got %v\", expected[index], res)\n\t\t}\n\t}\n}\n\nfunc createBinaryTree(nums []int) *TreeNode {\n\treturn performCreate(nums, 0)\n}\n\nfunc performCreate(nums []int, index int) *TreeNode {\n\tif index >= len(nums) {\n\t\treturn nil\n\t}\n\troot := &TreeNode{Val: nums[index]}\n\troot.Left = performCreate(nums, 2*index+1)\n\troot.Right = performCreate(nums, 2*index+2)\n\treturn root\n}\n"
  },
  {
    "path": "solutions/0236_Lowest_Common_Ancestor_of_a_Binary_Tree/lca.go",
    "content": "/*\n236. Lowest Common Ancestor of a Binary Tree\nhttps://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/\n*/\n// 2019-02-21\n\npackage lca\n\n// TreeNode Definition for TreeNode.\ntype TreeNode struct {\n\tVal   int\n\tLeft  *TreeNode\n\tRight *TreeNode\n}\n\nfunc lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {\n\tif root == nil || root == p || root == q {\n\t\treturn root\n\t}\n\tleft := lowestCommonAncestor(root.Left, p, q)\n\tright := lowestCommonAncestor(root.Right, p, q)\n\n\tif left != nil && right != nil {\n\t\treturn root\n\t}\n\tif left != nil {\n\t\treturn left\n\t}\n\treturn right\n}\n"
  },
  {
    "path": "solutions/0236_Lowest_Common_Ancestor_of_a_Binary_Tree/lca_test.go",
    "content": "package lca\n\nimport \"testing\"\n\nfunc TestLCA(t *testing.T) {\n\ttype arg struct {\n\t\troot, p, q *TreeNode\n\t}\n\n\ttree := createBinaryTree([]int{6, 2, 8, 0, 4, 7, 9, -1, 1, 3, 5})\n\n\ttestCases := []arg{\n\t\t{root: tree, p: tree.Left.Right.Left, q: tree.Left.Right.Right},\n\t\t{p: &TreeNode{Val: 2}, q: &TreeNode{Val: 8}},\n\t}\n\texpected := []*TreeNode{{Val: 4}, nil}\n\n\tfor index, data := range testCases {\n\t\tif res := lowestCommonAncestor(data.root, data.p, data.q); res != nil && res.Val != expected[index].Val {\n\t\t\tt.Errorf(\"expected %v, got %v\", expected[index], res)\n\t\t} else if res == nil && res != expected[index] {\n\t\t\tt.Errorf(\"expected %v, got %v\", expected[index], res)\n\t\t}\n\t}\n}\n\nfunc createBinaryTree(nums []int) *TreeNode {\n\treturn performCreate(nums, 0)\n}\n\nfunc performCreate(nums []int, index int) *TreeNode {\n\tif index >= len(nums) {\n\t\treturn nil\n\t}\n\troot := &TreeNode{Val: nums[index]}\n\troot.Left = performCreate(nums, 2*index+1)\n\troot.Right = performCreate(nums, 2*index+2)\n\treturn root\n}\n"
  },
  {
    "path": "solutions/0237_delete_node_in_a_linked_list/dniall.go",
    "content": "/*\n237. Delete Node in a Linked List\nhttps://leetcode.com/problems/delete-node-in-a-linked-list/\n\nWrite a function to delete a node (except the tail) in a singly linked list, given only access to that node.\n\nNote:\nThe linked list will have at least two elements.\nAll of the nodes' values will be unique.\nThe given node will not be the tail and it will always be a valid node of the linked list.\nDo not return anything from your function.\n*/\n// time: 2019-01-07\n\npackage dniall\n\n// ListNode Definition for singly-linked list.\ntype ListNode struct {\n\tVal  int\n\tNext *ListNode\n}\n\n// time complexity: O(1)\n// space complexity: O(1)\nfunc deleteNode(node *ListNode) {\n\t// The linked list will have at least two elements.\n\t// All the nodes' values will be unique.\n\t// The given node will not be the tail and it will always be a valid node of the linked list.\n\tnode.Val = node.Next.Val\n\tnode.Next = node.Next.Next\n}\n"
  },
  {
    "path": "solutions/0237_delete_node_in_a_linked_list/dniall_test.go",
    "content": "package dniall\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc createSinglyLinkedList(nums []int) *ListNode {\n\thead := &ListNode{}\n\tcur := head\n\n\tfor _, num := range nums {\n\t\tcur.Next = &ListNode{Val: num}\n\t\tcur = cur.Next\n\t}\n\treturn head.Next\n}\n\nfunc TestDeleteNode(t *testing.T) {\n\troot := createSinglyLinkedList([]int{4, 5, 1, 9})\n\tnode := root.Next\n\texpected := createSinglyLinkedList([]int{4, 1, 9})\n\tif deleteNode(node); !reflect.DeepEqual(expected, root) {\n\t\tt.Errorf(\"expected %v, got %v\", expected, root)\n\t}\n}\n"
  },
  {
    "path": "solutions/0257_binary_tree_paths/binary_tree_paths.go",
    "content": "/*\n257. Binary Tree Paths\nhttps://leetcode.com/problems/binary-tree-paths/\n\nGiven a binary tree, return all root-to-leaf paths.\n\nNote: A leaf is a node with no children.\n*/\n// time: 2019-01-07\n\npackage btp\n\nimport \"strconv\"\n\n// TreeNode Definition for a binary tree node.\ntype TreeNode struct {\n\tVal   int\n\tLeft  *TreeNode\n\tRight *TreeNode\n}\n\n// recursive\n// time complexity: O(n), where n is the nodes number in the tree.\n// space complexity: O(h), where h is the height of the tree.\nfunc binaryTreePaths(root *TreeNode) []string {\n\tif root == nil {\n\t\treturn []string{}\n\t}\n\n\tvar res []string\n\n\tif root.Left == nil && root.Right == nil {\n\t\treturn append(res, strconv.Itoa(root.Val))\n\t}\n\n\tif root.Left != nil {\n\t\tfor _, i := range binaryTreePaths(root.Left) {\n\t\t\tres = append(res, strconv.Itoa(root.Val)+\"->\"+i)\n\t\t}\n\t}\n\tif root.Right != nil {\n\t\tfor _, i := range binaryTreePaths(root.Right) {\n\t\t\tres = append(res, strconv.Itoa(root.Val)+\"->\"+i)\n\t\t}\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0257_binary_tree_paths/binary_tree_paths_test.go",
    "content": "package btp\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc createBinaryTree(nums []int) *TreeNode {\n\treturn performCreate(nums, 0)\n}\n\nfunc performCreate(nums []int, index int) *TreeNode {\n\tif index >= len(nums) {\n\t\treturn nil\n\t}\n\n\ttree := TreeNode{Val: nums[index]}\n\ttree.Left = performCreate(nums, 2*index+1)\n\ttree.Right = performCreate(nums, 2*index+2)\n\treturn &tree\n}\n\nfunc TestBinaryTreePaths(t *testing.T) {\n\ttestCases := []*TreeNode{\n\t\tcreateBinaryTree([]int{1, 2, 3, 4, 5}),\n\t\tnil,\n\t}\n\texpected := [][]string{\n\t\t{\"1->2->4\", \"1->2->5\", \"1->3\"},\n\t\t{},\n\t}\n\tfor index, root := range testCases {\n\t\tif res := binaryTreePaths(root); !reflect.DeepEqual(res, expected[index]) {\n\t\t\tt.Errorf(\"expected %v, got %v\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0258_add_digits/add_digits.go",
    "content": "/*\n258. Add Digits\nhttps://leetcode.com/problems/add-digits/\n\nGiven a non-negative integer num, repeatedly add all its digits until the result has only one digit.\n*/\n// time: 2019-01-07\n\npackage ad\n\n// time complexity: O(\tlog num\t)\n// space complexity: O(1)\nfunc addDigits(num int) int {\n\t// num is a non-negative integer\n\tfor num > 9 {\n\t\tnum = performAdd(num)\n\t}\n\treturn num\n}\n\nfunc performAdd(num int) (res int) {\n\tfor num > 0 {\n\t\tres += num % 10\n\t\tnum /= 10\n\t}\n\treturn\n}\n\n// time complexity: O(1)\n// space complexity: O(1)\nfunc addDigits1(num int) int {\n\t/*\n\t\t0\t1\t2\t3\t4\t5\t6\t7\t8\t\t9\n\t\t0\t1\t2\t3\t4\t5\t6\t7\t8\t\t9\n\t\t10\t11\t12\t13\t14\t15\t16\t17\t18\t\t19\n\t\t1\t2\t3\t4\t5\t6\t7\t8\t9\t\t10/1\n\t\t20\t21\t22\t23\t24\t25\t26\t27\t28\t\t29\n\t\t2\t3\t4\t5\t6\t7\t8\t9\t10/1\t\t11/2\n\t*/\n\treturn (num-1)%9 + 1\n}\n"
  },
  {
    "path": "solutions/0258_add_digits/add_digits_test.go",
    "content": "package ad\n\nimport \"testing\"\n\nfunc TestAddDigits(t *testing.T) {\n\tnum := 38\n\texpected := 2\n\n\ttestFuncs := []func(int) int{\n\t\taddDigits,\n\t\taddDigits1,\n\t}\n\n\tfor _, function := range testFuncs {\n\t\tif res := function(num); res != expected {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected, res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0283_move_zeroes/move_zeroes.go",
    "content": "/*\n283. Move Zeroes\nhttps://leetcode.com/problems/move-zeroes/\n\nGiven an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.\n*/\n\npackage movezeroes\n\n// Time complexity: O(n)\n// Space complexity: O(1)\nfunc moveZeroes(nums []int) {\n\tvar k int\n\tfor i := range nums {\n\t\tif nums[i] != 0 {\n\t\t\tif k != i {\n\t\t\t\tnums[i], nums[k] = nums[k], nums[i]\n\t\t\t}\n\t\t\tk++\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0283_move_zeroes/move_zeroes2.go",
    "content": "/*\n283. Move Zeroes\nhttps://leetcode.com/problems/move-zeroes/\n\nGiven an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.\n*/\n\npackage movezeroes\n\n// Time complexity: O(n)\n// Space complexity: O(1)\nfunc moveZeroes2(nums []int) {\n\tvar k int\n\tfor i := range nums {\n\t\tif nums[i] != 0 {\n\t\t\tif k != i {\n\t\t\t\tnums[k] = nums[i]\n\t\t\t}\n\t\t\tk++\n\t\t}\n\t}\n\n\tfor i := k; i < len(nums); i++ {\n\t\tnums[i] = 0\n\t}\n}\n"
  },
  {
    "path": "solutions/0283_move_zeroes/move_zeroes2_test.go",
    "content": "package movezeroes\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestMoveZeroes2(t *testing.T) {\n\ttestData := [][]int{\n\t\t{0, 1, 0, 3, 12},\n\t}\n\n\texpectedData := [][]int{\n\t\t{1, 3, 12, 0, 0},\n\t}\n\n\tfor index, data := range testData {\n\t\tif moveZeroes2(data); !reflect.DeepEqual(data, expectedData[index]) {\n\t\t\tt.Errorf(\"expected %v, got %v\", expectedData[index], data)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0283_move_zeroes/move_zeroes_test.go",
    "content": "package movezeroes\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestMoveZeroes(t *testing.T) {\n\ttestData := [][]int{\n\t\t{0, 1, 0, 3, 12},\n\t}\n\n\texpectedData := [][]int{\n\t\t{1, 3, 12, 0, 0},\n\t}\n\n\tfor index, data := range testData {\n\t\tif moveZeroes(data); !reflect.DeepEqual(data, expectedData[index]) {\n\t\t\tt.Errorf(\"expected %v, got %v\", expectedData[index], data)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0300_longest_increasing_subsequence/lis.go",
    "content": "/*\n300. Longest Increasing Subsequence\nhttps://leetcode.com/problems/longest-increasing-subsequence/\n\nGiven an unsorted array of integers, find the length of longest increasing subsequence.\n\nExample:\n\nInput: [10,9,2,5,3,7,101,18]\nOutput: 4\nExplanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4.\nNote:\n\nThere may be more than one LIS combination, it is only necessary for you to return the length.\nYour algorithm should run in O(n2) complexity.\n\nFollow up:\n\tCould you improve it to O(n log n) time complexity?\n*/\n\npackage lis\n\nimport \"leetcode/utils\"\n\n// Dynamic Programming\n// TIme complexity: O(n^2)\n// Space Complexity: O(n)\nfunc lengthOfLIS(nums []int) int {\n\tn := len(nums)\n\tif n == 0 {\n\t\treturn 0\n\t}\n\n\tmemo := make([]int, n)\n\tfor i := 0; i < n; i++ {\n\t\tmemo[i] = 1\n\t}\n\n\tfor i := 1; i < n; i++ {\n\t\tfor j := 0; j < i; j++ {\n\t\t\tif nums[j] < nums[i] {\n\t\t\t\tmemo[i] = utils.CalcMaxInt(memo[i], 1+memo[j])\n\t\t\t}\n\t\t}\n\t}\n\treturn utils.CalcMaxInt(memo...)\n}\n"
  },
  {
    "path": "solutions/0300_longest_increasing_subsequence/lis_test.go",
    "content": "package lis\n\nimport (\n\t\"os\"\n\t\"testing\"\n)\n\nfunc TestLIS(t *testing.T) {\n\ttestData := [][]int{\n\t\t{10, 9, 2, 5, 3, 7, 101, 18},\n\t\t{},\n\t}\n\texpectedData := []int{4, 0}\n\n\tfor index, data := range testData {\n\t\tif res := lengthOfLIS(data); res != expectedData[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expectedData[index], res)\n\t\t}\n\t}\n}\n\nfunc TestMain(m *testing.M) {\n\tos.Exit(m.Run())\n}\n"
  },
  {
    "path": "solutions/0303_range_sum_query/rsqim.go",
    "content": "/*\n303. Range Sum Query - Immutable\nhttps://leetcode.com/problems/range-sum-query-immutable/\n\nGiven an integer array nums,\nfind the sum of the elements between indices i and j (i ≤ j), inclusive.\n*/\n// time: 2018-12-28\n\npackage rsqim\n\n// NumArray store sum of nums sub-list.\ntype NumArray struct {\n\tsum []int\n}\n\n// Constructor make NumArray instance.\nfunc Constructor(nums []int) NumArray {\n\tnA := NumArray{sum: make([]int, 1)}\n\tfor _, num := range nums {\n\t\tnA.sum = append(nA.sum, nA.sum[len(nA.sum)-1]+num)\n\t}\n\treturn nA\n}\n\n// SumRange find the sum of the elements between indices i and j(i <= j), inclusive.\nfunc (na *NumArray) SumRange(i int, j int) int {\n\tj++\n\treturn na.sum[j] - na.sum[i]\n}\n"
  },
  {
    "path": "solutions/0303_range_sum_query/rsqim_test.go",
    "content": "package rsqim\n\nimport \"testing\"\n\nfunc TestSumRange(t *testing.T) {\n\tnums := []int{-2, 0, 3, -5, 2, -1}\n\tranges := [][]int{\n\t\t{0, 2},\n\t\t{2, 5},\n\t\t{0, 5},\n\t}\n\n\texpected := []int{1, -1, -3}\n\n\tobj := Constructor(nums)\n\n\tfor index, section := range ranges {\n\t\tif res := obj.SumRange(section[0], section[1]); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0307_Range_Sum_Query_Mutable/range_sum_query_mut.go",
    "content": "/*\n307. Range Sum Query - Mutable\nhttps://leetcode.com/problems/range-sum-query-mutable/\n\nGiven an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.\n\nThe update(i, val) function modifies nums by updating the element at index i to val.\n\nNote:\nThe array is only modifiable by the update function.\nYou may assume the number of calls to update and sumRange function is distributed evenly.\n*/\n// time: 2019-01-29\n// 线段树时间复杂度为：O(log n)\n\npackage rsqm\n\n// NumArray 线段树\ntype NumArray struct {\n\tsegmentTree []int\n\tdata        []int\n}\n\n// Constructor 线段树生成函数\nfunc Constructor(nums []int) NumArray {\n\tna := NumArray{segmentTree: make([]int, len(nums)*4), data: nums[:]}\n\tna.buildSegmentTree(0, 0, len(na.data)-1)\n\treturn na\n}\n\nfunc (na *NumArray) buildSegmentTree(treeIndex, left, right int) {\n\t// 线段树处理的数据不能为空。\n\t//if right < left {\n\t//\treturn\n\t//}\n\tif left == right {\n\t\tna.segmentTree[treeIndex] = na.data[left]\n\t\treturn\n\t}\n\tmid := left + (right-left)>>1\n\tleftTreeIndex := 2*treeIndex + 1\n\trightTreeIndex := 2*treeIndex + 2\n\tna.buildSegmentTree(leftTreeIndex, left, mid)\n\tna.buildSegmentTree(rightTreeIndex, mid+1, right)\n\tna.segmentTree[treeIndex] = na.segmentTree[leftTreeIndex] + na.segmentTree[rightTreeIndex]\n}\n\n// Update 线段树更新操作。\nfunc (na *NumArray) Update(i int, val int) {\n\tna.setter(0, 0, len(na.data)-1, i, val)\n}\n\nfunc (na *NumArray) setter(treeIndex, left, right, index, val int) {\n\tif left == right {\n\t\tna.segmentTree[treeIndex] = val\n\t\treturn\n\t}\n\tmid := left + (right-left)>>1\n\tleftTreeIndex := 2*treeIndex + 1\n\trightTreeIndex := 2*treeIndex + 2\n\tif index >= mid+1 {\n\t\tna.setter(rightTreeIndex, mid+1, right, index, val)\n\t} else {\n\t\tna.setter(leftTreeIndex, left, mid, index, val)\n\t}\n\tna.segmentTree[treeIndex] = na.segmentTree[leftTreeIndex] + na.segmentTree[rightTreeIndex]\n}\n\n// SumRange 线段树查询\nfunc (na *NumArray) SumRange(i int, j int) int {\n\treturn na.query(0, 0, len(na.data)-1, i, j)\n}\n\nfunc (na *NumArray) query(treeIndex, left, right, queryL, queryR int) int {\n\tif left == queryL && right == queryR {\n\t\treturn na.segmentTree[treeIndex]\n\t}\n\tmid := left + (right-left)>>1\n\tleftTreeIndex := 2*treeIndex + 1\n\trightTreeIndex := 2*treeIndex + 2\n\tif queryL >= mid+1 {\n\t\treturn na.query(rightTreeIndex, mid+1, right, queryL, queryR)\n\t} else if queryR <= mid {\n\t\treturn na.query(leftTreeIndex, left, mid, queryL, queryR)\n\t} else {\n\t\treturn na.query(leftTreeIndex, left, mid, queryL, mid) + na.query(rightTreeIndex, mid+1, right, mid+1, queryR)\n\t}\n}\n"
  },
  {
    "path": "solutions/0307_Range_Sum_Query_Mutable/range_sum_query_mut_test.go",
    "content": "package rsqm\n\nimport \"testing\"\n\nfunc TestNumArray(t *testing.T) {\n\tnums := []int{1, 2, 3, 6, 7, 8, 9, 3, 4, 2, 5}\n\tna := Constructor(nums)\n\tif res := na.SumRange(4, 9); res != 33 {\n\t\tt.Errorf(\"expected %d, got %d\", 43, res)\n\t}\n\tna.Update(6, 10)\n\tif res := na.SumRange(4, 9); res != 34 {\n\t\tt.Errorf(\"expected %d, got %d\", 44, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0328_odd_even_linked_list/odd_even_linked_list.go",
    "content": "/*\n328. Odd Even Linked List\nhttps://leetcode.com/problems/odd-even-linked-list/\n\nGiven a singly linked list, group all odd nodes together followed by the even nodes.\nPlease note here we are talking about the node number and not the value in the nodes.\n\nYou should try to do it in place.\nThe program should run in O(1) space complexity and O(nodes) time complexity.\n\nNote:\nThe relative order inside both the even and odd groups should remain as it was in the input.\nThe first node is considered odd, the second node even and so on ...\n*/\n// time: 2019-01-07\n\npackage oddevenlinkedlist\n\n// ListNode Definition for singly-linked list.\ntype ListNode struct {\n\tVal  int\n\tNext *ListNode\n}\n\n// time complexity: O(n)\n// space complexity: O(1)\nfunc oddEvenList(head *ListNode) *ListNode {\n\tif head == nil {\n\t\treturn head\n\t}\n\tvar (\n\t\todd      = head\n\t\tevenHead = head.Next\n\t\teven     = evenHead\n\t)\n\tfor even != nil && even.Next != nil {\n\t\todd.Next = even.Next\n\t\todd = odd.Next\n\t\teven.Next = odd.Next\n\t\teven = even.Next\n\t}\n\todd.Next = evenHead\n\treturn head\n}\n"
  },
  {
    "path": "solutions/0328_odd_even_linked_list/odd_even_linked_list_test.go",
    "content": "package oddevenlinkedlist\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc createSinglyLinkedList(nums []int) *ListNode {\n\thead := &ListNode{}\n\tcur := head\n\n\tfor _, num := range nums {\n\t\tcur.Next = &ListNode{Val: num}\n\t\tcur = cur.Next\n\t}\n\treturn head.Next\n}\n\nfunc TestOddEvenList(t *testing.T) {\n\ttestCases := []*ListNode{\n\t\tcreateSinglyLinkedList([]int{2, 1, 3, 5, 6, 4, 7}),\n\t\tnil,\n\t}\n\texpected := []*ListNode{\n\t\tcreateSinglyLinkedList([]int{2, 3, 6, 7, 1, 5, 4}),\n\t\tnil,\n\t}\n\n\tfor index, head := range testCases {\n\t\tif res := oddEvenList(head); !reflect.DeepEqual(res, expected[index]) {\n\t\t\tt.Errorf(\"expected %v, got %v\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0343_integer_break/integer_break.go",
    "content": "/*\n343. Integer Break\nhttps://leetcode.com/problems/integer-break\n\nGiven a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.\n\nExample 1:\n\nInput: 2\nOutput: 1\nExplanation: 2 = 1 + 1, 1 × 1 = 1.\nExample 2:\n\nInput: 10\nOutput: 36\nExplanation: 10 = 3 + 3 + 4, 3 × 3 × 4 = 36.\nNote: You may assume that n is not less than 2 and not larger than 58.\n*/\n\npackage integerbreak\n\nimport \"leetcode/utils\"\n\n// recursion\n/*\nfunc integerBreak(n int) int {\n\tif n == 1 {\n\t\treturn 1\n\t}\n\n\tres := utils.MinInt\n\n\tfor i := 1; i < n; i++ {\n\t\tres, _ = utils.CalcMaxInt(res, i*(n-i), i*integerBreak(n-i))\n\t}\n\treturn res\n}\n*/\n\n// memory search\n/*\nfunc integerBreak(n int) int {\n\tmemo := make([]int, n+1)\n\n\tfor i := 0; i <= n; i++ {\n\t\tmemo[i] = -1\n\t}\n\treturn breakInt(n, memo)\n}\n\nfunc breakInt(n int, memo []int) int {\n\tif n == 1 {\n\t\treturn 1\n\t}\n\n\tif memo[n] != -1 {\n\t\treturn memo[n]\n\t}\n\n\tres := utils.MinInt\n\tfor i := 1; i < n; i++ {\n\t\tres, _ = utils.CalcMaxInt(res, i*(n-i), i*breakInt(n-i, memo))\n\t}\n\tmemo[n] = res\n\treturn res\n}\n*/\n\n// dynamic programming\n// Time complexity: O(n^2)\n// Space complexity: O(n+1)\nfunc integerBreak(n int) int {\n\t// memo[i]表示将数字i分割（至少分割成两部分）后得到的最大乘积.\n\tmemo := make([]int, n+1)\n\n\tmemo[1] = 1\n\n\tfor i := 2; i <= n; i++ {\n\t\t// 求解memo[i]\n\t\tfor j := 1; j <= i-1; j++ {\n\t\t\tmemo[i] = utils.CalcMaxInt(memo[i], j*(i-j), j*memo[i-j])\n\t\t}\n\t}\n\treturn memo[n]\n}\n"
  },
  {
    "path": "solutions/0343_integer_break/integer_break_test.go",
    "content": "package integerbreak\n\nimport \"testing\"\n\nfunc TestIntegerBreak(t *testing.T) {\n\ttestData := []int{2, 10, 34}\n\texpectedData := []int{1, 36, 236196}\n\n\tfor index, data := range testData {\n\t\tif res := integerBreak(data); res != expectedData[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expectedData[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0344_reverse_string/reverse_string.go",
    "content": "/*\n344. Reverse String\nhttps://leetcode.com/problems/reverse-string/\n\nWrite a function that takes a string as input and returns the string reversed.\n*/\n// time: 2018-12-26\n\npackage reversestring\n\n// double index\n// time complexity: O(n)\n// space complexity: O(1)\nfunc reverseString(s string) string {\n\tvar (\n\t\tl int\n\t\tr = len(s) - 1\n\t)\n\n\tfor r > l {\n\t\tcharL := s[l]\n\t\tcharR := s[r]\n\t\ts = s[:r] + string(charL) + s[r+1:]\n\t\ts = s[:l] + string(charR) + s[l+1:]\n\t\tr--\n\t\tl++\n\t}\n\treturn s\n}\n\n// double index\n// time complexity: O(n)\n// space complexity: O(n)\nfunc reverseString1(s string) string {\n\tsLen := len(s)\n\tbytes := []byte(s)\n\tfor i := 0; i < sLen/2; i++ {\n\t\tbytes[i], bytes[sLen-i-1] = bytes[sLen-i-1], bytes[i]\n\t}\n\treturn string(bytes)\n}\n"
  },
  {
    "path": "solutions/0344_reverse_string/reverse_string_test.go",
    "content": "package reversestring\n\nimport \"testing\"\n\nfunc TestReverseString(t *testing.T) {\n\ttestCases := []string{\n\t\t\"hello\",\n\t\t\"A man, a plan, a canal: Panama\",\n\t\t\"\",\n\t\t\"ab\",\n\t}\n\texpected := []string{\n\t\t\"olleh\",\n\t\t\"amanaP :lanac a ,nalp a ,nam A\",\n\t\t\"\",\n\t\t\"ba\",\n\t}\n\n\tfor index, data := range testCases {\n\t\tif res := reverseString(data); res != expected[index] {\n\t\t\tt.Errorf(\"expected %s, got %s\", expected[index], res)\n\t\t}\n\t}\n}\n\nfunc TestReverseString1(t *testing.T) {\n\ttestCases := []string{\n\t\t\"hello\",\n\t\t\"A man, a plan, a canal: Panama\",\n\t\t\"\",\n\t\t\"ab\",\n\t}\n\texpected := []string{\n\t\t\"olleh\",\n\t\t\"amanaP :lanac a ,nalp a ,nam A\",\n\t\t\"\",\n\t\t\"ba\",\n\t}\n\n\tfor index, data := range testCases {\n\t\tif res := reverseString1(data); res != expected[index] {\n\t\t\tt.Errorf(\"expected %s, got %s\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0345_reverse_vowels_of_a_string/reverse_vowels.go",
    "content": "/*\n345. Reverse Vowels of a String\nhttps://leetcode.com/problems/reverse-vowels-of-a-string/\n\nWrite a function that takes a string as input and reverse only the vowels of a string.\n\n*/\n// time: 2018-12-26\n\npackage reversevowels\n\n// time complexity: O(n)\n// space complexity: O(1)\nfunc reverseVowels(s string) string {\n\tvar (\n\t\tl int\n\t\tr = len(s) - 1\n\t)\n\n\tfor r > l {\n\t\tfor r >= 0 && !isVowel(s[r]) {\n\t\t\tr--\n\t\t}\n\n\t\tfor l < len(s) && !isVowel(s[l]) {\n\t\t\tl++\n\t\t}\n\n\t\tif l >= r {\n\t\t\tbreak\n\t\t}\n\t\tcharL := s[l]\n\t\tcharR := s[r]\n\t\ts = s[:r] + string(charL) + s[r+1:]\n\t\ts = s[:l] + string(charR) + s[l+1:]\n\t\tr--\n\t\tl++\n\t}\n\treturn s\n}\n\n// time complexity: O(n)\n// space complexity: O(n)\nfunc reverseVowels1(s string) string {\n\tbytes := []byte(s)\n\tvar (\n\t\tl int\n\t\tr = len(bytes) - 1\n\t)\n\tfor r > l {\n\t\tfor r >= 0 && !isVowel(s[r]) {\n\t\t\tr--\n\t\t}\n\t\tfor l < len(bytes) && !isVowel(s[l]) {\n\t\t\tl++\n\t\t}\n\t\tif l >= r {\n\t\t\tbreak\n\t\t}\n\n\t\tbytes[l], bytes[r] = bytes[r], bytes[l]\n\t\tl++\n\t\tr--\n\t}\n\treturn string(bytes)\n}\n\nfunc isVowel(char byte) bool {\n\tvowels := [...]byte{'a', 'o', 'e', 'i', 'u', 'A', 'O', 'E', 'I', 'U'}\n\n\tfor _, k := range vowels {\n\t\tif char == k {\n\t\t\treturn true\n\t\t}\n\t}\n\treturn false\n}\n"
  },
  {
    "path": "solutions/0345_reverse_vowels_of_a_string/reverse_vowels_test.go",
    "content": "package reversevowels\n\nimport \"testing\"\n\nfunc TestReverseVowels(t *testing.T) {\n\ttestCases := []string{\n\t\t\"hello\",\n\t\t\"leetcode\",\n\t\t\"aA\",\n\t\t\"a.b,.\",\n\t}\n\n\texpected := []string{\n\t\t\"holle\",\n\t\t\"leotcede\",\n\t\t\"Aa\",\n\t\t\"a.b,.\",\n\t}\n\n\tfor index, data := range testCases {\n\t\tif res := reverseVowels(data); res != expected[index] {\n\t\t\tt.Errorf(\"expected %s, got %s\", expected[index], res)\n\t\t}\n\t}\n}\n\nfunc TestReverseVowels1(t *testing.T) {\n\ttestCases := []string{\n\t\t\"hello\",\n\t\t\"leetcode\",\n\t\t\"aA\",\n\t\t\"a.b,.\",\n\t}\n\n\texpected := []string{\n\t\t\"holle\",\n\t\t\"leotcede\",\n\t\t\"Aa\",\n\t\t\"a.b,.\",\n\t}\n\n\tfor index, data := range testCases {\n\t\tif res := reverseVowels1(data); res != expected[index] {\n\t\t\tt.Errorf(\"expected %s, got %s\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0347_top_k_frequent_elements/topkfe.go",
    "content": "/*\n347. Top K Frequent Elements\nhttps://leetcode.com/problems/top-k-frequent-elements/\n\nGiven a non-empty array of integers, return the k most frequent elements.\n\nNote:\nYou may assume k is always valid, 1 ≤ k ≤ number of unique elements.\nYour algorithm's time complexity must be better than O(n log n), where n is the array's size.\n*/\n// time: 2019-01-08\n\npackage topkfe\n\nimport \"container/heap\"\n\n// using priority\n// time complexity: O(n log n)\n// space complexity: O(n)\nfunc topKFrequent(nums []int, k int) []int {\n\tcount := make(map[int]int)\n\tfor _, num := range nums {\n\t\tif _, ok := count[num]; !ok {\n\t\t\tcount[num] = 1\n\t\t} else {\n\t\t\tcount[num]++\n\t\t}\n\t}\n\n\tnums_ := make(Nums, 0)\n\n\tfor num, cnt := range count {\n\t\tnums_ = append(nums_, Num{Val: num, Count: cnt})\n\t}\n\theap.Init(&nums_)\n\tvar res []int\n\tfor i := 0; i < k; i++ {\n\t\tnum := heap.Pop(&nums_).(Num)\n\t\tres = append(res, num.Val)\n\t}\n\treturn res\n}\n\n// Num stores its value and frequency as Count.\ntype Num struct {\n\tVal   int\n\tCount int\n}\n\n// Nums struct for impl Interface\ntype Nums []Num\n\n// Len sort Interface\nfunc (n Nums) Len() int { return len(n) }\n\n// Swap sort Interface\nfunc (n Nums) Swap(i, j int) { n[i], n[j] = n[j], n[i] }\n\n// Less sort Interface\nfunc (n Nums) Less(i, j int) bool { return n[i].Count >= n[j].Count }\n\n// Push heap Interface\nfunc (n *Nums) Push(num interface{}) {\n\tm := num.(Num)\n\t*n = append(*n, m)\n}\n\n// Pop heap Interface\nfunc (n *Nums) Pop() interface{} {\n\tres := (*n)[len(*n)-1]\n\t*n = (*n)[:len(*n)-1]\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0347_top_k_frequent_elements/topkfe_test.go",
    "content": "package topkfe\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestTopKFrequent(t *testing.T) {\n\tnums := []int{1, 1, 1, 2, 2, 3}\n\tk := 2\n\texpected := []int{1, 2}\n\tif res := topKFrequent(nums, k); !reflect.DeepEqual(res, expected) {\n\t\tt.Errorf(\"expected %v, got %v\", expected, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0349_intersection_of_2_arrays/intersection_of_two_arrays.go",
    "content": "/*\n349. Intersection of Two Arrays\nhttps://leetcode.com/problems/intersection-of-two-arrays\n\nGiven two arrays, write a function to compute their intersection.\n\nExample 1:\n\nInput: nums1 = [1,2,2,1], nums2 = [2,2]\nOutput: [2]\nExample 2:\n\nInput: nums1 = [4,9,5], nums2 = [9,4,9,8,4]\nOutput: [9,4]\n\nNote:\n\tEach element in the result must be unique.\n\tThe result can be in any order.\n*/\n\npackage intersectionof2arrays\n\nimport \"leetcode/utils\"\n\nfunc intersection(nums1 []int, nums2 []int) []int {\n\tset1 := utils.NewSet()\n\tfor _, num := range nums1 {\n\t\tset1.Add(num)\n\t}\n\tset2 := utils.NewSet()\n\tfor _, num := range nums2 {\n\t\tset2.Add(num)\n\t}\n\n\tvar res []int\n\tfor item := range set1 {\n\t\tif set2.Contains(item) {\n\t\t\tif value, ok := item.(int); ok {\n\t\t\t\tres = append(res, value)\n\t\t\t}\n\t\t}\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0349_intersection_of_2_arrays/intersection_of_two_arrays_test.go",
    "content": "package intersectionof2arrays\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestIntersection(t *testing.T) {\n\tnums1 := []int{1, 2, 2, 1}\n\tnums2 := []int{2, 2}\n\texpectedData := []int{2}\n\n\tif res := intersection(nums1, nums2); !reflect.DeepEqual(res, expectedData) {\n\t\tt.Errorf(\"expected %v, got %v\", expectedData, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0350_intersection_of_two_arrays2/intersection_of_two_arrays2.go",
    "content": "/*\n350. Intersection of Two Arrays II\nhttps://leetcode.com/problems/intersection-of-two-arrays-ii/\n\n*/\n\npackage intersectionof2arrays2\n\nfunc intersect(nums1 []int, nums2 []int) []int {\n\trecord := make(map[int]int)\n\tres := make([]int, 0)\n\tfor _, num := range nums1 {\n\t\tif _, ok := record[num]; !ok {\n\t\t\trecord[num] = 1\n\t\t} else {\n\t\t\trecord[num]++\n\t\t}\n\t}\n\n\tfor _, num := range nums2 {\n\t\tif count, ok := record[num]; ok && count > 0 {\n\t\t\tres = append(res, num)\n\t\t\trecord[num]--\n\t\t}\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0350_intersection_of_two_arrays2/intersection_of_two_arrays2_test.go",
    "content": "package intersectionof2arrays2\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestInteract(t *testing.T) {\n\tnums1 := []int{1, 2, 2, 1}\n\tnums2 := []int{2, 2}\n\n\texpectedData := []int{2, 2}\n\n\tif res := intersect(nums1, nums2); !reflect.DeepEqual(res, expectedData) {\n\t\tt.Errorf(\"expected %v, got %v\", expectedData, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0376_wiggle_subsequence/wiggle_subsequence.go",
    "content": "/*\n376. Wiggle Subsequence\nhttps://leetcode.com/problems/wiggle-subsequence/\n\nA sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.\n\nFor example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.\n\nGiven a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.\n\nExample 1:\n\nInput: [1,7,4,9,2,5]\nOutput: 6\nExplanation: The entire sequence is a wiggle sequence.\nExample 2:\n\nInput: [1,17,5,10,13,15,10,5,16,8]\nOutput: 7\nExplanation: There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].\nExample 3:\n\nInput: [1,2,3,4,5,6,7,8,9]\nOutput: 2\n\nFollow up:\nCan you do it in O(n) time?\n*/\n\npackage wigglesubsequence\n\nimport \"leetcode/utils\"\n\n/*\n * 用up[i]和down[i]分别记录到第i个元素为止以上升沿和下降沿结束的最长“摆动”\n * 序列长度，遍历数组，如果nums[i]>nums[i-1]，表明第i-1到第i个元素是上升的，\n * 因此up[i]只需在down[i-1]的基础上加1即可，而down[i]保持down[i-1]不变；\n * 如果nums[i]<nums[i-1]，表明第i-1到第i个元素是下降的，因此down[i]\n * 只需在up[i-1]的基础上加1即可，而up[i]保持up[i-1]不变；如果nums[i]==nums[i-1]，\n * 则up[i]保持up[i-1]，down[i]保持down[i-1]。比较最终以上升沿和下降沿结束的\n * 最长“摆动”序列长度即可获取最终结果\n * */\n// dynamic programming\n// Time complexity: O(n^2)\n// Space complexity: O(2n) ==> O(n)\nfunc wiggleMaxLength(nums []int) int {\n\tn := len(nums)\n\n\tif n == 1 || n == 0 {\n\t\treturn n\n\t}\n\n\tup := make([]int, n)\n\tfor i := 0; i < n; i++ {\n\t\tup[i] = 1\n\t}\n\tdown := make([]int, n)\n\tcopy(down, up)\n\n\tfor i := 1; i < n; i++ {\n\t\tfor j := 0; j < i; j++ {\n\t\t\tif nums[i] > nums[j] {\n\t\t\t\tup[i] = utils.CalcMaxInt(up[i], down[j]+1)\n\t\t\t} else if nums[i] < nums[j] {\n\t\t\t\tdown[i] = utils.CalcMaxInt(down[i], up[j]+1)\n\t\t\t}\n\t\t}\n\t}\n\treturn utils.CalcMaxInt(up[n-1], down[n-1])\n}\n"
  },
  {
    "path": "solutions/0376_wiggle_subsequence/wiggle_subsequence_test.go",
    "content": "package wigglesubsequence\n\nimport \"testing\"\n\nfunc TestWiggleSubsequence(t *testing.T) {\n\ttestData := [][]int{\n\t\t{1, 17, 5, 10, 13, 15, 10, 5, 16, 8},\n\t\t{1, 7, 4, 9, 2, 5},\n\t\t{1, 2, 3, 4, 5, 6, 7, 8, 9},\n\t\t{},\n\t\t{2},\n\t\t{3, 4},\n\t\t{3, 3, 3, 2, 5},\n\t\t{\n\t\t\t372, 492, 288, 399, 81, 2, 320, 94, 416, 469, 427, 117, 265, 357, 399, 456, 496, 337, 355, 219, 475, 295, 457,\n\t\t\t350, 490, 470, 281, 127, 131, 36, 430, 412, 442, 174, 128, 253, 1, 56, 306, 295, 340, 73, 253, 130, 259, 223, 14,\n\t\t\t79, 409, 384, 209, 151, 317, 441, 156, 275, 140, 224, 128, 250, 290, 191, 161, 472, 477, 125, 470, 230, 321, 5, 311,\n\t\t\t23, 27, 248, 138, 284, 215, 356, 320, 194, 434, 136, 221, 273, 450, 440, 28, 179, 36, 386, 482, 203, 24, 8, 391, 21,\n\t\t\t500, 484, 135, 348, 292, 396, 145, 443, 406, 61, 212, 480, 455, 78, 309, 318, 84, 474, 209, 225, 177, 356, 227, 263,\n\t\t\t181, 476, 478, 151, 494, 395, 23, 114, 395, 429, 450, 247, 245, 150, 354, 230, 100, 172, 454, 155, 189, 33, 290, 187,\n\t\t\t443, 123, 59, 358, 241, 141, 39, 196, 491, 381, 157, 157, 134, 431, 295, 20, 123, 118, 207, 199, 317, 188, 267, 335, 315,\n\t\t\t308, 115, 321, 56, 52, 253, 492, 97, 374, 398, 272, 74, 206, 109, 172, 471, 55, 452, 452, 329, 367, 372, 252, 99, 62, 122,\n\t\t\t287, 320, 325, 307, 481, 316, 378, 87, 97, 457, 21, 312, 249, 354, 286, 196, 43, 170, 500, 265, 253, 19, 480, 438, 113,\n\t\t\t473, 247, 257, 33, 395, 456, 246, 310, 469, 408, 112, 385, 53, 449, 117, 122, 210, 286, 149, 20, 364, 372, 71, 26, 155,\n\t\t\t292, 16, 72, 384, 160, 79, 241, 346, 230, 15, 427, 96, 95, 59, 151, 325, 490, 223, 131, 81, 294, 18, 70, 171, 339, 14, 40,\n\t\t\t463, 421, 355, 123, 408, 357, 202, 235, 390, 344, 198, 98, 361, 434, 174, 216, 197, 274, 231, 85, 494, 57, 136, 258, 134,\n\t\t\t441, 477, 456, 318, 155, 138, 461, 65, 426, 162, 90, 342, 284, 374, 204, 464, 9, 280, 391, 491, 231, 298, 284, 82, 417, 355,\n\t\t\t356, 207, 367, 262, 244, 283, 489, 477, 143, 495, 472, 372, 447, 322, 399, 239, 450, 168, 202, 89, 333, 276, 199, 416, 490,\n\t\t\t494, 488, 137, 327, 113, 189, 430, 320, 197, 120, 71, 262, 31, 295, 218, 74, 238, 169, 489, 308, 300, 260, 397, 308, 328, 267,\n\t\t\t419, 84, 357, 486, 289, 312, 230, 64, 468, 227, 268, 28, 243, 267, 254, 153, 407, 399, 346, 385, 77, 297, 273, 484, 366, 482,\n\t\t\t491, 368, 221, 423, 107, 272, 98, 309, 426, 181, 320, 77, 185, 382, 478, 398, 476, 22, 328, 450, 299, 211, 285, 62, 344, 484, 395,\n\t\t\t466, 291, 487, 301, 407, 28, 295, 36, 429, 99, 462, 240, 124, 261, 387, 30, 362, 161, 156, 184, 188, 99, 377, 392, 442, 300, 98, 285,\n\t\t\t312, 312, 365, 415, 367, 105, 81, 378, 413, 43, 326, 490, 320, 266, 390, 53, 327, 75, 332, 454, 29, 370, 392, 360, 1, 335, 355, 344, 120,\n\t\t\t417, 455, 93, 60, 256, 451, 188, 161, 388, 338, 238, 26, 275, 340, 109, 185,\n\t\t},\n\t}\n\n\texpectedData := []int{\n\t\t7, 6, 2,\n\t\t0,\n\t\t1,\n\t\t2, 3, 334,\n\t}\n\n\tfor index, data := range testData {\n\t\tif res := wiggleMaxLength(data); res != expectedData[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expectedData[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0392_is_subsequence/is_subsequence.go",
    "content": "/*\n392. Is Subsequence\nhttps://leetcode.com/problems/is-subsequence/\n\nGiven a string s and a string t, check if s is subsequence of t.\n\nYou may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100).\n\nA subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, \"ace\" is a subsequence of \"abcde\" while \"aec\" is not).\n\nExample 1:\ns = \"abc\", t = \"ahbgdc\"\n\nReturn true.\n\nExample 2:\ns = \"axc\", t = \"ahbgdc\"\n\nReturn false.\n\nFollow up:\nIf there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?\n\nCredits:\nSpecial thanks to @pbrother for adding this problem and creating all test cases.\n*/\n\npackage issubsequence\n\n// greedy\n// Time complexity: O(n), where n is min(  len(s),   len(t)    )\n// Space complexity: O(1)\nfunc isSubsequence(s string, t string) bool {\n\tvar si, ti int\n\n\tfor si < len(s) && ti < len(t) {\n\t\tif s[si] == t[ti] {\n\t\t\tsi++\n\t\t\tti++\n\t\t} else {\n\t\t\tti++\n\t\t}\n\t}\n\treturn si == len(s)\n}\n"
  },
  {
    "path": "solutions/0392_is_subsequence/is_subsequence_test.go",
    "content": "package issubsequence\n\nimport \"testing\"\n\ntype args struct {\n\ts string\n\tt string\n}\n\nfunc TestAssignCookies(t *testing.T) {\n\ttestData := []args{\n\t\t{\n\t\t\ts: \"abc\",\n\t\t\tt: \"ahbgdc\",\n\t\t},\n\t\t{\n\t\t\ts: \"acb\",\n\t\t\tt: \"ahbgdc\",\n\t\t},\n\t}\n\texpectedData := []bool{true, false}\n\n\tfor index, data := range testData {\n\t\tif res := isSubsequence(data.s, data.t); res != expectedData[index] {\n\t\t\tt.Errorf(\"expected %t, got %t\", expectedData[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0404_sum_of_left_leaves/sum_of_left_leaves.go",
    "content": "/*\n404. Sum of Left Leaves\nhttps://leetcode.com/problems/sum-of-left-leaves/\n\nFind the sum of all left leaves in a given binary tree.\n*/\n// time: 2019-01-07\n\npackage soll\n\n// TreeNode Definition for a binary tree node.\ntype TreeNode struct {\n\tVal   int\n\tLeft  *TreeNode\n\tRight *TreeNode\n}\n\n// time complexity: O(n), where n is nodes number in the tree.\n// space complexity: O(h), where h is height of the tree.\nfunc sumOfLeftLeaves(root *TreeNode) int {\n\treturn performSum(root, false)\n}\n\nfunc performSum(root *TreeNode, isLeft bool) int {\n\tif root == nil {\n\t\treturn 0\n\t}\n\n\tif root.Left == nil && root.Right == nil && isLeft {\n\t\treturn root.Val\n\t}\n\n\treturn performSum(root.Left, true) + performSum(root.Right, false)\n}\n"
  },
  {
    "path": "solutions/0404_sum_of_left_leaves/sum_of_left_leaves_test.go",
    "content": "package soll\n\nimport \"testing\"\n\nfunc createBinaryTree(nums []int) *TreeNode {\n\treturn performCreate(nums, 0)\n}\n\nfunc performCreate(nums []int, index int) *TreeNode {\n\tif index >= len(nums) {\n\t\treturn nil\n\t}\n\n\ttree := TreeNode{Val: nums[index]}\n\ttree.Left = performCreate(nums, 2*index+1)\n\ttree.Right = performCreate(nums, 2*index+2)\n\treturn &tree\n}\n\nfunc TestSumOfLeftLeaves(t *testing.T) {\n\troot := createBinaryTree([]int{3, 9, 20, 15, 7, 2})\n\texpected := 17\n\n\tif res := sumOfLeftLeaves(root); res != expected {\n\t\tt.Errorf(\"expected %d, got %d\", expected, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0416_partition_equal_subset_sum/partition_equal_subset_sum.go",
    "content": "/*\n416. Partition Equal Subset Sum\nhttps://leetcode.com/problems/partition-equal-subset-sum/\n\nGiven a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.\n\nNote:\n\t1. Each of the array element will not exceed 100.\n\t2. The array size will not exceed 200.\n*/\n\npackage partitionequalsubsetsum\n\n/*\n此问题可以使用0-1背包问题的思路求解\n\nc是数组和的一半。\n\n状态定义：F(n c)\n\n状态转移方程：F(n c) = max(    F(n-1, c) ,     n + F(n-1, c-n)          )\n*/\nfunc canPartition(nums []int) bool {\n\tvar sum int\n\tfor _, num := range nums {\n\t\tsum += num\n\t}\n\n\tif sum%2 != 0 {\n\t\treturn false\n\t}\n\n\tc := sum / 2\n\tn := len(nums)\n\tmemo := make([]bool, c+1)\n\n\tfor i := 0; i <= c; i++ {\n\t\tmemo[i] = nums[0] == i\n\t}\n\n\tfor i := 0; i < n; i++ {\n\t\tfor j := c; j >= nums[i]; j-- {\n\t\t\tmemo[j] = memo[j] || memo[j-nums[i]]\n\t\t}\n\t}\n\treturn memo[c]\n}\n"
  },
  {
    "path": "solutions/0416_partition_equal_subset_sum/partition_equal_subset_sum_test.go",
    "content": "package partitionequalsubsetsum\n\nimport \"testing\"\n\nfunc TestPartitionEqualSubsetSum(t *testing.T) {\n\ttestData := [][]int{\n\t\t{1, 5, 11, 5},\n\t\t{1, 2, 3, 5},\n\t}\n\n\texpectedData := []bool{true, false}\n\n\tfor index, data := range testData {\n\t\tif res := canPartition(data); res != expectedData[index] {\n\t\t\tt.Errorf(\"expected %t, got %t\", expectedData[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0435_non_overlapping_intervals/dp_solution.go",
    "content": "/*\n435. Non-overlapping Intervals\nhttps://leetcode.com/problems/non-overlapping-intervals/\n\nGiven a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.\n\nNote:\n\t1. You may assume the interval's end point is always bigger than its start point.\n\t2. Intervals like [1,2] and [2,3] have borders \"touching\" but they don't overlap each other.\n*/\n\npackage nonoverlappingintervals\n\nimport (\n\t\"leetcode/utils\"\n\t\"sort\"\n)\n\nfunc eraseOverlapIntervalsDp(intervals []Interval) int {\n\tn := len(intervals)\n\tif n == 0 {\n\t\treturn 0\n\t}\n\n\tsort.Sort(IntervalSliceCompareWithStart(intervals))\n\n\t// memo[i]表示使用intervals[0...i]的区间能构成的最长不重叠区间序列\n\tmemo := make([]int, n)\n\tfor i := 0; i < n; i++ {\n\t\tmemo[i] = 1\n\t}\n\n\tfor i := 1; i < n; i++ {\n\t\t// memo[i]\n\t\tfor j := 0; j < i; j++ {\n\t\t\tif intervals[i].Start >= intervals[j].End {\n\t\t\t\tmemo[i] = utils.CalcMaxInt(memo[i], 1+memo[j])\n\t\t\t}\n\t\t}\n\t}\n\n\tvar res int\n\tfor i := 0; i < n; i++ {\n\t\tres = utils.CalcMaxInt(res, memo[i])\n\t}\n\treturn n - res\n}\n"
  },
  {
    "path": "solutions/0435_non_overlapping_intervals/greedy_solution.go",
    "content": "/*\n435. Non-overlapping Intervals\nhttps://leetcode.com/problems/non-overlapping-intervals/\n\nGiven a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.\n\nNote:\n\t1. You may assume the interval's end point is always bigger than its start point.\n\t2. Intervals like [1,2] and [2,3] have borders \"touching\" but they don't overlap each other.\n*/\n\npackage nonoverlappingintervals\n\nimport \"sort\"\n\nfunc eraseOverlapIntervalsGreedy(intervals []Interval) int {\n\tn := len(intervals)\n\tif n == 0 {\n\t\treturn 0\n\t}\n\n\tsort.Sort(IntervalSliceCompareWithEnd(intervals))\n\tvar (\n\t\tres = 1\n\t\tpre = 0\n\t)\n\n\tfor i := 1; i < n; i++ {\n\t\tif intervals[i].Start >= intervals[pre].End {\n\t\t\tres++\n\t\t\tpre = i\n\t\t}\n\t}\n\treturn n - res\n}\n"
  },
  {
    "path": "solutions/0435_non_overlapping_intervals/interval.go",
    "content": "package nonoverlappingintervals\n\n// Interval Definition for an interval.\ntype Interval struct {\n\tStart int\n\tEnd   int\n}\n\n// IntervalSliceCompareWithStart slice of Interval sort with start\ntype IntervalSliceCompareWithStart []Interval\n\n//  Len is the number of elements in the collection.\nfunc (i IntervalSliceCompareWithStart) Len() int { return len(i) }\n\n// Less compare Interval with start\nfunc (i IntervalSliceCompareWithStart) Less(j, k int) bool {\n\tif i[j].Start != i[k].Start {\n\t\treturn i[j].Start < i[k].Start\n\t}\n\treturn i[j].End < i[k].End\n}\n\nfunc (i IntervalSliceCompareWithStart) Swap(j, k int) { i[j], i[k] = i[k], i[j] }\n\n// IntervalSliceCompareWithEnd slice of Interval sort with end\ntype IntervalSliceCompareWithEnd []Interval\n\n// Len is the number of elements in the collection.\nfunc (i IntervalSliceCompareWithEnd) Len() int { return len(i) }\n\n// Less compare Interval with end\nfunc (i IntervalSliceCompareWithEnd) Less(j, k int) bool {\n\tif i[j].End != i[k].End {\n\t\treturn i[j].End < i[k].End\n\t}\n\treturn i[j].Start < i[k].Start\n}\n\nfunc (i IntervalSliceCompareWithEnd) Swap(j, k int) { i[j], i[k] = i[k], i[j] }\n"
  },
  {
    "path": "solutions/0435_non_overlapping_intervals/solution_test.go",
    "content": "package nonoverlappingintervals\n\nimport \"testing\"\n\nfunc TestEraseOverlapIntervals(t *testing.T) {\n\ttestData := [][]Interval{\n\t\t{\n\t\t\tInterval{Start: 1, End: 2},\n\t\t\tInterval{Start: 1, End: 2},\n\t\t\tInterval{Start: 1, End: 2},\n\t\t},\n\t\t{\n\t\t\tInterval{Start: 1, End: 2},\n\t\t\tInterval{Start: 2, End: 3},\n\t\t},\n\t\t{\n\t\t\tInterval{Start: 1, End: 2},\n\t\t\tInterval{Start: 2, End: 3},\n\t\t\tInterval{Start: 3, End: 4},\n\t\t\tInterval{Start: 1, End: 3},\n\t\t},\n\t}\n\texpectedData := []int{2, 0, 1}\n\n\ttestFuncs := []func([]Interval) int{\n\t\teraseOverlapIntervalsGreedy,\n\t\teraseOverlapIntervalsDp,\n\t}\n\n\tfor index, data := range testData {\n\t\tfor _, testFunc := range testFuncs {\n\t\t\tif res := testFunc(data); res != expectedData[index] {\n\t\t\t\tt.Errorf(\"expected %d, got %d\", expectedData[index], res)\n\t\t\t}\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0437_path_sum_3/path_sum_3.go",
    "content": "/*\n437. Path Sum III\nhttps://leetcode.com/problems/path-sum-iii/\n\nYou are given a binary tree in which each node contains an integer value.\n\nFind the number of paths that sum to a given value.\n\nThe path does not need to start or end at the root or a leaf,\nbut it must go downwards (traveling only from parent nodes to child nodes).\n\nThe tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.\n*/\n// 2019-01-08\n\npackage ps3\n\n// TreeNode Definition for a binary tree node.\ntype TreeNode struct {\n\tVal   int\n\tLeft  *TreeNode\n\tRight *TreeNode\n}\n\n// recursive\n// time complexity: O(n), where n is the nodes number of the tree.\n// space complexity: O(h), where h is the height of the tree.\nfunc pathSum(root *TreeNode, sum int) int {\n\tif nil == root {\n\t\treturn 0\n\t}\n\treturn findPath(root, sum) + pathSum(root.Left, sum) + pathSum(root.Right, sum)\n}\n\nfunc findPath(node *TreeNode, val int) int {\n\tif node == nil {\n\t\treturn 0\n\t}\n\n\tres := 0\n\tif val == node.Val {\n\t\tres += 1\n\t}\n\n\tres += findPath(node.Left, val-node.Val)\n\tres += findPath(node.Right, val-node.Val)\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0437_path_sum_3/path_sum_3_test.go",
    "content": "package ps3\n\nimport \"testing\"\n\nfunc createBinaryTree(nums []int) *TreeNode {\n\treturn performCreate(nums, 0)\n}\n\nfunc performCreate(nums []int, index int) *TreeNode {\n\tif index >= len(nums) {\n\t\treturn nil\n\t}\n\n\ttree := TreeNode{Val: nums[index]}\n\ttree.Left = performCreate(nums, 2*index+1)\n\ttree.Right = performCreate(nums, 2*index+2)\n\treturn &tree\n}\n\nfunc TestPathSum(t *testing.T) {\n\troot := createBinaryTree([]int{10, 5, -3, 3, 2, 9, 11, 3, -2, 3, 1})\n\tsum := 8\n\texpected := 3\n\n\tif res := pathSum(root, sum); res != expected {\n\t\tt.Errorf(\"expected %d, got %d\", expected, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0438_all_anagrams_in_a_string/all_anagrams_in_a_string.go",
    "content": "/*\n438. Find All Anagrams in a String\n\nSource: https://leetcode.com/problems/find-all-anagrams-in-a-string/submissions/\n\nGiven a string s and a non-empty string p, find all the start indices of p's anagrams in s.\n\nStrings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.\n\nThe order of output does not matter.\n\nExample 1:\n\nInput:\ns: \"cbaebabacd\" p: \"abc\"\n\nOutput:\n[0, 6]\n\nExplanation:\nThe substring with start index = 0 is \"cba\", which is an anagram of \"abc\".\nThe substring with start index = 6 is \"bac\", which is an anagram of \"abc\".\nExample 2:\n\nInput:\ns: \"abab\" p: \"ab\"\n\nOutput:\n[0, 1, 2]\n\nExplanation:\nThe substring with start index = 0 is \"ab\", which is an anagram of \"ab\".\nThe substring with start index = 1 is \"ba\", which is an anagram of \"ab\".\nThe substring with start index = 2 is \"ab\", which is an anagram of \"ab\".\n\nTime: 2018-12-17\n*/\n\npackage allanagramsinastring\n\n// Sliding Window\n// Time complexity: O(len(p) + len(s)) = O(n)\n// Space complexity: O(1)\nfunc findAnagrams(s string, p string) []int {\n\tvar (\n\t\tlenS  = len(s)\n\t\tlenP  = len(p)\n\t\tres   = []int{}\n\t\tfreqP = make([]int, 26)\n\t\tfreqS = make([]int, 26)\n\t\tleft  int\n\t\tright = -1 // Sliding window: s[left, right]\n\t)\n\n\tif lenS < lenP {\n\t\treturn res\n\t}\n\n\tfor i := 0; i < lenP; i++ {\n\t\tfreqP[p[i]-'a']++\n\t}\n\n\tfor right+1 < lenS {\n\t\tright++\n\t\tfreqS[s[right]-'a']++\n\t\tif right-left+1 > lenP {\n\t\t\tfreqS[s[left]-'a']--\n\t\t\tleft++\n\t\t}\n\t\tif right-left+1 == lenP && isSame(freqP, freqS) {\n\t\t\tres = append(res, left)\n\t\t}\n\t}\n\treturn res\n}\n\nfunc isSame(p []int, q []int) bool {\n\tfor i := 0; i < 26; i++ {\n\t\tif p[i] != q[i] {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n"
  },
  {
    "path": "solutions/0438_all_anagrams_in_a_string/all_anagrams_in_a_string_test.go",
    "content": "package allanagramsinastring\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestFindAnagrams(t *testing.T) {\n\ttype arg struct {\n\t\ts string\n\t\tp string\n\t}\n\tcases := []arg{\n\t\t{\n\t\t\ts: \"cbaebabacd\",\n\t\t\tp: \"abc\",\n\t\t},\n\t\t{\n\t\t\ts: \"abab\",\n\t\t\tp: \"ab\",\n\t\t},\n\t\t{\n\t\t\tp: \"abab\",\n\t\t\ts: \"ab\",\n\t\t},\n\t}\n\n\texpected := [][]int{\n\t\t{0, 6},\n\t\t{0, 1, 2},\n\t\t{},\n\t}\n\n\tfor index, args := range cases {\n\t\tif res := findAnagrams(args.s, args.p); !reflect.DeepEqual(res, expected[index]) {\n\t\t\tt.Errorf(\"expected %v, got %v\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0447_number_of_boomerangs/number_of_boomerangs.go",
    "content": "/*\n447. Number of Boomerangs\nhttps://leetcode.com/problems/number-of-boomerangs/\n\nGiven n points in the plane that are all pairwise distinct, a \"boomerang\" is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters).\n\nFind the number of boomerangs. You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive).\n*/\n\npackage numberofboomerangs\n\nimport (\n\t\"math\"\n)\n\n// Time complexity: O(n^2)\n// Space complexity: O(n)\nfunc numberOfBoomerangs(points [][]int) int {\n\tvar (\n\t\tres int\n\t\tn   = len(points)\n\t)\n\n\tfor i := 0; i < n; i++ {\n\t\trecord := make(map[int]int)\n\t\tfor j := 0; j < n; j++ {\n\t\t\tif j != i {\n\t\t\t\tdis := dis(points[i], points[j])\n\t\t\t\trecord[dis]++\n\t\t\t}\n\t\t}\n\n\t\tfor _, j := range record {\n\t\t\tres += j * (j - 1)\n\t\t}\n\t}\n\treturn res\n}\n\nfunc dis(point1 []int, point2 []int) int {\n\treturn int(math.Pow(float64(point1[0]-point2[0]), float64(2)) + math.Pow(float64(point1[1]-point2[1]), float64(2)))\n}\n"
  },
  {
    "path": "solutions/0447_number_of_boomerangs/number_of_boomerangs_test.go",
    "content": "package numberofboomerangs\n\nimport \"testing\"\n\nfunc TestNumberOfBoomerangs(t *testing.T) {\n\ttestData := [][]int{\n\t\t{489, 238}, {323, 460}, {853, 965}, {327, 426}, {264, 871}, {580, 947}, {362, 275}, {241, 772}, {967, 240}, {68, 847},\n\t\t{825, 703}, {922, 898}, {769, 217}, {23, 160}, {472, 802}, {755, 313}, {40, 78}, {125, 246}, {396, 452}, {672, 660},\n\t\t{323, 253}, {607, 37}, {880, 201}, {161, 847}, {441, 229}, {46, 266}, {284, 320}, {516, 53}, {889, 539}, {565, 713},\n\t\t{341, 320}, {26, 381}, {751, 504}, {979, 147}, {956, 652}, {807, 632}, {257, 767}, {669, 489}, {968, 831}, {336, 409},\n\t\t{60, 734}, {27, 697}, {54, 543}, {750, 944}, {82, 668}, {657, 423}, {988, 36}, {156, 91}, {540, 136}, {238, 496}, {140, 398},\n\t\t{128, 397}, {165, 150}, {238, 133}, {981, 926}, {894, 393}, {660, 921}, {90, 66}, {464, 193}, {10, 898}, {861, 20}, {321, 201},\n\t\t{408, 829}, {293, 948}, {965, 531}, {796, 457}, {929, 277}, {206, 446}, {427, 444}, {931, 760}, {370, 825}, {153, 30},\n\t\t{98, 244}, {449, 914}, {789, 811}, {812, 650}, {831, 485}, {203, 239}, {315, 496}, {539, 632}, {380, 336}, {442, 661},\n\t\t{613, 648}, {108, 392}, {93, 391}, {152, 815}, {217, 305}, {198, 667}, {901, 647}, {934, 690}, {458, 746}, {692, 642},\n\t\t{584, 896}, {233, 251}, {744, 773}, {235, 124}, {109, 677}, {786, 74}, {326, 246}, {466, 771}, {989, 618}, {586, 558},\n\t\t{923, 136}, {226, 177}, {783, 160}, {867, 594}, {258, 912}, {236, 842}, {808, 469}, {445, 552}, {242, 681}, {29, 703},\n\t\t{358, 167}, {777, 36}, {765, 595}, {807, 754}, {213, 746}, {313, 489}, {882, 539}, {666, 18}, {51, 885}, {612, 309},\n\t\t{149, 200}, {504, 957}, {669, 949}, {862, 264}, {630, 891}, {319, 341}, {410, 449}, {377, 175}, {44, 537}, {929, 610},\n\t\t{635, 242}, {99, 869}, {133, 117}, {887, 184}, {354, 851}, {846, 504}, {51, 350}, {813, 73}, {651, 675}, {337, 634},\n\t\t{918, 656}, {975, 328}, {105, 704}, {503, 502}, {241, 785}, {112, 876}, {27, 211}, {98, 513}, {680, 985}, {697, 386},\n\t\t{189, 895}, {890, 240}, {245, 56}, {313, 897}, {83, 2}, {531, 2}, {659, 858}, {682, 116}, {562, 538}, {618, 804}, {323, 730},\n\t\t{32, 702}, {293, 482}, {215, 325}, {468, 265}, {64, 657}, {160, 306}, {249, 406}, {362, 915}, {655, 446}, {917, 538},\n\t\t{800, 576}, {396, 482}, {45, 310}, {20, 15}, {466, 343}, {98, 851}, {46, 743}, {333, 261}, {421, 801}, {878, 485}, {810, 39},\n\t\t{791, 412}, {797, 154}, {327, 452}, {600, 244}, {342, 400}, {173, 90}, {234, 570}, {400, 255}, {585, 867}, {950, 683},\n\t\t{718, 996}, {779, 51}, {610, 200}, {205, 488}, {685, 367}, {879, 476}, {779, 676}, {982, 458}, {128, 934}, {703, 822},\n\t\t{686, 228}, {912, 921}, {798, 313}, {176, 735}, {180, 478}, {771, 898}, {475, 550}, {301, 437}, {750, 506}, {277, 787},\n\t\t{226, 157}, {615, 5}, {833, 598}, {816, 314}, {532, 519}, {136, 219}, {99, 49}, {492, 249}, {362, 20}, {984, 894}, {498, 755},\n\t\t{144, 325}, {657, 445}, {762, 407}, {304, 392}, {546, 530}, {549, 162}, {887, 734}, {760, 703}, {48, 644}, {574, 537},\n\t\t{215, 673}, {938, 707}, {922, 652}, {727, 259}, {546, 226}, {14, 42}, {551, 24}, {487, 666}, {783, 143}, {58, 330}, {673, 959},\n\t\t{492, 913}, {693, 604}, {616, 94}, {248, 191}, {631, 816}, {216, 569}, {523, 491}, {573, 603}, {750, 119}, {181, 116}, {513, 84},\n\t\t{140, 0}, {750, 924}, {496, 160}, {254, 521}, {119, 98}, {434, 165}, {702, 51}, {259, 302}, {594, 242}, {118, 810}, {163, 994},\n\t\t{653, 736}, {597, 403}, {207, 778}, {520, 720}, {862, 12}, {72, 965}, {936, 568}, {125, 542}, {442, 597}, {640, 876}, {762, 694},\n\t\t{279, 373}, {997, 225}, {967, 467}, {388, 130}, {461, 41}, {218, 410}, {445, 425}, {540, 317}, {497, 403}, {329, 569}, {720, 266}, {490, 197},\n\t\t{808, 932}, {146, 801}, {160, 260}, {495, 440}, {633, 844}, {17, 600}, {312, 405}, {82, 125}, {447, 300}, {536, 244}, {77, 76}, {561, 574}, {831, 890},\n\t\t{144, 903}, {508, 986}, {101, 669}, {918, 599}, {470, 78}, {860, 965}, {870, 845}, {810, 888}, {446, 122}, {645, 880}, {599, 92}, {181, 487}, {688, 610},\n\t\t{916, 249}, {185, 747}, {492, 681}, {3, 352}, {667, 456}, {21, 937}, {55, 491}, {15, 915}, {457, 238}, {761, 267}, {478, 559}, {741, 123},\n\t\t{439, 692}, {568, 972}, {180, 256}, {935, 96}, {858, 120}, {195, 702}, {801, 198}, {54, 820}, {654, 76}, {757, 62},\n\t\t{567, 772}, {977, 376}, {362, 90}, {995, 840}, {1, 88}, {316, 793}, {781, 884}, {765, 961}, {492, 700}, {57, 702},\n\t\t{172, 604}, {404, 325}, {803, 459}, {145, 809}, {887, 902}, {871, 454}, {27, 201}, {183, 741}, {643, 178}, {582, 645},\n\t\t{267, 250}, {438, 48}, {134, 555}, {361, 978}, {608, 770}, {681, 780}, {374, 437}, {106, 529}, {896, 603}, {339, 135},\n\t\t{858, 562}, {590, 885}, {115, 125}, {626, 759}, {303, 560}, {404, 922}, {810, 842}, {970, 296}, {397, 683}, {627, 5},\n\t\t{453, 308}, {138, 828}, {745, 596}, {709, 994}, {199, 48}, {129, 57}, {963, 71}, {294, 78}, {196, 273}, {189, 852},\n\t\t{833, 593}, {774, 996}, {787, 97}, {644, 537}, {780, 271}, {894, 234}, {579, 32}, {414, 677}, {628, 123}, {23, 180}, {524, 504},\n\t\t{589, 487}, {576, 884}, {917, 124}, {157, 107}, {976, 342}, {52, 103}, {690, 840}, {200, 335}, {377, 980}, {606, 271},\n\t\t{566, 538}, {656, 980}, {567, 636}, {456, 590}, {168, 980}, {94, 758}, {819, 22}, {994, 88}, {147, 503}, {195, 475}, {197, 600},\n\t\t{578, 888}, {792, 130}, {223, 169}, {463, 181}, {792, 29}, {719, 800}, {10, 286}, {789, 466}, {228, 957}, {798, 323},\n\t\t{715, 617}, {697, 61}, {705, 196}, {564, 253}, {672, 762}, {205, 602}, {650, 997}, {85, 225}, {518, 548}, {406, 662},\n\t\t{577, 478}, {463, 939}, {116, 252}, {757, 345}, {561, 555}, {20, 277}, {524, 717}, {690, 582}, {914, 255}, {187, 938},\n\t\t{17, 392}, {892, 19}, {741, 977}, {596, 259}, {525, 2}, {273, 455}, {832, 736}, {394, 949}, {340, 504}, {294, 902},\n\t\t{59, 314}, {531, 936}, {383, 221}, {870, 297}, {828, 57}, {587, 197}, {801, 480}, {568, 894}, {457, 164}, {153, 335},\n\t\t{519, 426}, {790, 351}, {515, 536}, {652, 207}, {40, 946}, {461, 452}, {612, 344}, {388, 996}, {918, 610}, {645, 746}, {19, 233}, {296, 820},\n\t\t{65, 864}, {66, 522}, {29, 219}, {209, 548}, {997, 351}, {251, 864}, {888, 904}, {72, 928}, {202, 885}, {732, 815}, {230, 472},\n\t\t{163, 148}, {82, 160}, {246, 101}, {745, 542}, {273, 810}, {407, 339},\n\t}\n\texpectedData := 1000\n\n\tif res := numberOfBoomerangs(testData); res != expectedData {\n\t\tt.Errorf(\"expected %d, got %d\", expectedData, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0454_4sum2/4sum2.go",
    "content": "/*\n454. 4Sum II\nhttps://leetcode.com/problems/4sum-ii/\n\nGiven four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.\n\nTo make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1.\n*/\n\npackage foursum2\n\n// Time complexity: O(n^2)\n// Space complexity: O(n^2)\nfunc fourSumCount(A []int, B []int, C []int, D []int) int {\n\tvar (\n\t\trecord = make(map[int]int)\n\t\tres    int\n\t)\n\n\tfor _, i := range A {\n\t\tfor _, j := range B {\n\t\t\trecord[i+j]++\n\t\t}\n\t}\n\n\tfor _, i := range C {\n\t\tfor _, j := range D {\n\t\t\tif s, ok := record[-i-j]; ok && s > 0 {\n\t\t\t\tres += s\n\t\t\t}\n\t\t}\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0454_4sum2/4sum2_test.go",
    "content": "package foursum2\n\nimport \"testing\"\n\nfunc TestFourSumCount(t *testing.T) {\n\tA := []int{1, 2}\n\tB := []int{-2, -1}\n\tC := []int{-1, 2}\n\tD := []int{0, 2}\n\n\texpectedData := 2\n\n\tif res := fourSumCount(A, B, C, D); res != expectedData {\n\t\tt.Errorf(\"expected %d, got %d\", expectedData, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0455_assign_cookies/assign_cookies.go",
    "content": "/*\n455. Assign Cookies\nhttps://leetcode.com/problems/assign-cookies/\n\nAssume you are an awesome parent and want to give your children some cookies.\nBut, you should give each child at most one cookie.\nEach child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with;\nand each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the child i, and the child i will be content.\nYour goal is to maximize the number of your content children and output the maximum number.\n\nNote:\n\t1. You may assume the greed factor is always positive.\n\t2. You cannot assign more than one cookie to one child.\n*/\n\npackage assigncookies\n\nimport \"sort\"\n\n// greedy\n// Time complexity: O(n), where n is min (  len(g), len(s)       )\n// Space complexity: O(1)\nfunc findContentChildren(g []int, s []int) int {\n\tsort.Sort(sort.Reverse(sort.IntSlice(s)))\n\tsort.Sort(sort.Reverse(sort.IntSlice(g)))\n\n\tvar (\n\t\tsi, gi, res int\n\t)\n\n\tfor gi < len(g) && si < len(s) {\n\t\tif s[si] >= g[gi] {\n\t\t\tres++\n\t\t\tgi++\n\t\t\tsi++\n\t\t} else {\n\t\t\tgi++\n\t\t}\n\t}\n\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0455_assign_cookies/assign_cookies_test.go",
    "content": "package assigncookies\n\nimport \"testing\"\n\ntype args struct {\n\tg []int\n\ts []int\n}\n\nfunc TestAssignCookies(t *testing.T) {\n\ttestData := []args{\n\t\t{\n\t\t\tg: []int{1, 2, 3},\n\t\t\ts: []int{1, 1},\n\t\t},\n\t\t{\n\t\t\tg: []int{1, 2},\n\t\t\ts: []int{1, 2, 3},\n\t\t},\n\t}\n\texpectedData := []int{1, 2}\n\n\tfor index, data := range testData {\n\t\tif res := findContentChildren(data.g, data.s); res != expectedData[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expectedData[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0557_reverse_words_in_a_string_3/reverse_words_in_a_string_3.go",
    "content": "/*\n557. Reverse Words in a String III\nhttps://leetcode.com/problems/reverse-words-in-a-string-iii/\n\nGiven a string, you need to reverse the order of characters in each word within a sentence\nwhile still preserving whitespace and initial word order.\n\nNote: In the string, each word is separated by single space and there will not be any extra space in the string.\n*/\n// time: 2019-01-08\n\npackage rwias3\n\nimport \"strings\"\n\n// split and reverse\n// time complexity: O(len(s))\n// space complexity: O(n)\nfunc reverseWords(s string) string {\n\twords := strings.Split(s, \" \")\n\n\tvar temp []string\n\tfor _, word := range words {\n\t\ttemp = append(temp, reverseString(word))\n\t}\n\treturn strings.Join(temp, \" \")\n}\n\nfunc reverseString(s string) string {\n\trunes := []rune(s)\n\n\tfor start, end := 0, len(runes)-1; start < end; start, end = start+1, end-1 {\n\t\trunes[start], runes[end] = runes[end], runes[start]\n\t}\n\treturn string(runes)\n}\n"
  },
  {
    "path": "solutions/0557_reverse_words_in_a_string_3/reverse_words_in_a_string_3_test.go",
    "content": "package rwias3\n\nimport \"testing\"\n\nfunc TestReverseWords(t *testing.T) {\n\ts := \"Let's take LeetCode contest\"\n\texpected := \"s'teL ekat edoCteeL tsetnoc\"\n\n\tif res := reverseWords(s); res != expected {\n\t\tt.Errorf(\"expected %s, got %s\", expected, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0674_longest_continuous_increasing_subsequence/lcis.go",
    "content": "/*\n674. Longest Continuous Increasing Subsequence\nhttps://leetcode.com/problems/longest-continuous-increasing-subsequence/\n\nGiven an unsorted array of integers,\nfind the length of longest continuous increasing subsequence (subarray).\n*/\n// time: 2018-12-29\n\npackage lcis\n\n// time complexity: O(n), where n is nums's length.\n// space complexity: O(1)\nfunc findLengthOfLCIS(nums []int) int {\n\tn := len(nums)\n\tif 0 == n || 1 == n {\n\t\treturn n\n\t}\n\n\tvar res, l, r int\n\n\tfor r < n {\n\t\tif res < r-l+1 {\n\t\t\tres = r - l + 1\n\t\t}\n\t\tif r+1 < n && nums[r+1] <= nums[r] {\n\t\t\tl = r + 1\n\t\t}\n\t\tr++\n\t}\n\treturn res\n}\n\n// time complexity: O(n), where n is nums's length.\n// space complexity: O(1)\nfunc findLengthOfLCIS1(nums []int) int {\n\tn := len(nums)\n\tif 0 == n || 1 == n {\n\t\treturn n\n\t}\n\n\tvar (\n\t\tmaxLen int\n\t\tcount  = 1\n\t)\n\n\tfor i := 1; i < n; i++ {\n\t\tif nums[i] > nums[i-1] {\n\t\t\tcount++\n\t\t} else {\n\t\t\tcount = 1\n\t\t}\n\n\t\tif count > maxLen {\n\t\t\tmaxLen = count\n\t\t}\n\t}\n\treturn maxLen\n}\n"
  },
  {
    "path": "solutions/0674_longest_continuous_increasing_subsequence/lcis_test.go",
    "content": "package lcis\n\nimport \"testing\"\n\nfunc TestFindLengthOfLCIS(t *testing.T) {\n\ttestCases := [][]int{\n\t\t{1, 3, 5, 4, 7},\n\t\t{2, 2, 2, 2, 2},\n\t\t{},\n\t\t{2},\n\t}\n\ttestFuncs := []func([]int) int{\n\t\tfindLengthOfLCIS,\n\t\tfindLengthOfLCIS1,\n\t}\n\n\texpected := []int{3, 1, 0, 1}\n\tfor _, tfn := range testFuncs {\n\t\tfor index, data := range testCases {\n\t\t\tif res := tfn(data); res != expected[index] {\n\t\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t\t}\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0677_map_sum_pairs/map_sum_pairs.go",
    "content": "/*\n677. Map Sum Pairs\nhttps://leetcode.com/problems/map-sum-pairs/\n\nImplement a MapSum class with insert, and sum methods.\n\nFor the method insert, you'll be given a pair of (string, integer).\nThe string represents the key and the integer represents the value.\nIf the key already existed, then the original key-value pair will be overridden to the new one.\n\nFor the method sum, you'll be given a string representing the prefix,\nand you need to return the sum of all the pairs' value whose key starts with the prefix.\n*/\n// time: 2019-02-01\n\npackage mapsumpairs\n\ntype node struct {\n\tval  int\n\tnext map[rune]*node\n}\n\n// MapSum data structure for solution.\ntype MapSum struct {\n\troot *node\n}\n\n// Constructor initialize data structure here.\nfunc Constructor() MapSum {\n\treturn MapSum{&node{next: make(map[rune]*node)}}\n}\n\n// Insert inserts a word into the trie.\nfunc (ms *MapSum) Insert(key string, val int) {\n\tcur := ms.root\n\tfor _, c := range key {\n\t\tif _, ok := cur.next[c]; !ok {\n\t\t\tcur.next[c] = &node{next: make(map[rune]*node)}\n\t\t}\n\t\tcur = cur.next[c]\n\t}\n\tcur.val = val\n}\n\n// Sum sum of all the pairs' value whose key starts with the prefix.\nfunc (ms *MapSum) Sum(prefix string) int {\n\tcur := ms.root\n\tfor _, c := range prefix {\n\t\tif _, ok := cur.next[c]; !ok {\n\t\t\treturn 0\n\t\t}\n\t\tcur = cur.next[c]\n\t}\n\treturn sum(cur)\n}\n\nfunc sum(n *node) int {\n\tres := n.val\n\tfor _, nextNode := range n.next {\n\t\tres += sum(nextNode)\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0677_map_sum_pairs/map_sum_pairs_test.go",
    "content": "package mapsumpairs\n\nimport \"testing\"\n\nfunc TestMapSumPairs(t *testing.T) {\n\tobj := Constructor()\n\tobj.Insert(\"apple\", 3)\n\tif res := obj.Sum(\"ap\"); res != 3 {\n\t\tt.Errorf(\"expected %d, got %d\", 3, res)\n\t}\n\n\tobj.Insert(\"app\", 2)\n\tif res := obj.Sum(\"ap\"); res != 5 {\n\t\tt.Errorf(\"expected %d, got %d\", 5, res)\n\t}\n\n\tif res := obj.Sum(\"al\"); res != 0 {\n\t\tt.Errorf(\"expected %d, got %d\", 0, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0704_binary_search/binary_search.go",
    "content": "/*\n704. Binary Search\nhttps://leetcode.com/problems/binary-search/\n\nGiven a sorted (in ascending order) integer array nums of n elements and a target value,\nwrite a function to search target in nums. If target exists, then return its index, otherwise return -1.\n\nNote:\n\t1. You may assume that all elements in nums are unique.\n\t2. n will be in the range [1, 10000].\n\t3. The value of each element in nums will be in the range [-9999, 9999].\n*/\n\n// time: 2018-12-19\n\npackage binarysearch\n\n// iterative\n// Time complexity: O(log(n))\n// Space complexity: O(1)\nfunc search(nums []int, target int) int {\n\tvar (\n\t\tl int\n\t\tr = len(nums) - 1\n\t) // 在[l...r]区间寻找target\n\n\tfor l <= r { // 当l==r时，区间依然是有效的。\n\t\tmid := l + (r-l)/2 // (l+r)/2  可能会整型益处\n\t\tif nums[mid] == target {\n\t\t\treturn mid\n\t\t}\n\t\tif nums[mid] > target {\n\t\t\tr = mid - 1\n\t\t} else {\n\t\t\tl = mid + 1\n\t\t}\n\t}\n\treturn -1\n}\n"
  },
  {
    "path": "solutions/0704_binary_search/binary_search_test.go",
    "content": "package binarysearch\n\nimport \"testing\"\n\nfunc TestSearch(t *testing.T) {\n\ttype arg struct {\n\t\tnums   []int\n\t\ttarget int\n\t}\n\n\ttestCases := []arg{\n\t\t{\n\t\t\tnums:   []int{-1, 0, 3, 5, 9, 12},\n\t\t\ttarget: 9,\n\t\t},\n\t\t{\n\t\t\tnums:   []int{-1, 0, 3, 5, 9, 12},\n\t\t\ttarget: -3,\n\t\t},\n\t}\n\n\texpected := []int{4, -1}\n\n\tfor index, data := range testCases {\n\t\tif res := search(data.nums, data.target); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0713_subarray_product_less_than_k/spltk.go",
    "content": "/*\n713. Subarray Product Less Than K\nhttps://leetcode.com/problems/subarray-product-less-than-k/\n\nYour are given an array of positive integers nums.\n\nCount and print the number of (contiguous) subarrays\nwhere the product of all the elements in the subarray is less than k.\n*/\n\n// time: 2018-12-27\n\npackage spltk\n\n// sliding window\n// time complexity: O(n), where n = len(nums)\n// space complexity: O(1)\nfunc numSubArrayProductLessThanK(nums []int, k int) int {\n\tif k <= 1 {\n\t\treturn 0\n\t}\n\n\tvar (\n\t\tn    = len(nums)\n\t\tl, r int\n\t\tres  int\n\t\tprod = 1\n\t)\n\n\tfor l < n {\n\t\tif r < n && prod*nums[r] < k {\n\t\t\tprod *= nums[r]\n\t\t\tr++\n\t\t} else if l == r {\n\t\t\tl++\n\t\t\tr++\n\t\t} else {\n\t\t\tres += r - l\n\t\t\tprod /= nums[l]\n\t\t\tl++\n\t\t}\n\t}\n\treturn res\n}\n\n// 2019-06-16\nfunc numSubArrayProductLessThanK2(nums []int, k int) int {\n\tif k <= 1 {\n\t\treturn 0\n\t}\n\tvar (\n\t\tprod = 1\n\t\tres  = 0\n\t\tleft = 0\n\t)\n\n\tfor right, val := range nums {\n\t\tprod *= val\n\t\tfor prod >= k {\n\t\t\tprod /= nums[left]\n\t\t\tleft++\n\t\t}\n\t\tres += right - left + 1\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0713_subarray_product_less_than_k/spltk_test.go",
    "content": "package spltk\n\nimport \"testing\"\n\nfunc TestNumSubArrayProductLessThanK(t *testing.T) {\n\ttestCases := [][]int{\n\t\t{10, 5, 2, 6},\n\t\t{10, 5, 2, 6},\n\t\t{10, 5, 100, 6},\n\t}\n\n\tks := []int{100, 0, 100}\n\texpected := []int{8, 0, 4}\n\n\tfunctions := []func([]int, int) int{\n\t\tnumSubArrayProductLessThanK,\n\t\tnumSubArrayProductLessThanK2,\n\t}\n\tfor _, testFunc := range functions {\n\t\tfor index, data := range testCases {\n\t\t\tif res := testFunc(data, ks[index]); res != expected[index] {\n\t\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t\t}\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0717_1_bit_and_2_bit_characters/1bitand2bitc.go",
    "content": "/*\n717. 1-bit and 2-bit Characters\nhttps://leetcode.com/problems/1-bit-and-2-bit-characters/\n\nWe have two special characters.\nThe first character can be represented by one bit 0.\nThe second character can be represented by two bits (10 or 11).\n\nNow given a string represented by several bits.\nReturn whether the last character must be a one-bit character or not.\nThe given string will always end with a zero.\n\nNote:\n1 <= len(bits) <= 1000.\nbits[i] is always 0 or 1.\n*/\n// time: 2018-12-28\n\npackage onebitandtwobitcharacters\n\n// time complexity: O(n)\n// space complexity: O(1)\nfunc isOneBitCharacter(bits []int) bool {\n\tn := len(bits)\n\tif 1 == n {\n\t\treturn true\n\t}\n\n\tcur := 0\n\tflag := false\n\n\tfor cur < n {\n\t\tif 0 == bits[cur] {\n\t\t\tcur++\n\t\t} else {\n\t\t\tcur += 2\n\t\t}\n\t\tif cur == n-1 {\n\t\t\tflag = true\n\t\t}\n\t}\n\treturn flag\n}\n"
  },
  {
    "path": "solutions/0717_1_bit_and_2_bit_characters/1bitand2bitc_test.go",
    "content": "package onebitandtwobitcharacters\n\nimport \"testing\"\n\nfunc TestIsOneBitCharacter(t *testing.T) {\n\ttestCases := [][]int{\n\t\t{1, 0, 0},\n\t\t{1, 1, 1, 0},\n\t\t{0},\n\t}\n\texpected := []bool{true, false, true}\n\n\tfor index, bits := range testCases {\n\t\tif res := isOneBitCharacter(bits); res != expected[index] {\n\t\t\tt.Errorf(\"expected %t, got %t\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0728_self_dividing_numbers/self_dividing_numbers.go",
    "content": "/*\n728. Self Dividing Numbers\n\nSource: https://leetcode.com/problems/self-dividing-numbers/\n\nA self-dividing number is a number that is divisible by every digit it contains.\n\nFor example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.\n\nAlso, a self-dividing number is not allowed to contain the digit zero.\n\nGiven a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.\n\nExample 1:\nInput:\nleft = 1, right = 22\nOutput: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]\nNote:\n\nThe boundaries of each input argument are 1 <= left <= right <= 10000.\n\nTime: 2018-12-18\n*/\n\npackage selfdividingnumbers\n\nimport \"strconv\"\n\n//Time complexity: O(right-left+1)\n// Space complexity: O(1)\nfunc selfDividingNumbers(left int, right int) []int {\n\tres := make([]int, 0)\n\n\tfor num := left; num <= right; num++ {\n\t\tflag := true\n\t\tstrNum := strconv.Itoa(num)\n\n\t\tfor j := 0; j < len(strNum); j++ {\n\t\t\tif divisor := int(strNum[j] - '0'); divisor == 0 || num%divisor != 0 {\n\t\t\t\tflag = false\n\t\t\t}\n\t\t}\n\t\tif flag {\n\t\t\tres = append(res, num)\n\t\t}\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0728_self_dividing_numbers/self_dividing_numbers_test.go",
    "content": "package selfdividingnumbers\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestSelfDividingNumbers(t *testing.T) {\n\texpected := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22}\n\tif res := selfDividingNumbers(1, 22); !reflect.DeepEqual(res, expected) {\n\t\tt.Errorf(\"expected %v, got %v\", expected, res)\n\t}\n}\n"
  },
  {
    "path": "solutions/0735_asteroid_collision/ac.go",
    "content": "/*\n735. Asteroid Collision\nhttps://leetcode.com/problems/asteroid-collision/\n\nWe are given an array asteroids of integers representing asteroids in a row.\n\nFor each asteroid, the absolute value represents its size,\nand the sign represents its direction (positive meaning right, negative meaning left).\nEach asteroid moves at the same speed.\n\nFind out the state of the asteroids after all collisions.\nIf two asteroids meet, the smaller one will explode. If both are the same size, both will explode.\nTwo asteroids moving in the same direction will never meet.\n\nNote:\nThe length of asteroids will be at most 10000.\nEach asteroid will be a non-zero integer in the range [-1000, 1000]..\n*/\n// time: 2019-01-14\n\npackage ac\n\n// stack\n// time complexity:  O(N), where NN is the number of asteroids. Our stack pushes and pops each asteroid at most once.\n// space complexity: O(N), the size of stack.\nfunc asteroidCollision(asteroids []int) []int {\n\tstack := make([]int, 0)\n\n\tfor _, asteroid := range asteroids {\n\t\tflag := true\n\t\tfor len(stack) > 0 && asteroid < 0 && stack[len(stack)-1] > 0 {\n\t\t\tif stack[len(stack)-1] == -asteroid {\n\t\t\t\tstack = stack[:len(stack)-1]\n\t\t\t} else if stack[len(stack)-1] < -asteroid {\n\t\t\t\tstack = stack[:len(stack)-1]\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tflag = false\n\t\t\tbreak\n\t\t}\n\t\tif flag {\n\t\t\tstack = append(stack, asteroid)\n\t\t}\n\t}\n\treturn stack\n}\n"
  },
  {
    "path": "solutions/0735_asteroid_collision/ac_test.go",
    "content": "package ac\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestAsteroidCollision(t *testing.T) {\n\ttestCases := [][]int{\n\t\t{5, 10, -5},\n\t\t{-2, 2, -1, -2},\n\t\t{-2, -1, 1, 2},\n\t\t{8, -8},\n\t\t{10, 2, -5},\n\t}\n\n\texpected := [][]int{\n\t\t{5, 10},\n\t\t{-2},\n\t\t{-2, -1, 1, 2},\n\t\t{},\n\t\t{10},\n\t}\n\n\tfor index, asteroids := range testCases {\n\t\tif res := asteroidCollision(asteroids); !reflect.DeepEqual(res, expected[index]) {\n\t\t\tt.Errorf(\"expected %v, got %v\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0747_largest_number_at_least_twice_of_others/largest_number_at_least_twice_of_others.go",
    "content": "/*\n747. Largest Number At Least Twice of Others\n\nSource: https://leetcode.com/problems/largest-number-at-least-twice-of-others/\n\nIn a given integer array nums, there is always exactly one largest element.\n\nFind whether the largest element in the array is at least twice as much as every other number in the array.\n\nIf it is, return the index of the largest element, otherwise return -1.\n\nExample 1:\n\nInput: nums = [3, 6, 1, 0]\nOutput: 1\nExplanation: 6 is the largest integer, and for every other number in the array x,\n6 is more than twice as big as x.  The index of value 6 is 1, so we return 1.\n\n\nExample 2:\n\nInput: nums = [1, 2, 3, 4]\nOutput: -1\nExplanation: 4 isn't at least as big as twice the value of 3, so we return -1.\n\n\nNote:\n\n1. nums will have a length in the range [1, 50].\n2. Every nums[i] will be an integer in the range [0, 99].\n\n*/\n// 2018-12-18\n\npackage largestnumberatleasttwiceofothers\n\nimport \"leetcode/utils\"\n\n// Time complexity: O(n)\n// Space complexity: O(1)\nfunc dominantIndex(nums []int) int {\n\tvar (\n\t\tmaxNum = utils.MinInt\n\t\tres    int\n\t)\n\n\tfor i, j := range nums {\n\t\tif j > maxNum {\n\t\t\tmaxNum = j\n\t\t\tres = i\n\t\t}\n\t}\n\n\tfor _, j := range nums {\n\t\tif maxNum < 2*j && j != maxNum {\n\t\t\treturn -1\n\t\t}\n\t}\n\treturn res\n}\n"
  },
  {
    "path": "solutions/0747_largest_number_at_least_twice_of_others/largest_number_at_least_twice_of_others_test.go",
    "content": "package largestnumberatleasttwiceofothers\n\nimport \"testing\"\n\nfunc TestDominantIndex(t *testing.T) {\n\ttestCases := [][]int{\n\t\t{0, 0, 0, 1},\n\t\t{0, 0, 1, 1},\n\t\t{1, 2, 3, 4},\n\t\t{3, 6, 1, 0},\n\t}\n\texpected := []int{3, 2, -1, 1}\n\tfor index, data := range testCases {\n\t\tif res := dominantIndex(data); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/0872_leaf_similar_trees/leaf_similar_trees.go",
    "content": "/*\n872. Leaf-Similar Trees\nhttps://leetcode.com/problems/leaf-similar-trees/\n*/\n// time: 2019-01-08\n\npackage lst\n\nimport (\n\t\"reflect\"\n)\n\n// TreeNode Definition for a binary tree node.\ntype TreeNode struct {\n\tVal   int\n\tLeft  *TreeNode\n\tRight *TreeNode\n}\n\n// time complexity: O(n1 + n2), where n is nodes number in the tree.\n// space complexity: O(h1 + h2), where h is height of the tree.\nfunc leafSimilar(root1 *TreeNode, root2 *TreeNode) bool {\n\tl1 := make([]int, 0)\n\tl2 := make([]int, 0)\n\n\tdfs(root1, &l1)\n\tdfs(root2, &l2)\n\n\treturn reflect.DeepEqual(l1, l2)\n}\n\nfunc dfs(root *TreeNode, l *[]int) {\n\tif nil == root {\n\t\treturn\n\t}\n\n\tif root.Left == nil && root.Right == nil {\n\t\t*l = append(*l, root.Val)\n\t}\n\tdfs(root.Left, l)\n\tdfs(root.Right, l)\n}\n"
  },
  {
    "path": "solutions/0872_leaf_similar_trees/leaf_similar_trees_test.go",
    "content": "package lst\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc createBinaryTree(nums []int) *TreeNode {\n\treturn performCreate(nums, 0)\n}\n\nfunc performCreate(nums []int, index int) *TreeNode {\n\tif index >= len(nums) {\n\t\treturn nil\n\t}\n\n\ttree := TreeNode{Val: nums[index]}\n\ttree.Left = performCreate(nums, 2*index+1)\n\ttree.Right = performCreate(nums, 2*index+2)\n\treturn &tree\n}\n\nfunc TestLeafSimilar(t *testing.T) {\n\ttype arg struct {\n\t\troot1, root2 *TreeNode\n\t}\n\n\ttestCases := []arg{\n\t\t{root1: createBinaryTree([]int{3, 5, 1, 6, 2, 9, 8}), root2: createBinaryTree([]int{6, 4, 1, 6, 2, 9, 8})},\n\t\t{root1: createBinaryTree([]int{3, 5, 1, 6, 2, 9, 4}), root2: createBinaryTree([]int{6, 4, 1, 6, 2, 9, 8})},\n\t}\n\n\texpected := []bool{true, false}\n\n\tfor index, data := range testCases {\n\t\tif res := leafSimilar(data.root1, data.root2); !reflect.DeepEqual(res, expected[index]) {\n\t\t\tt.Errorf(\"expected %t, got %t\", expected[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/1021_Remove_Outermost_Parentheses/remove_outmost_parentheses.go",
    "content": "/*\n1021. Remove Outermost Parentheses\n\nA valid parentheses string is either empty (\"\"), \"(\" + A + \")\", or A + B, where A and B are valid parentheses strings, and + represents string concatenation.  For example, \"\", \"()\", \"(())()\", and \"(()(()))\" are all valid parentheses strings.\n\nA valid parentheses string S is primitive if it is nonempty, and there does not exist a way to split it into S = A+B, with A and B nonempty valid parentheses strings.\n\nGiven a valid parentheses string S, consider its primitive decomposition: S = P_1 + P_2 + ... + P_k, where P_i are primitive valid parentheses strings.\n\nReturn S after removing the outermost parentheses of every primitive string in the primitive decomposition of S.\n\n\n\nExample 1:\n\nInput: \"(()())(())\"\nOutput: \"()()()\"\nExplanation:\nThe input string is \"(()())(())\", with primitive decomposition \"(()())\" + \"(())\".\nAfter removing outer parentheses of each part, this is \"()()\" + \"()\" = \"()()()\".\nExample 2:\n\nInput: \"(()())(())(()(()))\"\nOutput: \"()()()()(())\"\nExplanation:\nThe input string is \"(()())(())(()(()))\", with primitive decomposition \"(()())\" + \"(())\" + \"(()(()))\".\nAfter removing outer parentheses of each part, this is \"()()\" + \"()\" + \"()(())\" = \"()()()()(())\".\nExample 3:\n\nInput: \"()()\"\nOutput: \"\"\nExplanation:\nThe input string is \"()()\", with primitive decomposition \"()\" + \"()\".\nAfter removing outer parentheses of each part, this is \"\" + \"\" = \"\".\n\n\nNote:\n\nS.length <= 10000\nS[i] is \"(\" or \")\"\nS is a valid parentheses string\n\n\n\n*/\n\n// time: 2020-05-04\n\npackage removeoutmostparentheses\n\n// time complexity: O(n), where n is length of S.\n// space complexity: O(1)\nfunc removeOuterParentheses(S string) string {\n\t// S is valid parentheses string, so, s is empty \"\" or starts with \"(\"\n\tif len(S) == 0 {\n\t\treturn S\n\t}\n\n\tvar stackLength, left int\n\n\tvar ret string\n\n\tfor i := 0; i < len(S); i++ {\n\t\tif stackLength == 0 {\n\t\t\tleft = i\n\t\t}\n\t\tif S[i] == '(' {\n\t\t\tstackLength++\n\t\t} else {\n\t\t\tstackLength--\n\t\t}\n\t\tif stackLength == 0 {\n\t\t\tret += S[left+1 : i]\n\t\t}\n\t}\n\treturn ret\n}\n"
  },
  {
    "path": "solutions/1021_Remove_Outermost_Parentheses/remove_outmost_parentheses_test.go",
    "content": "package removeoutmostparentheses\n\nimport (\n\t\"testing\"\n)\n\nfunc TestRemoveOuterParentheses(t *testing.T) {\n\ttestCases := []map[string]string{\n\t\t{\"case\": \"(()())(())\", \"expected\": \"()()()\"},\n\t\t{\"case\": \"(()())(())(()(()))\", \"expected\": \"()()()()(())\"},\n\t\t{\"case\": \"\", \"expected\": \"\"},\n\t\t{\"case\": \"()()\", \"expected\": \"\"},\n\t}\n\n\tfor _, testCase := range testCases {\n\t\tif testCase[\"expected\"] != removeOuterParentheses(testCase[\"case\"]) {\n\t\t\tt.Errorf(\"hello\")\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "solutions/148_Sort_List/sortlist.go",
    "content": "/*\n148. Sort List\nhttps://leetcode.com/problems/sort-list/\n*/\n// time: 2019-03-04\n\npackage sortlist\n\n// ListNode Definition for singly-linked list.\ntype ListNode struct {\n\tVal  int\n\tNext *ListNode\n}\n\n// merge sort\n// time complexity: O(n * log(n))\n// using recursion, the system stack is used.\nfunc sortList(head *ListNode) *ListNode {\n\tif head == nil || head.Next == nil {\n\t\treturn head\n\t}\n\n\tprev, slow, fast := head, head, head\n\tfor fast != nil && fast.Next != nil {\n\t\tprev = slow\n\t\tslow = slow.Next\n\t\tfast = fast.Next.Next\n\t}\n\tprev.Next = nil\n\treturn merge(sortList(head), sortList(slow))\n}\n\nfunc merge(headA, headB *ListNode) *ListNode {\n\tdummy := &ListNode{}\n\ttail := dummy\n\n\tfor headA != nil && headB != nil {\n\t\tif headA.Val > headB.Val {\n\t\t\ttail.Next = headB\n\t\t\theadB = headB.Next\n\t\t} else {\n\t\t\ttail.Next = headA\n\t\t\theadA = headA.Next\n\t\t}\n\t\ttail = tail.Next\n\t}\n\tif headA != nil {\n\t\ttail.Next = headA\n\t} else {\n\t\ttail.Next = headB\n\t}\n\treturn dummy.Next\n}\n"
  },
  {
    "path": "solutions/148_Sort_List/sortlist_test.go",
    "content": "package sortlist\n\nimport (\n\t\"reflect\"\n\t\"testing\"\n)\n\nfunc TestSortList(t *testing.T) {\n\thead := createSingleLinkedList([]int{-1, 5, 3, 4, 0})\n\texpected := createSingleLinkedList([]int{-1, 0, 3, 4, 5})\n\tif res := sortList(head); !reflect.DeepEqual(res, expected) {\n\t\tt.Errorf(\"expected %v, got %v\", expected, res)\n\t}\n}\n\nfunc createSingleLinkedList(nums []int) *ListNode {\n\tdummy := ListNode{}\n\tcur := &dummy\n\tfor _, val := range nums {\n\t\tcur.Next = &ListNode{Val: val}\n\t\tcur = cur.Next\n\t}\n\treturn dummy.Next\n}\n"
  },
  {
    "path": "solutions/304_Range_Sum_Query_2D/rsq.go",
    "content": "/*\n304. Range Sum Query 2D - Immutable\nhttps://leetcode.com/problems/range-sum-query-2d-immutable/\n*/\n// 2019-02-27\n\npackage rsq\n\n// NumMatrix 累计区域和的数组\ntype NumMatrix struct {\n\tdp [][]int\n}\n\n// Constructor 初始化构造函数\nfunc Constructor(matrix [][]int) NumMatrix {\n\tif len(matrix) == 0 || len(matrix[0]) == 0 {\n\t\treturn NumMatrix{}\n\t}\n\tnumMatrix := NumMatrix{dp: make([][]int, len(matrix)+1)}\n\tfor i := 0; i < len(numMatrix.dp); i++ {\n\t\tnumMatrix.dp[i] = make([]int, len(matrix[0])+1)\n\t}\n\n\tfor i := 1; i <= len(matrix); i++ {\n\t\tfor j := 1; j <= len(matrix[0]); j++ {\n\t\t\tnumMatrix.dp[i][j] = numMatrix.dp[i-1][j] + numMatrix.dp[i][j-1] - numMatrix.dp[i-1][j-1] + matrix[i-1][j-1]\n\t\t}\n\t}\n\treturn numMatrix\n}\n\n// SumRegion 求区域和\nfunc (nm *NumMatrix) SumRegion(row1 int, col1 int, row2 int, col2 int) int {\n\treturn nm.dp[row2+1][col2+1] - nm.dp[row1][col2+1] - nm.dp[row2+1][col1] + nm.dp[row1][col1]\n}\n"
  },
  {
    "path": "solutions/304_Range_Sum_Query_2D/rsq_test.go",
    "content": "package rsq\n\nimport \"testing\"\n\nfunc TestSunRegion(t *testing.T) {\n\tmatrix := [][]int{\n\t\t{3, 0, 1, 4, 2},\n\t\t{5, 6, 3, 2, 1},\n\t\t{1, 2, 0, 1, 5},\n\t\t{4, 1, 0, 1, 7},\n\t\t{1, 0, 3, 0, 5},\n\t}\n\n\tobj := Constructor(matrix)\n\n\ttestData := [][]int{\n\t\t{2, 1, 4, 3},\n\t\t{1, 1, 2, 2},\n\t\t{1, 2, 2, 4},\n\t}\n\texpected := []int{8, 11, 12}\n\n\tfor index, data := range testData {\n\t\tif res := obj.SumRegion(data[0], data[1], data[2], data[3]); res != expected[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expected[index], res)\n\t\t}\n\t}\n\n\tif res := Constructor([][]int{}); res.dp != nil {\n\t\tt.Errorf(\"expected nil, got %v\", res.dp)\n\t}\n\tif res := Constructor(make([][]int, 3)); res.dp != nil {\n\t\tt.Errorf(\"expected nil, got %v\", res.dp)\n\t}\n}\n"
  },
  {
    "path": "solutions/README.md",
    "content": "### Solutions for LeetCode algorithm problems, continually updating.\n\n|ID|Title && solution|Coefficient of difficulty|remarks|\n|:---:|:---|:---:|:---:|\n|0001|[Two Sum](0001_two_sum/twosum.go)|Easy|*`array;`*  *`lookup table`*|\n|0002|[Add Two Numbers](0002_add_two_numbers/add_two_numbers.go)|Medium|*`linked list`*|\n|0003|[Longest Substring Without Repeating Characters](0003_longest_substring_without_repeating_characters/longest_substring_without_repeating_characters.go)|Medium|*`sliding window`*|\n|0004|[4. Median of Two Sorted Arrays](0004_median_of_two_sorted_arrays/motsa.go)|Hard|*`binary search`*|\n|0007|[7. Reverse Integer](0007_reverse_integer/reverse_integer.go)|Easy|*`math`*|\n|0009|[9. Palindrome Number](0009_palindrome_number/palindrome_number.go)|Easy|*`math`*|\n|0011|[11. Container With Most Water](0011_container_with_most_water/container_with_most_water.go)|Medium|*`array;`*  *`double index`*|\n|0013|[13. Roman to Integer](0013_roman_to_integer/roman_to_integer.go)|Easy|*`math`*|\n|0014|[14. Longest Common Prefix](0014_longest_common_prefix/lcp.go)|Easy||\n|0015|[15. 3Sum](0015_3Sum/3sum.go)|Medium||\n|0017|[Letter Combinations of a Phone Number](0017_letter_combination_of_a_phone_number/letter_combination_of_phone_number.go)|Medium|*`tree`*|\n|0019|[19. Remove Nth Node From End of List](0019_remove_nth_node_from_end_of_list/remove_nth_node_from_end_of_list.go)|Medium|*`linked list`*|\n|0020|[Valid Parentheses](0020_valid_parentheses/valid_parentheses.go)|Easy|*`string;`* *`stack`*|\n|0021|[Merge Two Sorted Lists](0021_merge_two_sorted_lists/mergeTwoLists.go)|Easy|*`linked list`*|\n|0023|[23. Merge k Sorted Lists](0023_merge_k_sorted_lists/mksl.go)|Hard|*`linked list;`* *`heap`*|\n|0024|[24. Swap Nodes in Pairs](0024_swap_nodes_in_pairs/swap_nodes_in_pairs.go)|Medium|*`linked list`*|\n|0025|[Reverse Nodes in k-Group](./0025_reverse_nodes_in_k_group/reverse_node_k_group.go)|Hard|*`linked list`*|\n|0026|[Remove Duplicates from Sorted Array](0026_remove_duplicates_from_sorted_array/rdfsa.go)|Easy|*`array;`*  *`double index`*|\n|0027|[Remove Element](0027_remove_element/remove_element.go)|Easy|*`array`*|\n|0028|[28. Implement strStr()](0028_implement_strstr/implement_strstr.go)|Easy|*`double index`*|\n|0033|[Search in Rotated Sorted Array](0033_search_in_rotated_sorted_array/search_in_rotated_sorted_array.go)|Medium|*`binary search`*|\n|0034|[Find First and Last Position of Element in Sorted Array](0034_find_first_and_last_position_of_element_in_sorted_array/find_first_and_last_position_of_element_in_sorted_array.go)|Medium|*`binary search`*|\n|0035|[35. Search Insert Position](0035_search_insert_position/search_insert_position.go)|Easy|*`binary search`*|\n|0048|[48. Rotate Image](0048_rotate_image/rotate_image.go)|Medium|*`array`*|\n|0053|[53. Maximum Subarray](0053_maximum_subarray/maximum_subarray.go)|Easy|*`dynamic programming`*|\n|0058|[58. Length of Last Word](0058_length_of_last_word/len_of_last_word.go)|Easy||\n|0061|[Rotate List](./0061_rotate_list/rotate_list.go)|Medium|*`linked list`*|\n|0062|[Unique Paths](./0062_unique_paths/unique_paths.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|\n|0063|[Unique Paths 2](./0063_unique_paths_2/unique_paths2.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|\n|0064|[Minimum Path Sum](./0064_minimum_path_sum/minimum_path_sum.go)|Medium|*`dynamic programming;`* *` dfs`*|\n|0066|[66. Plus One](0066_plus_one/plus_one.go)|Easy|*`math`*|\n|0067|[add Binary](./0067_add_binary/add_binary.go)|Easy||\n|0069|[Sqrt(x)](0069_sqrtx/sqrtx.go)|Easy|*`binary search`*|\n|0070|[Climbing Stairs](./0070_climbing_stairs/climbing_stairs.go)|Easy|*`dynamic programming`*|\n|0075|[75. Sort Colors](0075_sort_colors/sort_colors.go)|Medium|*`sort`*|\n|0076|[Minimum Window Substring](./0076_minimum_window_substring/minimum_window_substring.go)|Hard|*`sliding window`*|\n|0077|[77. Combinations](0077_combinations/combinations.go)|Medium|*`backtracking;`* *`combine`*|\n|0079|[79. Word Search](0079_word_search/word_search.go)|Medium|*`backtracking;`* *`array`*|\n|0080|[80. Remove Duplicates from Sorted Array II](0080_remove_duplicates_from_sorted_array2/rdfsa2.go)|Medium|*`double index`*|\n|0082|[82. Remove Duplicates from Sorted List II](0082_remove_duplicates_from_sorted_list_2/rdfsl.go)|Medium|*`linked list`*|\n|0083|[83. Remove Duplicates from Sorted List](0083_remove_duplicates_from_sorted_list/rdfsl.go)|Easy|*`linked list`*|\n|0086|[86. Partition List](0086_partition_list/partition_list.go)|Medium|*`linked list`*|\n|0088|[88. Merge Sorted Array](0088_merge_sorted_array/msa.go)|Easy|*`sort`*|\n|0092|[92. Reverse Linked List II](0092_reverse_linked_list_2/reverse_linked_list2.go)|Medium|*`linked list`*|\n|0094|[Binary Tree Inorder Traversal](./0094_binary_tree_inorder_traversal/binary_tree_inorder_traversal.go)|Medium|*`binary tree`*|\n|0100|[Same Tree](./0100_same_tree/same_tree.go)|Easy|*`binary tree`*|\n|0101|[Symmetric Tree](./0101_symmetric_tree/symmetric_tree.go)|Easy|*`stack;`* *`recursion; `* *`iterative`*|\n|0102|[102. Binary Tree Level Order Traversal](0102_binary_tree_level_order_traversal/binary_tree_level_order_traversal.go)|Medium|*`binary tree;`* *`bfs`*|\n|0104|[104. Maximum Depth of Binary Tree](0104_maximun_depth_of_binary_tree/maxdobt.go)|Easy|*`binary tree`*|\n|0107|[Binary Tree Level Order Traversal II](./0107_binary_tree_level_order_traversal_2/binary_tree_level_order_traversal2.go)|Easy|*`binary tree`*|\n|0111|[Minimum Depth of Binary Tree](./0111_minimum_depth_of_binary_tree/minimum_depth_of_binary_tree.go)|Easy|*`binary tree`*|\n|0112|[Path Sum](./0112_path_sum/path_sum.go)|Easy|*`binary tree`*|\n|0120|[Triangle](./0120_triangle/triangle.go)|Medium|*`dynamic programming;`* *` dfs`*|\n|0121|[121. Best Time to Buy and Sell Stock](0121_best_time_to_buy_and_sell_stock/maxprofit.go)|Easy||\n|0122|[122. Best Time to Buy and Sell Stock II](0122_best_time_to_buy_and_sell_stock_2/maxprofit.go)|Easy|*`greedy`*|\n|0125|[Valid Palindrome](0125_valid_palindrome/valid_palindrome.go)|Easy||\n|0136|[136. Single Number](0136_single_number/single_number.go)|Easy|*`hash table;`* *`bit manipulation`*|\n|0144|[144. Binary Tree Preorder Traversal](0144_binary_tree_preorder_traversal/binary_tree_preorder_traversal.go)|Medium|*`binary tree`*|\n|0148|[148. Sort List](148_Sort_List/sortlist.go)|Medium|*`sort;`* *`linked list`*|\n|0150|[150. Evaluate Reverse Polish Notation](0150_evaluate_reverse_polish_notation/evaluate_reverse_polish_notation.go)|Medium|*`stack`*|\n|0153|[153. Find Minimum in Rotated Sorted Array](0153_find_minimum_in_rotated_sorted_array/fmirsa.go)|Medium|*`binary search`*|\n|0155|[155. Min Stack](0155_min_stack/min_stack.go)|Easy|*`stack`*|\n|0165|[165. Compare Version Numbers](0165_compare_version_numbers/compare_version_numbers.go)|Medium|*`string`*|\n|0167|[Two Sum II - Input array is sorted](./0167_two_sum2/two_sum2.go)|Easy|*`对撞指针(双索引)`*|\n|0179|[179. Largest Number](0179_largest_number/ln.go)|Medium|*`sort`*|\n|0198|[House Robber](./0198_house_robber/house_robber.go)|Easy|*`memory search;`* *`dynamic programming`*|\n|0200|[200. Number of Islands](0200_number_of_island/number_of_island.go)|Medium|*`dfs;`* *`bfs`*|\n|0203|[203. Remove Linked List Elements](0203_remove_linked_list_elements/remove_linked_list_elements.go)|Easy|*`linked list`*|\n|0206|[206. Reverse Linked List](0206_reverse_linked_list/reverse_linked_list.go)|Easy|*`linked list`*|\n|0208|[208. Implement Trie (Prefix Tree)](0208_implement_trie_prefix_tree/impltrie.go)|Medium|*`trie`*|\n|0209|[Minimum Size Subarray Sum](./0209_minimum_size_subarray_sum/minimum_size_subarray_sum.go)|Medium|*`sliding window`*|\n|0211|[211. Add and Search Word - Data structure design](0211_add_and_search_word/add_and_search_word.go)|Medium|*`trie`*|\n|0215|[215. Kth Largest Element in an Array](0215_kth_largest_element_in_an_array/kthleiaa.go)|Medium|*`sort`*|\n|0217|[217. Contains Duplicate](0217_contains_duplicate/contains_duplicate.go)|Easy|*`map`*|\n|0219|[219. Contains Duplicate II](0219_contains_duplicate_2/contains_duplicate_2.go)|Easy|*`map`*|\n|0226|[Invert Binary Tree](./0226_invert_binary_tree/invert_binary_tree.go)|Easy|*`recursion; `* *`binary tree`*|\n|0235|[235. Lowest Common Ancestor of a Binary Search Tree](0235_lowest_common_ancestor_of_a_binary_search_tree/lcaoabst.go)|Easy|*`recursion; `* *`binary tree`*|\n|0236|[236. Lowest Common Ancestor of a Binary Tree](0236_Lowest_Common_Ancestor_of_a_Binary_Tree/lca.go)|Medium|*`recursion; `* *`binary tree`*|\n|0237|[237. Delete Node in a Linked List](0237_delete_node_in_a_linked_list/dniall.go)|Easy|*`linked list`*|\n|0257|[257. Binary Tree Paths](0257_binary_tree_paths/binary_tree_paths.go)|Easy|*`binary tree`*|\n|0258|[258. Add Digits](0258_add_digits/add_digits.go)|Easy|*`math`*|\n|0283|[Move Zeroes(solution1)](./0283_move_zeroes/move_zeroes.go) <br/>  [Move Zeroes(solution2)](./0283_move_zeroes/move_zeroes2.go)|Easy|*`array`*|\n|0300|[Longest Increasing Subsequence](./0300_longest_increasing_subsequence/lis.go)|Medium|*`dp`*|\n|0303|[303. Range Sum Query - Immutable](0303_range_sum_query/rsqim.go)|Easy||\n|0304|[304. Range Sum Query 2D - Immutable](304_Range_Sum_Query_2D/rsq.go)|Medium|*`dp`*|\n|0307|[307. Range Sum Query - Mutable](0307_Range_Sum_Query_Mutable/range_sum_query_mut.go)|Medium|*`segment tree`*|\n|0328|[328. Odd Even Linked List](0328_odd_even_linked_list/odd_even_linked_list.go)|Medium|*`singly linked list`*|\n|0343|[Integer Break](./0343_integer_break/integer_break.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|\n|0344|[344. Reverse String](0344_reverse_string/reverse_string.go)|Easy|*`double index`*|\n|0345|[345. Reverse Vowels of a String](0345_reverse_vowels_of_a_string/reverse_vowels.go)|Easy|*`double index`*|\n|0347|[347. Top K Frequent Elements](0347_top_k_frequent_elements/topkfe.go)|Medium|*`map;`* *`heap;`* *`array`*|\n|0349|[Intersection of Two Arrays](./0349_intersection_of_2_arrays/intersection_of_two_arrays.go)|Easy|*`set`*|\n|0350| [Intersection of Two Arrays II](./0350_intersection_of_two_arrays2/intersection_of_two_arrays2.go)|Easy|*`map`*|\n|0376|[Wiggle Subsequence](./0376_wiggle_subsequence/wiggle_subsequence.go)|Medium|*`dp`*|\n|0392|[Is Subsequence](./0392_is_subsequence/is_subsequence.go)|Medium|*`greedy algorithm`*|\n|0404|[404. Sum of Left Leaves](0404_sum_of_left_leaves/sum_of_left_leaves.go)|Easy|*`binary tree`*|\n|0416|[Partition Equal Subset Sum](./0416_partition_equal_subset_sum/partition_equal_subset_sum.go)|Medium|*`dp;`* *`0-1 knapsack problem`*|\n|0435|[Non-overlapping Intervals(dp solution)](./0435_non_overlapping_intervals/dp_solution.go)   <br/>[Non-overlapping Intervals(greedy solution)](./0435_non_overlapping_intervals/greedy_solution.go)|Medium|*`dp;`* *`0-1 knapsack problem`*|\n|0437|[437. Path Sum III](0437_path_sum_3/path_sum_3.go)|Easy|*`binary tree`*|\n|0438|[ Find All Anagrams in a String](./0438_all_anagrams_in_a_string/all_anagrams_in_a_string.go)|Easy|*`sliding window`*|\n|0447|[Number of Boomerangs](./0447_number_of_boomerangs/number_of_boomerangs.go)|Easy||\n|0454|[4Sum II](./0454_4sum2/4sum2.go)|Medium||\n|0455|[Assign Cookies](./0455_assign_cookies/assign_cookies.go)|Easy|*`greedy algorithm`*|\n|0557|[557. Reverse Words in a String III](0557_reverse_words_in_a_string_3/reverse_words_in_a_string_3.go)|Easy|*`string`*|\n|0674|[674. Longest Continuous Increasing Subsequence](0674_longest_continuous_increasing_subsequence/lcis.go)|Easy||\n|0677|[677. Map Sum Pairs](0677_map_sum_pairs/map_sum_pairs.go)|Medium|*`trie`*|\n|0704|[Binary Search](0704_binary_search/binary_search.go)|Easy|*`binary search`*|\n|0713|[713. Subarray Product Less Than K](0713_subarray_product_less_than_k/spltk.go)|Medium|*`sliding window`*|\n|0717|[717. 1-bit and 2-bit Characters](0717_1_bit_and_2_bit_characters/1bitand2bitc.go)|Easy||\n|0728|[Self Dividing Numbers](./0728_self_dividing_numbers/self_dividing_numbers.go)|Easy||\n|0735|[735. Asteroid Collision](0735_asteroid_collision/ac.go)|Medium|*`stack`*|\n|0747|[Largest Number At Least Twice of Others](./0747_largest_number_at_least_twice_of_others/largest_number_at_least_twice_of_others.go)|Easy||\n|0872|[872. Leaf-Similar Trees](0872_leaf_similar_trees/leaf_similar_trees.go)|Easy|*`binary tree`*|\n|1021|[1021. Remove Outermost Parentheses](1021_Remove_Outermost_Parentheses/remove_outmost_parentheses.go)|Easy|*`stack`*|\n"
  },
  {
    "path": "ut.bash",
    "content": "#!/bin/bash\n\nfunction cleanTestCache() {\n    go clean -testcache;\n}\n\nfunction test_() {\n    go test ./...;\n}\n\ncase $1 in\n    clean) cleanTestCache;\n    ;;\nesac\n\ntest_;\n"
  },
  {
    "path": "utils/infinite.go",
    "content": "package utils\n\nconst (\n\t// MaxUint max unsigned int.\n\tMaxUint = ^uint(0)\n\t// MaxInt max int.\n\tMaxInt = int(MaxUint >> 1)\n\n\t// MinUint min unsigned int.\n\tMinUint = 0\n\n\t// MinInt min int\n\tMinInt = -MaxInt - 1\n)\n"
  },
  {
    "path": "utils/maxint.go",
    "content": "package utils\n\n// CalcMaxInt calc max int from multi nums.\nfunc CalcMaxInt(nums ...int) (res int) {\n\t// 此处也可使用堆排序，构建大顶堆，heapify之后直接取最大值.\n\tif len(nums) == 0 {\n\t\treturn 0\n\t}\n\n\tres = nums[0]\n\n\tfor _, num := range nums {\n\t\tif num > res {\n\t\t\tres = num\n\t\t}\n\t}\n\treturn\n}\n"
  },
  {
    "path": "utils/maxint_test.go",
    "content": "package utils\n\nimport \"testing\"\n\nfunc TestCalcMaxInt(t *testing.T) {\n\ttestData := [][]int{\n\t\t{},\n\t\t{3, 4, 67, 8},\n\t}\n\texpectedData := []int{0, 67}\n\n\tfor index, data := range testData {\n\t\tif res := CalcMaxInt(data...); res != expectedData[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expectedData[index], res)\n\t\t}\n\t}\n}\n\nfunc BenchmarkCalcMaxInt(b *testing.B) {\n\tdata := []int{3, 4, 67, 8}\n\tfor i := 0; i < b.N; i++ {\n\t\tCalcMaxInt(data...)\n\t}\n}\n"
  },
  {
    "path": "utils/minint.go",
    "content": "package utils\n\n// CalcMinInt calc min int from multi nums.\nfunc CalcMinInt(nums ...int) (res int) {\n\t// 此处也可使用堆排序，构建小顶堆，heapify之后直接取最小值.\n\tif len(nums) == 0 {\n\t\treturn 0\n\t}\n\n\tres = nums[0]\n\n\tfor _, num := range nums {\n\t\tif num < res {\n\t\t\tres = num\n\t\t}\n\t}\n\treturn\n}\n"
  },
  {
    "path": "utils/minint_test.go",
    "content": "package utils\n\nimport \"testing\"\n\nfunc TestCalcMinInt(t *testing.T) {\n\ttestData := [][]int{\n\t\t{3, 4, 67, 8},\n\t\t{},\n\t\t{54, 3, 12, 1},\n\t}\n\texpectedData := []int{3, 0, 1}\n\n\tfor index, data := range testData {\n\t\tif res := CalcMinInt(data...); res != expectedData[index] {\n\t\t\tt.Errorf(\"expected %d, got %d\", expectedData[index], res)\n\t\t}\n\t}\n}\n"
  },
  {
    "path": "utils/set.go",
    "content": "package utils\n\n// Exists 空结构体\nvar Exists = struct{}{}\n\n// Set data structure implement by go.\ntype Set map[interface{}]struct{}\n\n// NewSet 初始化set\nfunc NewSet(items ...interface{}) Set {\n\ts := make(Set)\n\ts.Add(items...)\n\treturn s\n}\n\n// Add 向set中添加元素\nfunc (s Set) Add(items ...interface{}) error {\n\tfor _, item := range items {\n\t\ts[item] = Exists\n\t}\n\treturn nil\n}\n\n// Contains 查看set中是否存在item\nfunc (s Set) Contains(item interface{}) bool {\n\t_, ok := s[item]\n\treturn ok\n}\n\n// Size  集合的大小\nfunc (s Set) Size() int {\n\treturn len(s)\n}\n\n// Clear 清空集合\nfunc (s *Set) Clear() {\n\t*s = make(Set)\n}\n\n// Equal 判断两个set是否相等\nfunc (s Set) Equal(other Set) bool {\n\tif s.Size() != other.Size() {\n\t\treturn false\n\t}\n\tfor key := range s {\n\t\tif !other.Contains(key) {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n\n//IsSubset 判断s是否是other的子集\nfunc (s Set) IsSubset(other Set) bool {\n\tif s.Size() > other.Size() {\n\t\treturn false\n\t}\n\tfor key := range s {\n\t\tif !other.Contains(key) {\n\t\t\treturn false\n\t\t}\n\t}\n\treturn true\n}\n"
  },
  {
    "path": "utils/set_test.go",
    "content": "package utils\n\nimport (\n\t\"testing\"\n\t\"unsafe\"\n)\n\nfunc TestEmptyStruct(t *testing.T) {\n\tif unsafe.Sizeof(Exists) != 0 {\n\t\tt.Error(\"Exists size must be zero.\")\n\t}\n}\n\nfunc TestContains(t *testing.T) {\n\tset := NewSet(3, 4)\n\tset.Add(5)\n\tif set.Contains(3) != true {\n\t\tt.Error(\"should contains 4.\")\n\t}\n\n\tif set.Contains(6) != false {\n\t\tt.Error(\"should not contains 6.\")\n\t}\n}\n\nfunc TestSize(t *testing.T) {\n\tset := NewSet(3, 4)\n\tset.Add(5)\n\tif set.Size() != 3 {\n\t\tt.Error(\"size should be 3.\")\n\t}\n}\n\nfunc TestEqual(t *testing.T) {\n\tset := NewSet(3, 4)\n\tset.Add(5)\n\n\tset1 := NewSet(3, 4, 5)\n\tif set.Equal(set1) != true {\n\t\tt.Error(\"set should equal with set1.\")\n\t}\n\tset1.Add(6)\n\tif set.Equal(set1) == true {\n\t\tt.Error(\"set shouldn't equal with set1.\")\n\t}\n}\n\nfunc TestIsSubset(t *testing.T) {\n\tset := NewSet(3, 4)\n\tset.Add(5)\n\n\tset1 := NewSet(3, 4, 5, 6)\n\n\tif set1.IsSubset(set) == true {\n\t\tt.Error(\"set1 shouldn't be set's subset.\")\n\t}\n\n\tif set.IsSubset(set1) == false {\n\t\tt.Error(\"set should be set1's subset.\")\n\t}\n}\n\nfunc TestClear(t *testing.T) {\n\tset := NewSet(3, 4)\n\tset.Clear()\n\tif set.Size() != 0 {\n\t\tt.Error(\"set should be clear.\")\n\t}\n}\n"
  }
]