├── src ├── reverse_linkedlist │ ├── README.md │ └── Solution.java ├── 3sum_smaller │ ├── Makefile │ └── README.md ├── 400_nth_digit │ ├── Makefile │ └── README.md ├── 403_frog_jump │ └── Makefile ├── bomb_enemy │ ├── Makefile │ └── README.md ├── cloneGraph │ └── Makefile ├── decode_string │ └── Makefile ├── flip_game_ii │ ├── Makefile │ └── README.md ├── insertionSortList │ └── README.md ├── is_subsequence │ └── Makefile ├── setMatrixZero │ ├── Makefile │ └── README.md ├── 263_ugly_number │ ├── Makefile │ └── README.md ├── 401_binary_watch │ └── Makefile ├── 402_remove_k_digits │ └── Makefile ├── elimination_game │ └── Makefile ├── evaluate_division │ └── Makefile ├── find_the_difference │ └── Makefile ├── integer_replacement │ └── Makefile ├── perfect_rectangle │ └── Makefile ├── random_pick_index │ ├── Makefile │ └── README.md ├── rotate_function │ └── Makefile ├── utf_8_validation │ └── Makefile ├── wiggle_sequence │ └── Makefile ├── 404_sum_of_left_leaves │ ├── Makefile │ └── README.md ├── buildTreePreOrderInOrder │ ├── Makefile │ └── README.md ├── contains_duplicate_ii │ ├── Makefile │ └── README.md ├── sortColors │ └── main.pyc ├── 405_convert_a_number_to_hex │ └── Makefile ├── 407_trapping_rain_water_ii │ └── Makefile ├── 408_valid_word_abbreviation │ └── Makefile ├── buildTreeInOrderPostOrder │ ├── Makefile │ └── README.md ├── find_the_duplicate_number │ ├── Makefile │ └── README.md ├── guess_number_higher_or_lower │ └── Makefile ├── longest_absolute_file_path │ └── Makefile ├── 406_queue_reconstruction_by_height │ └── Makefile ├── first_unique_character_in_a_string │ ├── Makefile │ └── README.md ├── guess_number_higher_or_lower_ii │ └── Makefile ├── 2328_number_of_increasing_path_in_a_grid │ └── Makefile ├── containerWithMostWater │ ├── main.pyc │ └── README.md ├── squareRoot │ ├── README.md │ └── main.py ├── implement_trie_prefix_tree │ └── README.md ├── power_of_two │ ├── README.md │ └── Solution.java ├── longest_substring_with_at_least_k_repeating_characters │ └── Makefile ├── implement_queue_using_stacks │ ├── Makefile │ └── README.md ├── 3Sum │ ├── Makefile │ └── README.md ├── anagram │ ├── Makefile │ ├── README.md │ └── solution.py ├── atoi │ └── Makefile ├── h_index │ └── Makefile ├── increasing_triplet_subsequence │ ├── README.md │ └── Makefile ├── largestRectangleHistogram │ ├── histogram.png │ └── histogram_area.png ├── two_sum │ ├── Makefile │ └── README.md ├── 3SumClosest │ ├── Makefile │ └── README.md ├── addBinary │ ├── Makefile │ ├── README.md │ └── main.py ├── add_digits │ ├── Makefile │ ├── main.cpp │ └── README.md ├── coin_change │ ├── Makefile │ └── README.md ├── flip_game │ └── Makefile ├── game_of_life │ └── Makefile ├── h_index_ii │ ├── Makefile │ └── README.md ├── mergeKSorted │ ├── Makefile │ └── README.md ├── nim_game │ └── Makefile ├── paint_fence │ ├── Makefile │ └── README.md ├── paint_house │ └── Makefile ├── sortedArrayToBST │ └── README.md ├── super_pow │ ├── Makefile │ └── README.md ├── wiggle_sort │ ├── Makefile │ └── README.md ├── word_pattern │ └── Makefile ├── add_two_numbers │ ├── Makefile │ └── README.md ├── additive_number │ └── Makefile ├── alien_dictionary │ └── Makefile ├── basic_calculator │ └── makefile ├── binary_tree_paths │ ├── Makefile │ └── README.md ├── bulb_switcher │ └── Makefile ├── bulls_and_cows │ └── Makefile ├── counting_bits │ └── Makefile ├── design_snake_game │ └── Makefile ├── design_twitter │ └── Makefile ├── first_bad_version │ ├── Makefile │ └── README.md ├── flatten_2d_vector │ ├── Makefile │ └── README.md ├── graph_valid_tree │ └── Makefile ├── house_robber_iii │ └── Makefile ├── integer_break │ ├── Makefile │ └── README.md ├── line_reflection │ ├── Makefile │ └── README.md ├── meeting_rooms │ ├── Makefile │ └── README.md ├── meeting_rooms_ii │ ├── Makefile │ └── README.md ├── missing_number │ ├── Makefile │ └── README.md ├── moving_zeroes │ ├── Makefile │ └── README.md ├── paint_house_ii │ └── Makefile ├── palindrome_pairs │ ├── Makefile │ └── README.md ├── patching_array │ └── Makefile ├── peeking_iterator │ └── Makefile ├── perfect_squares │ ├── Makefile │ └── README.md ├── power_of_four │ ├── Makefile │ └── README.md ├── power_of_three │ ├── Makefile │ └── README.md ├── range_addition │ └── Makefile ├── reverse_string │ ├── Makefile │ └── README.md ├── self_crossing │ └── Makefile ├── single_number_iii │ └── Makefile ├── sortList │ └── README.md ├── super_ugly_number │ ├── Makefile │ └── README.md ├── ugly_number_ii │ └── Makefile ├── valid_anagram │ ├── Makefile │ ├── README.md │ └── main.cpp ├── walls_and_gates │ └── Makefile ├── wiggle_sort_ii │ ├── Makefile │ └── README.md ├── word_pattern_ii │ ├── Makefile │ └── README.md ├── zigzag_iterator │ └── Makefile ├── best_meeting_point │ ├── Makefile │ └── README.md ├── create_maximum_number │ └── Makefile ├── design_hit_counter │ └── Makefile ├── design_tic_tac_toe │ └── Makefile ├── factor_combinations │ └── Makefile ├── find_the_celebrity │ └── Makefile ├── group_shifted_strings │ ├── Makefile │ └── README.md ├── largest_bst_subtree │ └── Makefile ├── logger_rate_limiter │ └── Makefile ├── minimum_height_trees │ └── Makefile ├── nested_list_weight_sum │ ├── Makefile │ └── README.md ├── number_of_digit_one │ ├── Makefile │ └── README.md ├── number_of_islands_ii │ └── Makefile ├── odd_even_linked_list │ ├── Makefile │ └── README.md ├── palindrome_linked_list │ ├── Makefile │ └── README.md ├── palindrome_permutation │ ├── Makefile │ └── README.md ├── plus_one_linked_list │ ├── Makefile │ └── README.md ├── reconstruct_itinerary │ └── Makefile ├── russian_doll_envelopes │ ├── Makefile │ └── README.md ├── search_a_2d_matrix_ii │ ├── Makefile │ └── README.md ├── shortest_word_distance │ ├── Makefile │ └── README.md ├── sliding_window_maximum │ └── Makefile ├── sort_transformed_array │ ├── Makefile │ └── README.md ├── sortedListToBST │ └── README.md ├── strobogrammatic_number │ ├── Makefile │ └── README.md ├── sum_of_two_integers │ ├── Makefile │ └── README.md ├── valid_perfect_square │ ├── Makefile │ └── README.md ├── water_and_jug_problem │ └── Makefile ├── android_unlock_patterns │ └── Makefile ├── count_univalue_subtrees │ ├── Makefile │ └── README.md ├── encode_and_decode_strings │ └── Makefile ├── expression_add_operators │ ├── Makefile │ └── README.md ├── find_leaves_of_binary_tree │ └── Makefile ├── generalized_abbreviation │ ├── Makefile │ └── README.md ├── inorder_successor_in_bst │ ├── Makefile │ └── README.md ├── integer_to_english_words │ ├── Makefile │ └── README.md ├── intersection_of_two_arrays │ ├── Makefile │ └── README.md ├── isValidSudoku │ ├── 250px-Sudoku-by-L2G-20050714.svg.png │ └── README.md ├── largest_divisible_subset │ ├── Makefile │ └── README.md ├── mini_parser │ └── Makefile ├── nested_list_weight_sum_ii │ └── Makefile ├── palindrome_permutation_ii │ ├── Makefile │ └── README.md ├── range_sum_query_2d_mutable │ └── Makefile ├── range_sum_query_immutable │ ├── Makefile │ └── README.md ├── range_sum_query_mutable │ ├── Makefile │ └── README.md ├── ransom_note │ └── Makefile ├── remove_duplicate_letters │ ├── Makefile │ └── README.md ├── remove_invalid_parentheses │ ├── Makefile │ └── README.md ├── reverse_vowels_of_a_string │ ├── Makefile │ └── README.md ├── shortest_word_distance_ii │ └── Makefile ├── shortest_word_distance_iii │ ├── Makefile │ └── README.md ├── strobogrammatic_number_ii │ ├── Makefile │ └── README.md ├── strobogrammatic_number_iii │ └── Makefile ├── top_k_frequent_elements │ ├── Makefile │ └── README.md ├── unique_word_abbreviation │ └── Makefile ├── closest_binary_search_tree_value │ ├── Makefile │ └── README.md ├── combination_sum_iv │ └── Makefile ├── count_numbers_with_unique_digits │ └── Makefile ├── delete_node_in_a_linked_list │ ├── Makefile │ ├── README.md │ └── main.cpp ├── find_k_pairs_with_smallest_sums │ ├── Makefile │ └── README.md ├── flatten_nested_list_iterator │ ├── Makefile │ └── README.md ├── integerToRoman │ └── README.md ├── intersection_of_two_arrays_ii │ ├── Makefile │ └── README.md ├── longest_increasing_subsequence │ ├── Makefile │ └── README.md ├── maximum_product_of_word_lengths │ └── Makefile ├── moving_average_from_data_stream │ ├── Makefile │ └── README.md ├── number_of_connected_components │ └── Makefile ├── pow_x_n │ └── README.md ├── product_of_array_except_self │ ├── Makefile │ └── README.md ├── range_sum_query_2d_immutable │ └── Makefile ├── shuffle_an_array │ └── Makefile ├── sparse_matrix_multiplication │ ├── Makefile │ └── README.md ├── stock2 │ ├── README.md │ ├── Solution.java │ └── main.py ├── binary_tree_vertical_order_traversal │ └── Makefile ├── closest_binary_search_tree_value_ii │ └── Makefile ├── count_of_smaller_number_after_self │ └── Makefile ├── design_phone_directory │ └── Makefile ├── different_ways_to_add_parentheses │ ├── Makefile │ └── README.md ├── lexicographical_numbers │ ├── Makefile │ └── README.md ├── linked_list_random_node │ └── Makefile ├── longest_increasing_path_in_a_matrix │ └── Makefile ├── max_sum_of_rectangle_no_larger_than_k │ └── Makefile ├── maximum_size_subarray_sum_equals_k │ └── Makefile ├── rearrange_string_k_distance_apart │ └── Makefile ├── serialize_and_deserialize_binary_tree │ └── Makefile ├── shortest_distance_from_all_buildings │ └── Makefile ├── binary_tree_longest_consecutive_sequence │ └── Makefile ├── find_median_from_data_stream │ └── Makefile ├── insert_delete_getrandom_o1 │ └── Makefile ├── kth_smallest_sorted_matrix │ └── Makefile ├── longestSubstringWithoutRepeatingCharacters │ ├── Makefile │ └── README.md ├── lowest_common_ancestor_of_a_binary_tree │ └── Makefile ├── nQueensII │ └── README.md ├── smallest_rectangle_enclosing_black_pixels │ ├── Makefile │ └── README.md ├── best_time_to_buy_and_sell_stock_with_cooldown │ └── Makefile ├── letterCombinationPhoneNumber │ ├── 200px-Telephone-keypad2.svg.png │ └── README.md ├── lowest_common_ancestor_of_a_binary_search_tree │ └── Makefile ├── verify_preorder_sequence_in_binary_search_tree │ └── Makefile ├── verify_preorder_serialization_of_a_binary_tree │ └── Makefile ├── implement_strstr │ └── README.md ├── insert_delete_getrandom_o1_duplicate │ └── Makefile ├── longest_substring_with_at_most_k_distinct_characters │ ├── Makefile │ └── README.md ├── summary_ranges │ └── README.md ├── majority_element_ii │ └── README.md ├── linkedListCycleII │ └── README.md ├── climbStairs │ ├── README.md │ └── Solution.java ├── maxDepthOfBinaryTree │ ├── README.md │ └── Solution.java ├── plusOne │ └── README.md ├── burst_balloons │ └── Makefile ├── count_of_range_sum │ └── Makefile ├── pascalII │ └── README.md ├── sameTree │ ├── README.md │ └── Solution.java ├── remove_duplicate_sorted_list │ └── README.md ├── sudokuSolver │ └── README.md ├── data_stream_as_disjoint_intervals │ ├── Makefile │ └── README.md ├── minDepthOfTree │ └── README.md ├── permutations │ └── README.md ├── removeElement │ └── README.md ├── remove_linkedlist_element │ ├── README.md │ └── Solution.java ├── merge2Sorted │ └── README.md ├── missingRanges │ └── README.md ├── contains_duplicate │ ├── README.md │ └── Solution.java ├── divideTwoInteger │ └── README.md ├── balanced_binary_tree │ └── README.md ├── pascal │ └── README.md ├── romanToInt │ └── README.md ├── singleNumber │ ├── README.md │ ├── Solution.java │ └── main.py ├── restoreIPaddress │ └── README.md ├── stock1 │ ├── README.md │ └── Solution.java ├── binaryTreeMaxPathSum │ └── README.md ├── longestCommonPrefix │ └── README.md ├── number_1_bits │ ├── README.md │ └── Solution.java ├── preOrderTraversal │ ├── README.md │ └── main.py ├── mergeIntervals │ └── README.md ├── remove_duplicate_from_sorted_array_ii │ └── README.md ├── minPathSum │ └── README.md ├── multiplyString │ ├── Solution.java │ └── README.md ├── valid_parentheses │ └── README.md ├── factorial_trailing_zeroes │ ├── README.md │ └── Solution.java ├── rotateImage │ └── README.md ├── combination │ └── README.md ├── spiralMatrixII │ └── README.md ├── rectangle_area │ ├── README.md │ └── Solution.java ├── spiralMatrix │ └── README.md ├── swapInPairs │ └── README.md ├── maxSubArray │ ├── README.md │ └── Solution.java ├── reverseLinkedList2 │ └── README.md ├── rotateList │ └── README.md ├── bitwiseAndRange │ ├── README.md │ └── Solution.java ├── searchInRotatedList │ └── README.md ├── inOrderTraversal │ └── README.md ├── palindromePartition │ └── README.md ├── excel_sheet_column_number │ ├── README.md │ └── Solution.java ├── recoverBinarySearchTree │ └── README.md ├── linkedListCycle │ └── README.md ├── interleaveString │ └── README.md ├── reorderList │ └── README.md ├── reverseInteger │ └── main.py ├── shortest_palindrome │ ├── main.py │ └── README.md ├── partitionList │ └── README.md ├── subset │ └── README.md ├── jumpGame │ ├── Solution.java │ └── README.md ├── searchInsert │ └── README.md ├── searchRange │ └── README.md ├── nutsInOasis │ └── NutsInOasisTest.java ├── removeNthNodeList │ └── README.md ├── contains_duplicate_iii │ └── README.md ├── lengthOfLastWord │ └── README.md ├── reverseWordsInString │ ├── README.md │ └── solution.py ├── validNumber │ └── README.md ├── kth_largest_element_in_an_array │ └── README.md ├── postOrderTraversal │ ├── README.md │ └── main.py ├── excel_sheet_column_title │ └── README.md ├── maximal_square │ └── README.md ├── remove_duplicate_from_sorted_array │ ├── README.md │ └── main.py ├── permutationSequence │ └── README.md ├── minimum_size_subarray_sum │ └── README.md ├── largest_number │ └── README.md ├── levelOrderII │ └── README.md ├── remove_duplicate_sorted_list_ii │ └── README.md ├── edit_distance │ └── README.md ├── stock3 │ └── README.md ├── wordBreak │ └── README.md ├── mergeSortedArray │ ├── README.md │ └── Solution.java ├── maximum_gap │ └── README.md ├── min_stack │ └── README.md ├── generateParentheses │ └── README.md ├── wordBreakII │ └── README.md ├── basic_calculator_ii │ └── README.md ├── max_product_subarray │ └── README.md ├── uniquePaths │ └── README.md ├── longestValidParentheses │ └── README.md ├── maximalRectangle │ └── README.md ├── combination_sum_iii │ └── README.md ├── count_complete_tree_nodes │ └── README.md ├── longestSubstringWith2UniqueChars │ └── README.md ├── pathSumII │ └── README.md ├── validPalindrome │ └── README.md ├── levelOrder │ └── README.md ├── pathSum │ └── README.md ├── triangle │ ├── README.md │ └── main.py ├── number_of_islands │ └── README.md ├── uniqueBST │ ├── README.md │ └── Solution.java ├── decodeWays │ └── README.md ├── simplifyPath │ └── README.MD ├── uniquePathsII │ └── README.md ├── sumRootToLeaf │ └── README.md ├── invert_binary_tree │ ├── README.md │ └── Solution.java ├── longestConsecutiveSequence │ └── README.md ├── reverse_bits │ ├── README.md │ └── Solution.java ├── firstMissingPositive │ └── README.md ├── palindromeNumber │ └── palindrome.cpp ├── singleNumberII │ ├── Solution.java │ └── README.md ├── uniqueBST2 │ └── README.md ├── zigzagConversion │ └── README.md ├── count_primes │ └── README.md ├── substringWithCatAllWords │ └── README.md ├── binaryTreeZigzag │ └── README.md ├── house_robber │ └── Solution.java ├── validBST │ ├── README.md │ └── Solution.java ├── readNCharactersGivenRead4 │ └── README.md ├── wordSearch │ └── README.md ├── binaryTreeRightSideView │ └── README.md ├── longestPalindromeSubstring │ └── README.md ├── reverseKgroup │ └── README.md ├── searchInRotatedListII │ └── README.md ├── symmetricTree │ └── README.md ├── countAndSay │ └── README.md ├── candy │ ├── main.py │ └── README.md ├── searchInMatrix │ └── README.md ├── isomorphic_strings │ └── README.md ├── binary_search_tree_iterator │ └── README.md ├── find_minimum_in_rotated_sorted_array │ └── README.md ├── find_minimum_in_rotated_sorted_array_II │ └── README.md ├── copyListWithRandomPointer │ └── README.md ├── partitionPainter │ └── README.md ├── populateNextRightII │ └── README.md ├── happy_number │ └── README.md ├── rotate_array │ └── README.md ├── fraction_to_recurring_decimal │ └── README.md ├── insertInterval │ └── README.md ├── repeated_DNA_sequences │ └── README.md ├── surroundedRegions │ └── README.md ├── wordLadderII │ └── README.md ├── add_and_search_word_data_structure_design │ └── README.md ├── maxPointsOnALine │ └── README.md └── trappingRainWater │ └── README.md ├── xcode-project └── leetcode-debugger │ ├── leetcode-debugger │ ├── README.md │ ├── Makefile │ └── main.cpp │ └── leetcode-debugger.xcodeproj │ ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ │ └── IDEWorkspaceChecks.plist │ └── xcuserdata │ └── yubin.xcuserdatad │ └── xcschemes │ └── xcschememanagement.plist └── common_questions ├── lowest_common_ancestor └── README.md ├── largest_slope └── README.md ├── replace_word_in_string └── README.md ├── number_all_factors └── README.md ├── text_file_iterable └── README.md ├── prime_count_in_range └── README.md ├── print_one_to_hundred └── Print.java └── intimate_numbers └── README.md /src/reverse_linkedlist/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Reverse a singly linked list. -------------------------------------------------------------------------------- /src/3sum_smaller/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/400_nth_digit/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/403_frog_jump/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/bomb_enemy/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/cloneGraph/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/decode_string/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/flip_game_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/insertionSortList/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Sort a linked list using insertion sort. -------------------------------------------------------------------------------- /src/is_subsequence/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/setMatrixZero/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/263_ugly_number/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/401_binary_watch/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/402_remove_k_digits/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/elimination_game/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/evaluate_division/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/find_the_difference/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/integer_replacement/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/perfect_rectangle/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/random_pick_index/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/rotate_function/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/utf_8_validation/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/wiggle_sequence/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/404_sum_of_left_leaves/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/buildTreePreOrderInOrder/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/contains_duplicate_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/sortColors/main.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yubinbai/leetcode/HEAD/src/sortColors/main.pyc -------------------------------------------------------------------------------- /src/405_convert_a_number_to_hex/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/407_trapping_rain_water_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/408_valid_word_abbreviation/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/buildTreeInOrderPostOrder/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/find_the_duplicate_number/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/guess_number_higher_or_lower/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/longest_absolute_file_path/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /xcode-project/leetcode-debugger/leetcode-debugger/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Solution 5 | === 6 | -------------------------------------------------------------------------------- /src/406_queue_reconstruction_by_height/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/first_unique_character_in_a_string/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/guess_number_higher_or_lower_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/2328_number_of_increasing_path_in_a_grid/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/containerWithMostWater/main.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yubinbai/leetcode/HEAD/src/containerWithMostWater/main.pyc -------------------------------------------------------------------------------- /src/squareRoot/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Implement int sqrt(int x). 4 | 5 | Compute and return the square root of x. -------------------------------------------------------------------------------- /src/implement_trie_prefix_tree/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Implement a trie with insert, search, and startsWith methods 4 | -------------------------------------------------------------------------------- /src/power_of_two/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given an integer, write a function to determine if it is a power of two. 5 | -------------------------------------------------------------------------------- /xcode-project/leetcode-debugger/leetcode-debugger/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/longest_substring_with_at_least_k_repeating_characters/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | -------------------------------------------------------------------------------- /src/implement_queue_using_stacks/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o 3 | run: 4 | clang++ main.cpp -o main.o 5 | ./main.o 6 | -------------------------------------------------------------------------------- /src/3Sum/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/anagram/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/atoi/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/h_index/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/increasing_triplet_subsequence/README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yubinbai/leetcode/HEAD/src/increasing_triplet_subsequence/README.md -------------------------------------------------------------------------------- /src/largestRectangleHistogram/histogram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yubinbai/leetcode/HEAD/src/largestRectangleHistogram/histogram.png -------------------------------------------------------------------------------- /src/two_sum/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/3SumClosest/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/addBinary/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/add_digits/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/coin_change/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/flip_game/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/game_of_life/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/h_index_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/mergeKSorted/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/nim_game/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/paint_fence/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/paint_house/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/sortedArrayToBST/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an array where elements are sorted in ascending order, convert it to a height balanced BST. -------------------------------------------------------------------------------- /src/super_pow/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/wiggle_sort/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/word_pattern/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/add_two_numbers/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/additive_number/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/alien_dictionary/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/basic_calculator/makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/binary_tree_paths/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/bulb_switcher/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/bulls_and_cows/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/counting_bits/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/design_snake_game/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/design_twitter/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/first_bad_version/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/flatten_2d_vector/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/graph_valid_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/house_robber_iii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/integer_break/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/largestRectangleHistogram/histogram_area.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yubinbai/leetcode/HEAD/src/largestRectangleHistogram/histogram_area.png -------------------------------------------------------------------------------- /src/line_reflection/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/meeting_rooms/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/meeting_rooms_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/missing_number/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/moving_zeroes/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/paint_house_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/palindrome_pairs/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/patching_array/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/peeking_iterator/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/perfect_squares/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/power_of_four/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/power_of_three/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/range_addition/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/reverse_string/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/self_crossing/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/single_number_iii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/sortList/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Sort a linked list in O(n log n) time using constant space complexity. 4 | 5 | Solution 6 | === 7 | Merge sort -------------------------------------------------------------------------------- /src/super_ugly_number/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/ugly_number_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/valid_anagram/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/walls_and_gates/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/wiggle_sort_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/word_pattern_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/zigzag_iterator/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/best_meeting_point/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/create_maximum_number/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/design_hit_counter/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/design_tic_tac_toe/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/factor_combinations/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/find_the_celebrity/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/group_shifted_strings/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/largest_bst_subtree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/logger_rate_limiter/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/minimum_height_trees/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/nested_list_weight_sum/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/number_of_digit_one/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/number_of_islands_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/odd_even_linked_list/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/palindrome_linked_list/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/palindrome_permutation/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/plus_one_linked_list/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/reconstruct_itinerary/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/russian_doll_envelopes/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/search_a_2d_matrix_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/shortest_word_distance/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/sliding_window_maximum/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/sort_transformed_array/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/sortedListToBST/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. -------------------------------------------------------------------------------- /src/strobogrammatic_number/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/sum_of_two_integers/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/valid_perfect_square/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/water_and_jug_problem/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/android_unlock_patterns/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/count_univalue_subtrees/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/encode_and_decode_strings/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/expression_add_operators/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/find_leaves_of_binary_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/generalized_abbreviation/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/inorder_successor_in_bst/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/integer_to_english_words/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/intersection_of_two_arrays/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/isValidSudoku/250px-Sudoku-by-L2G-20050714.svg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yubinbai/leetcode/HEAD/src/isValidSudoku/250px-Sudoku-by-L2G-20050714.svg.png -------------------------------------------------------------------------------- /src/largest_divisible_subset/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/mini_parser/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | 5 | run: 6 | clang++ main.cpp -o main.o -std=c++11 7 | ./main.o 8 | -------------------------------------------------------------------------------- /src/nested_list_weight_sum_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/palindrome_permutation_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/power_of_two/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isPowerOfTwo(int n) { 3 | return n > 0 && (n & (n - 1)) == 0; 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /src/range_sum_query_2d_mutable/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/range_sum_query_immutable/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/range_sum_query_mutable/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/ransom_note/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | 5 | run: 6 | clang++ main.cpp -o main.o -std=c++11 7 | ./main.o 8 | -------------------------------------------------------------------------------- /src/remove_duplicate_letters/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/remove_invalid_parentheses/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/reverse_vowels_of_a_string/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/shortest_word_distance_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/shortest_word_distance_iii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/strobogrammatic_number_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/strobogrammatic_number_iii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/top_k_frequent_elements/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/unique_word_abbreviation/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/closest_binary_search_tree_value/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/combination_sum_iv/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | 5 | run: 6 | clang++ main.cpp -o main.o -std=c++11 7 | ./main.o 8 | -------------------------------------------------------------------------------- /src/count_numbers_with_unique_digits/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/delete_node_in_a_linked_list/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/find_k_pairs_with_smallest_sums/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/flatten_nested_list_iterator/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/increasing_triplet_subsequence/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/integerToRoman/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an integer, convert it to a roman numeral. 4 | 5 | Input is guaranteed to be within the range from 1 to 3999. -------------------------------------------------------------------------------- /src/intersection_of_two_arrays_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/longest_increasing_subsequence/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/maximum_product_of_word_lengths/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/moving_average_from_data_stream/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/number_of_connected_components/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/pow_x_n/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Implement pow(x, n). 4 | 5 | Solution 6 | === 7 | Edge case guarding: 8 | 9 | x == 0? 10 | 11 | n < 0 ? 12 | -------------------------------------------------------------------------------- /src/product_of_array_except_self/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/range_sum_query_2d_immutable/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/reverse_string/README.md: -------------------------------------------------------------------------------- 1 | Problem === Write a function that takes a string as input and returns the string reversed. Example: Given s = "hello", return "olleh". -------------------------------------------------------------------------------- /src/shuffle_an_array/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | 5 | run: 6 | clang++ main.cpp -o main.o -std=c++11 7 | ./main.o 8 | -------------------------------------------------------------------------------- /src/sparse_matrix_multiplication/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/stock2/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Say you have an array for which the ith element is the price of a given 4 | stock on day i. 5 | 6 | unlimited buy and sell 7 | -------------------------------------------------------------------------------- /src/binary_tree_vertical_order_traversal/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/closest_binary_search_tree_value_ii/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/count_of_smaller_number_after_self/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/design_phone_directory/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | 5 | run: 6 | clang++ main.cpp -o main.o -std=c++11 7 | ./main.o 8 | -------------------------------------------------------------------------------- /src/different_ways_to_add_parentheses/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/lexicographical_numbers/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | 5 | run: 6 | clang++ main.cpp -o main.o -std=c++11 7 | ./main.o 8 | -------------------------------------------------------------------------------- /src/linked_list_random_node/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | 5 | run: 6 | clang++ main.cpp -o main.o -std=c++11 7 | ./main.o 8 | -------------------------------------------------------------------------------- /src/longest_increasing_path_in_a_matrix/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/max_sum_of_rectangle_no_larger_than_k/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/maximum_size_subarray_sum_equals_k/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/rearrange_string_k_distance_apart/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/serialize_and_deserialize_binary_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/shortest_distance_from_all_buildings/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/binary_tree_longest_consecutive_sequence/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/find_median_from_data_stream/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | 5 | run: 6 | clang++ main.cpp -o main.o -std=c++11 7 | ./main.o 8 | -------------------------------------------------------------------------------- /src/insert_delete_getrandom_o1/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | 5 | run: 6 | clang++ main.cpp -o main.o -std=c++11 7 | ./main.o 8 | -------------------------------------------------------------------------------- /src/kth_smallest_sorted_matrix/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | 5 | run: 6 | clang++ main.cpp -o main.o -std=c++11 7 | ./main.o 8 | -------------------------------------------------------------------------------- /src/longestSubstringWithoutRepeatingCharacters/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/lowest_common_ancestor_of_a_binary_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/nQueensII/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Follow up for N-Queens problem. 4 | 5 | Now, instead outputting board configurations, return the total number of distinct solutions. -------------------------------------------------------------------------------- /src/smallest_rectangle_enclosing_black_pixels/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/best_time_to_buy_and_sell_stock_with_cooldown/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/letterCombinationPhoneNumber/200px-Telephone-keypad2.svg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yubinbai/leetcode/HEAD/src/letterCombinationPhoneNumber/200px-Telephone-keypad2.svg.png -------------------------------------------------------------------------------- /src/lowest_common_ancestor_of_a_binary_search_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/verify_preorder_sequence_in_binary_search_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/verify_preorder_serialization_of_a_binary_tree/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /common_questions/lowest_common_ancestor/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | find the lowest common ancestor of two nodes in a tree 5 | 6 | Solution 7 | === 8 | 9 | post-order traversal -------------------------------------------------------------------------------- /src/implement_strstr/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Implement strStr(). 4 | 5 | Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. -------------------------------------------------------------------------------- /src/insert_delete_getrandom_o1_duplicate/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | ./main.o 4 | 5 | run: 6 | clang++ main.cpp -o main.o -std=c++11 7 | ./main.o 8 | -------------------------------------------------------------------------------- /src/longest_substring_with_at_most_k_distinct_characters/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | -------------------------------------------------------------------------------- /src/palindrome_linked_list/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a singly linked list, determine if it is a palindrome. 4 | 5 | Follow up: 6 | Could you do it in O(n) time and O(1) space? -------------------------------------------------------------------------------- /src/buildTreeInOrderPostOrder/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given inorder and postorder traversal of a tree, construct the binary tree. 4 | 5 | Solution 6 | === 7 | Post order to find root, divide and conquer -------------------------------------------------------------------------------- /src/buildTreePreOrderInOrder/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given inorder and preorder traversal of a tree, construct the binary tree. 4 | 5 | Solution 6 | === 7 | Pre order to find root, divide and conquer -------------------------------------------------------------------------------- /src/summary_ranges/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a sorted integer array without duplicates, return the summary of its ranges. 5 | 6 | For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. -------------------------------------------------------------------------------- /common_questions/largest_slope/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | There are N points on a plane. Each pair of points determines a line. 4 | 5 | Fine the line that has the largest slope 6 | 7 | Solution 8 | === 9 | -------------------------------------------------------------------------------- /src/majority_element_ii/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 5 | -------------------------------------------------------------------------------- /src/sum_of_two_integers/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. 4 | 5 | Example: 6 | Given a = 1 and b = 2, return 3. 7 | 8 | -------------------------------------------------------------------------------- /src/linkedListCycleII/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a linked list, return the node where the cycle begins. If there is no cycle, return *null*. 4 | 5 | Follow up: 6 | Can you solve it without using extra space? -------------------------------------------------------------------------------- /src/palindrome_permutation/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a string, determine if a permutation of the string could form a palindrome. 4 | 5 | For example, 6 | "code" -> False, "aab" -> True, "carerac" -> True. -------------------------------------------------------------------------------- /src/climbStairs/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | You are climbing a stair case. It takes n steps to reach to the top. 4 | 5 | Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? -------------------------------------------------------------------------------- /src/maxDepthOfBinaryTree/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a binary tree, find its maximum depth. 4 | 5 | The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. -------------------------------------------------------------------------------- /src/plusOne/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a non-negative number represented as an array of digits, plus one to the number. 4 | 5 | The digits are stored such that the most significant digit is at the head of the list. -------------------------------------------------------------------------------- /src/burst_balloons/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | 8 | run1: 9 | clang++ main1.cpp -o main1.o -std=c++11 10 | ./main1.o 11 | -------------------------------------------------------------------------------- /src/count_of_range_sum/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | 8 | run2: 9 | clang++ main2.cpp -o main2.o -std=c++11 10 | ./main2.o 11 | -------------------------------------------------------------------------------- /src/pascalII/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an index k, return the kth row of the Pascal's triangle. 4 | 5 | For example, given k = 3, 6 | Return [1,3,3,1]. 7 | 8 | Note: Could you optimize your algorithm to use only O(k) extra space? -------------------------------------------------------------------------------- /src/reverse_vowels_of_a_string/README.md: -------------------------------------------------------------------------------- 1 | Problem === Write a function that takes a string as input and reverse only the vowels of a string. Example 1: Given s = "hello", return "holle". Example 2: Given s = "leetcode", return "leotcede". -------------------------------------------------------------------------------- /src/sameTree/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given two binary trees, write a function to check if they are equal or not. 5 | 6 | Two binary trees are considered equal if they are structurally identical and the nodes have the same value. -------------------------------------------------------------------------------- /src/inorder_successor_in_bst/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a binary search tree and a node in it, find the in-order successor of that node in the BST. 5 | Note: If the given node has no in-order successor in the tree, return null. 6 | -------------------------------------------------------------------------------- /src/remove_duplicate_sorted_list/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a sorted linked list, delete all duplicates such that each element appear only once. 4 | 5 | For example, 6 | Given 1->1->2, return 1->2. 7 | Given 1->1->2->3->3, return 1->2->3. -------------------------------------------------------------------------------- /src/sudokuSolver/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Write a program to solve a Sudoku puzzle by filling the empty cells. 5 | 6 | Empty cells are indicated by the character '.'. 7 | 8 | You may assume that there will be only one unique solution. -------------------------------------------------------------------------------- /src/wiggle_sort/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... 4 | For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4]. 5 | -------------------------------------------------------------------------------- /common_questions/replace_word_in_string/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | ==== 3 | replaces every instances of string 'find' with string 'repl' 4 | returned the modified string 5 | 6 | Solution 7 | === 8 | String builder, keeping finding next needle in long string -------------------------------------------------------------------------------- /src/data_stream_as_disjoint_intervals/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | clang++ main.cpp -o main.o -std=c++11 3 | 4 | run: 5 | clang++ main.cpp -o main.o -std=c++11 6 | ./main.o 7 | 8 | run2: 9 | clang++ main2.cpp -o main.o -std=c++11 10 | ./main.o 11 | -------------------------------------------------------------------------------- /src/minDepthOfTree/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a binary tree, find its minimum depth. 4 | 5 | The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 6 | 7 | Solution 8 | === 9 | BFS -------------------------------------------------------------------------------- /src/permutations/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a collection of numbers, return all possible permutations. 4 | 5 | For example, 6 | 7 | [1,2,3] have the following permutations: 8 | 9 | [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1]. -------------------------------------------------------------------------------- /src/power_of_four/README.md: -------------------------------------------------------------------------------- 1 | Problem === Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion? -------------------------------------------------------------------------------- /src/removeElement/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given an array and a value, remove all instances of that value in place and return the new length. 5 | 6 | The order of elements can be changed. It doesn't matter what you leave beyond the new length. -------------------------------------------------------------------------------- /src/remove_linkedlist_element/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Remove all elements from a linked list of integers that have value val. 4 | 5 | Example 6 | Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 7 | Return: 1 --> 2 --> 3 --> 4 --> 5 8 | 9 | -------------------------------------------------------------------------------- /src/merge2Sorted/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Merge 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. 4 | 5 | Solution 6 | === 7 | Keep track of two lists and generate -------------------------------------------------------------------------------- /src/missingRanges/README.md: -------------------------------------------------------------------------------- 1 | Missing Ranges 2 | === 3 | 4 | Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing ranges. 5 | 6 | For example, given [0, 1, 3, 50, 75], return [“2”, “4->49”, “51->74”, “76->99”] -------------------------------------------------------------------------------- /src/contains_duplicate/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. -------------------------------------------------------------------------------- /src/divideTwoInteger/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Divide two integers without using multiplication, division and mod operator. 4 | 5 | Solution 6 | === 7 | Long division in binary [Wikipedia](http://www.wikiwand.com/en/Division_algorithm#/Long_division) 8 | -------------------------------------------------------------------------------- /src/intersection_of_two_arrays/README.md: -------------------------------------------------------------------------------- 1 | Problem === Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note: Each element in the result must be unique. The result can be in any order. -------------------------------------------------------------------------------- /xcode-project/leetcode-debugger/leetcode-debugger.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /common_questions/number_all_factors/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | ==== 3 | 4 | 5 | Given a number, return all ways it can be factorized 6 | 7 | example: 8 | 9 | 12 10 | 11 | 1 * 12 12 | 2 * 2 * 3 13 | 2 * 6 14 | 3 * 4 15 | 16 | Solution 17 | === 18 | Backtracking -------------------------------------------------------------------------------- /src/balanced_binary_tree/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a binary tree, determine if it is height-balanced. 4 | 5 | For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. -------------------------------------------------------------------------------- /src/longest_substring_with_at_most_k_distinct_characters/README.md: -------------------------------------------------------------------------------- 1 | Problem === Given a string, find the length of the longest substring T that contains at most 2 distinct characters. For example, Given s = "eceba", T is "ece" which its length is 3. Solution === Sliding pointers -------------------------------------------------------------------------------- /src/pascal/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given numRows, generate the first numRows of Pascal's triangle. 4 | 5 | For example, given numRows = 5, 6 | Return 7 | 8 | [ 9 | [1], 10 | [1,1], 11 | [1,2,1], 12 | [1,3,3,1], 13 | [1,4,6,4,1] 14 | ] 15 | -------------------------------------------------------------------------------- /src/romanToInt/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a roman numeral, convert it to an integer. 5 | 6 | Input is guaranteed to be within the range from 1 to 3999. 7 | 8 | Solution 9 | === 10 | 11 | Scan from last char, determine what the value is based on current value -------------------------------------------------------------------------------- /src/singleNumber/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an array of integers, every element appears twice except for one. 4 | Find that single one. 5 | 6 | Note: 7 | Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 8 | 9 | -------------------------------------------------------------------------------- /src/addBinary/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given two binary strings, return their sum (also a binary string). 4 | 5 | For example, 6 | a = "11" 7 | b = "1" 8 | Return "100". 9 | 10 | Solution 11 | === 12 | Scan from end of strings, keep track of carry, use StringBuilder -------------------------------------------------------------------------------- /src/restoreIPaddress/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a string containing only digits, restore it by returning all possible valid IP address combinations. 4 | 5 | For example: 6 | Given "25525511135", 7 | 8 | return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) -------------------------------------------------------------------------------- /src/stock1/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Say you have an array for which the i-th element is the price of a given stock on day i. 4 | 5 | If you were only permitted to buy one share of the stock and sell one share of the stock, design an algorithm to find the best times to buy and sell. -------------------------------------------------------------------------------- /src/binaryTreeMaxPathSum/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a binary tree, find the maximum path sum. 4 | 5 | The path may start and end at any node in the tree. 6 | 7 | For example: 8 | Given the below binary tree, 9 | 10 | 1 11 | / \ 12 | 2 3 13 | Return 6. -------------------------------------------------------------------------------- /src/binary_tree_paths/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a binary tree, return all root-to-leaf paths. 5 | 6 | For example, given the following binary tree: 7 | 8 | 1 9 | / \ 10 | 2 3 11 | \ 12 | 5 13 | All root-to-leaf paths are: 14 | 15 | ["1->2->5", "1->3"] -------------------------------------------------------------------------------- /src/longestCommonPrefix/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Longest Common Prefix 4 | Write a function to find the longest common prefix string amongst an array of strings. 5 | 6 | Solution 7 | === 8 | 9 | Find common prefix of two strs, then reduce the function to the whole list 10 | -------------------------------------------------------------------------------- /src/meeting_rooms/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), determine if a person could attend all meetings. 4 | 5 | For example, 6 | 7 | Given [[0, 30],[5, 10],[15, 20]], 8 | 9 | return false. -------------------------------------------------------------------------------- /src/meeting_rooms_ii/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required. 5 | 6 | For example, 7 | Given [[0, 30],[5, 10],[15, 20]], 8 | return 2. -------------------------------------------------------------------------------- /src/number_1_bits/README.md: -------------------------------------------------------------------------------- 1 | Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). 2 | 3 | For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3. -------------------------------------------------------------------------------- /src/perfect_squares/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. 4 | 5 | For example, given n = 12, return 3 because 12 = 4 + 4 + 4; given n = 13, return 2 because 13 = 4 + 9. 6 | -------------------------------------------------------------------------------- /src/preOrderTraversal/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a binary tree, return the preorder traversal of its nodes' values. 4 | 5 | For example: 6 | Given binary tree {1,#,2,3}, 7 | 1 8 | \ 9 | 2 10 | / 11 | 3 12 | return [1,2,3]. -------------------------------------------------------------------------------- /src/mergeIntervals/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a collection of intervals, merge all overlapping intervals. 4 | 5 | For example, 6 | Given [1,3],[2,6],[8,10],[15,18], 7 | return [1,6],[8,10],[15,18]. 8 | 9 | Solution 10 | === 11 | 12 | Sort and merge adjacent ones, O(n logn) -------------------------------------------------------------------------------- /src/remove_duplicate_from_sorted_array_ii/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Follow up for "Remove Duplicates": 4 | What if duplicates are allowed at most twice? 5 | 6 | For example, 7 | Given sorted array A = [1,1,1,2,2,3], 8 | 9 | Your function should return length = 5, and A is now [1,1,2,2,3]. -------------------------------------------------------------------------------- /src/contains_duplicate_ii/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an array of integers and an integer k, return true if and only if there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between i and j is at most k. 4 | 5 | Solution 6 | === 7 | 8 | LRU cache -------------------------------------------------------------------------------- /src/minPathSum/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given 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. 4 | 5 | Note: You can only move either down or right at any point in time. 6 | 7 | Solution 8 | === 9 | DP -------------------------------------------------------------------------------- /src/multiplyString/Solution.java: -------------------------------------------------------------------------------- 1 | import java.math.*; 2 | public class Solution { 3 | public String multiply(String num1, String num2) { 4 | BigInteger b1 = new BigInteger(num1); 5 | BigInteger b2 = new BigInteger(num2); 6 | return b1.multiply(b2).toString(); 7 | } 8 | } -------------------------------------------------------------------------------- /src/valid_parentheses/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Valid Parentheses 4 | Given a string containing just the characters '(', ')', '{', '}', '['and ']', determine if the input string is valid. 5 | 6 | The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not. -------------------------------------------------------------------------------- /src/factorial_trailing_zeroes/README.md: -------------------------------------------------------------------------------- 1 | Factorial Trailing Zeroes 2 | === 3 | 4 | Given an integer n, return the number of trailing zeroes in n!. 5 | 6 | Note: Your solution should be in logarithmic time complexity. 7 | 8 | Solution 9 | === 10 | 5 occurs much more frequently than 2. Count 5s. 11 | -------------------------------------------------------------------------------- /src/rotateImage/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | You are given an n x n 2D matrix representing an image. 5 | 6 | Rotate the image by 90 degrees (clockwise). 7 | 8 | Follow up: 9 | Could you do this in-place? 10 | 11 | Solution 12 | === 13 | Use a graph to figure out the cooridinates of each point -------------------------------------------------------------------------------- /src/top_k_frequent_elements/README.md: -------------------------------------------------------------------------------- 1 | Problem === Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example, MovingAverage m = new MovingAverage(3); m.next(1) = 1 m.next(10) = (1 + 10) / 2 m.next(3) = (1 + 10 + 3) / 3 m.next(5) = (10 + 3 + 5) / 3 -------------------------------------------------------------------------------- /src/combination/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. 4 | 5 | For example, 6 | If n = 4 and k = 2, a solution is: 7 | 8 | [ 9 | [2,4], 10 | [3,4], 11 | [2,3], 12 | [1,2], 13 | [1,3], 14 | [1,4], 15 | ] 16 | -------------------------------------------------------------------------------- /src/moving_average_from_data_stream/README.md: -------------------------------------------------------------------------------- 1 | Problem === Given a stream of integers and a window size, calculate the moving average of all integers in the sliding window. For example, MovingAverage m = new MovingAverage(3); m.next(1) = 1 m.next(10) = (1 + 10) / 2 m.next(3) = (1 + 10 + 3) / 3 m.next(5) = (10 + 3 + 5) / 3 -------------------------------------------------------------------------------- /src/spiralMatrixII/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 4 | 5 | For example, 6 | Given n = 3, 7 | 8 | You should return the following matrix: 9 | 10 | [ 11 | [ 1, 2, 3 ], 12 | [ 8, 9, 4 ], 13 | [ 7, 6, 5 ] 14 | ] -------------------------------------------------------------------------------- /src/implement_queue_using_stacks/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Implement the following operations of a queue using stacks. 4 | 5 | push(x) -- Push element x to the back of queue. 6 | pop() -- Removes the element from in front of queue. 7 | peek() -- Get the front element. 8 | empty() -- Return whether the queue is empty. 9 | -------------------------------------------------------------------------------- /src/multiplyString/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given two numbers represented as strings, return multiplication of the numbers as a string. 4 | 5 | Note: The numbers can be arbitrarily large and are non-negative. 6 | 7 | Solution 8 | === 9 | 10 | Long multiplication, already implemented in Java and Python libraries -------------------------------------------------------------------------------- /src/palindrome_permutation_ii/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form. 5 | 6 | For example: 7 | 8 | Given s = "aabb", return ["abba", "baab"]. 9 | 10 | Given s = "abc", return []. -------------------------------------------------------------------------------- /src/rectangle_area/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Find the total area covered by two rectilinear rectangles in a 2D plane. 5 | 6 | Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. 7 | 8 | Rectangle Area 9 | Assume that the total area is never beyond the maximum possible value of int. -------------------------------------------------------------------------------- /src/spiralMatrix/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 4 | 5 | For example, 6 | Given the following matrix: 7 | 8 | [ 9 | [ 1, 2, 3 ], 10 | [ 4, 5, 6 ], 11 | [ 7, 8, 9 ] 12 | ] 13 | You should return [1,2,3,6,9,8,7,4,5]. -------------------------------------------------------------------------------- /src/swapInPairs/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a linked list, swap every two adjacent nodes and return its head. 4 | For example, 5 | Given 1->2->3->4, you should return the list as 2->1->4->3. 6 | Your algorithm should use only constant space. You may not modify the 7 | values in the list, only nodes itself can be changed. 8 | -------------------------------------------------------------------------------- /common_questions/text_file_iterable/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Implement a (Java) Iterable object that iterates lines one by one from a text file. 4 | A reference to a file. 5 | 6 | 7 | Solution 8 | === 9 | 10 | Use BufferedReader to get file in 11 | 12 | Note: *array* or *Iterable* can be used in for..each loop 13 | 14 | -------------------------------------------------------------------------------- /src/anagram/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an array of strings, return all groups of strings that are anagrams. 4 | 5 | Note: All inputs will be in lower-case. 6 | 7 | Solution 8 | === 9 | 10 | Group strings by their char counts, an easy way to implement is to sort the char array of the string and use this as key of hash map. -------------------------------------------------------------------------------- /src/closest_binary_search_tree_value/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. 5 | Note: 6 | Given target value is a floating point. 7 | You are guaranteed to have only one unique value in the BST that is closest to the target. 8 | 9 | -------------------------------------------------------------------------------- /src/plus_one_linked_list/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a non-negative number represented as a singly linked list of digits, plus one to the number. 5 | 6 | The digits are stored such that the most significant digit is at the head of the list. 7 | 8 | Example: 9 | Input: 10 | 1->2->3 11 | 12 | Output: 13 | 1->2->4 14 | -------------------------------------------------------------------------------- /src/delete_node_in_a_linked_list/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. 5 | 6 | Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function. -------------------------------------------------------------------------------- /src/integer_break/README.md: -------------------------------------------------------------------------------- 1 | Problem === Given 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. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4). Note: you may assume that n is not less than 2. -------------------------------------------------------------------------------- /src/maxSubArray/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Find the contiguous subarray within an array (containing at least one number) which has the largest sum. 4 | 5 | For example, given the array [−2,1,−3,4,−1,2,1,−5,4], 6 | the contiguous subarray [4,−1,2,1] has the largest sum = 6. 7 | 8 | Solution 9 | === 10 | Kadane's algorithm, from CLRS -------------------------------------------------------------------------------- /src/reverseLinkedList2/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Reverse a linked list from position m to n. Do it in-place and in one-pass. 5 | 6 | For example: 7 | Given 1->2->3->4->5->NULL, m = 2 and n = 4, 8 | 9 | return 1->4->3->2->5->NULL. 10 | 11 | Note: 12 | Given m, n satisfy the following condition: 13 | 1 ≤ m ≤ n ≤ length of list. -------------------------------------------------------------------------------- /src/strobogrammatic_number/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). 4 | Write a function to determine if a number is strobogrammatic. The number is represented as a string. 5 | For example, the numbers "69", "88", and "818" are all strobogrammatic. 6 | -------------------------------------------------------------------------------- /src/super_pow/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Your task is to calculate a ** b mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array. 5 | 6 | Example1: 7 | 8 | a = 2 9 | b = [3] 10 | 11 | Result: 8 12 | Example2: 13 | 14 | a = 2 15 | b = [1,0] 16 | 17 | Result: 1024 18 | -------------------------------------------------------------------------------- /src/valid_anagram/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given two strings s and t, write a function to determine if t is an anagram of s. 5 | 6 | For example, 7 | s = "anagram", t = "nagaram", return true. 8 | s = "rat", t = "car", return false. 9 | 10 | Note: 11 | You may assume the string contains only lowercase alphabets. -------------------------------------------------------------------------------- /src/263_ugly_number/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Write a program to check whether a given number is an ugly number. 4 | 5 | Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. 6 | 7 | Note that 1 is typically treated as an ugly number. -------------------------------------------------------------------------------- /src/contains_duplicate/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Solution { 4 | public boolean containsDuplicate(int[] nums) { 5 | Arrays.sort(nums); 6 | for (int i = 1; i < nums.length; ++i) { 7 | if (nums[i] == nums[i - 1]) return true; 8 | } 9 | return false; 10 | } 11 | } -------------------------------------------------------------------------------- /src/rotateList/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a list, rotate the list to the right by k places, where k is non-negative. 5 | 6 | For example: 7 | Given 1->2->3->4->5->NULL and k = 2, 8 | return 4->5->1->2->3->NULL. 9 | 10 | Solution 11 | === 12 | 13 | Count the length of list, break list into two lists and cat. 14 | -------------------------------------------------------------------------------- /src/bitwiseAndRange/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. 4 | 5 | For example, given the range [5, 7], you should return 4. 6 | 7 | Solution 8 | === 9 | 10 | only possible for the prefix in the binary form, iterate to find prefix 11 | -------------------------------------------------------------------------------- /src/searchInRotatedList/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Suppose a sorted array is rotated at some pivot unknown to you beforehand. 4 | 5 | (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). 6 | 7 | You are given a target value to search. If found in the array return its index, otherwise return -1. 8 | 9 | You may assume no duplicate exists in the array. -------------------------------------------------------------------------------- /src/shortest_word_distance/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given two strings s and t, write a function to determine if t is an anagram of s. 5 | 6 | For example, 7 | s = "anagram", t = "nagaram", return true. 8 | s = "rat", t = "car", return false. 9 | 10 | Note: 11 | You may assume the string contains only lowercase alphabets. -------------------------------------------------------------------------------- /src/valid_perfect_square/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a positive integer num, write a function which returns True if num is a perfect square else False. 4 | 5 | Note: Do not use any built-in library function such as sqrt. 6 | 7 | Example 1: 8 | 9 | Input: 16 10 | Returns: True 11 | Example 2: 12 | 13 | Input: 14 14 | Returns: False 15 | -------------------------------------------------------------------------------- /src/flatten_2d_vector/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Implement an iterator to flatten a 2d vector. 4 | 5 | For example, 6 | 7 | Given 2d vector = 8 | 9 | [ 10 | [1,2], 11 | [3], 12 | [4,5,6] 13 | ] 14 | 15 | By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,2,3,4,5,6]. -------------------------------------------------------------------------------- /src/generalized_abbreviation/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Write a function to generate the generalized abbreviations of a word. 5 | 6 | Example: 7 | Given word = "word", return the following list (order does not matter): 8 | ["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"] 9 | 10 | -------------------------------------------------------------------------------- /src/inOrderTraversal/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a binary tree, return the inorder traversal of its nodes' values. 4 | 5 | For example: 6 | Given binary tree {1,#,2,3}, 7 | 1 8 | \ 9 | 2 10 | / 11 | 3 12 | return [1,3,2]. 13 | 14 | Note: Recursive solution is trivial, could you do it iteratively? -------------------------------------------------------------------------------- /src/palindromePartition/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a string s, partition s such that every substring of the partition is a palindrome. 4 | 5 | Return all possible palindrome partitioning of s. 6 | 7 | For example, given s = "aab", 8 | Return 9 | 10 | [ 11 | ["aa","b"], 12 | ["a","a","b"] 13 | ] 14 | 15 | Solution 16 | === 17 | Backtrack -------------------------------------------------------------------------------- /src/addBinary/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @param a, a string 3 | # @param b, a string 4 | # @return a string 5 | 6 | def addBinary(self, a, b): 7 | aBin = int(a, 2) 8 | bBin = int(b, 2) 9 | return bin(aBin + bBin)[2:] 10 | 11 | if __name__ == '__main__': 12 | s = Solution() 13 | print s.addBinary("11", "1") 14 | -------------------------------------------------------------------------------- /src/count_univalue_subtrees/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a binary tree, count the number of uni-value subtrees. 4 | 5 | A Uni-value subtree means all nodes of the subtree have the same value. 6 | 7 | For example: 8 | 9 | Given binary tree, 10 | 11 | 5 12 | / \ 13 | 1 5 14 | / \ \ 15 | 5 5 5 16 | 17 | return 4. 18 | -------------------------------------------------------------------------------- /src/excel_sheet_column_number/README.md: -------------------------------------------------------------------------------- 1 | Excel Sheet Column Number 2 | === 3 | 4 | Related to question Excel Sheet Column Title 5 | 6 | Given a column title as appear in an Excel sheet, return its corresponding column number. 7 | 8 | For example: 9 | 10 | A -> 1 11 | B -> 2 12 | C -> 3 13 | ... 14 | Z -> 26 15 | AA -> 27 16 | AB -> 28 -------------------------------------------------------------------------------- /src/paint_fence/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | There is a fence with n posts, each post can be painted with one of the k colors. 4 | 5 | You have to paint all the posts such that no more than two adjacent fence posts have the same color. 6 | 7 | Return the total number of ways you can paint the fence. 8 | 9 | Note: 10 | n and k are non-negative integers. 11 | -------------------------------------------------------------------------------- /src/recoverBinarySearchTree/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Two elements of a binary search tree (BST) are swapped by mistake. 4 | 5 | Recover the tree without changing its structure. 6 | 7 | Note: 8 | A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? 9 | 10 | Solution 11 | === 12 | In-order traversal with validations -------------------------------------------------------------------------------- /common_questions/prime_count_in_range/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given ints *a* and *b*, how many primes are there in the interval [*a*, *b*) ? 5 | 6 | a < b <= 1e12 7 | b - a <= 1e6 8 | 9 | Solution 10 | === 11 | 12 | all numbers small than *b* will have factors smaller than sqrt(b) 13 | 14 | use sieve between a and b, for all primes in [ 2, sqrt(b) ) 15 | -------------------------------------------------------------------------------- /src/linkedListCycle/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a linked list, determine if it has a cycle in it. 4 | 5 | Follow up: 6 | Can you solve it without using extra space? 7 | 8 | Solution 9 | === 10 | 11 | [http://en.wikipedia.org/wiki/Floyd%27s_cycle-finding_algorithm#Tortoise_and_hare](http://en.wikipedia.org/wiki/Floyd%27s_cycle-finding_algorithm#Tortoise_and_hare) -------------------------------------------------------------------------------- /src/missing_number/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. 5 | 6 | For example, 7 | Given nums = [0, 1, 3] return 2. 8 | 9 | Note: 10 | Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity? -------------------------------------------------------------------------------- /src/nested_list_weight_sum/README.md: -------------------------------------------------------------------------------- 1 | Problem === Given a nested list of integers, returns the sum of all integers in the list weighted by their depth For example, given the list {{1,1},2,{1,1}} the function should return 10 (four 1's at depth 2, one *2 at depth 1) Given the list {1,{4,{6}}} the function should return 27 (one 1 at depth 1, one 4 at depth 2, and *one 6 at depth 3) -------------------------------------------------------------------------------- /src/integer_to_english_words/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. 4 | 5 | For example, 6 | 123 -> "One Hundred Twenty Three" 7 | 12345 -> "Twelve Thousand Three Hundred Forty Five" 8 | 1234567 -> "One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven" 9 | -------------------------------------------------------------------------------- /src/interleaveString/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. 4 | 5 | For example, 6 | Given: 7 | s1 = "aabcc", 8 | s2 = "dbbca", 9 | 10 | When s3 = "aadbbcbcac", return true. 11 | When s3 = "aadbbbaccc", return false. 12 | 13 | Solution 14 | === 15 | 16 | DP, construct s3 from s1 and s2 17 | 18 | -------------------------------------------------------------------------------- /src/reorderList/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a singly linked list L: L0→L1→…→Ln-1→Ln, 4 | reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… 5 | 6 | You must do this in-place without altering the nodes' values. 7 | 8 | For example, 9 | Given {1,2,3,4}, reorder it to {1,4,2,3}. 10 | 11 | Solution 12 | === 13 | Split list in two, reverse second part, and interleave the two parts -------------------------------------------------------------------------------- /src/reverseInteger/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @return an integer 3 | def reverse(self, x): 4 | s = str(x) 5 | if s[0] == '-': 6 | return int("-" + s[-1:0:-1]) 7 | else: 8 | return int(s[::-1]) 9 | 10 | if __name__ == '__main__': 11 | s = Solution() 12 | print s.reverse(123) 13 | print s.reverse(-123) 14 | -------------------------------------------------------------------------------- /src/shortest_palindrome/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def shortestPalindrome(self, s): 3 | r = s[::-1] 4 | for i in range(len(s) + 1): 5 | if s.startswith(r[i:]): 6 | return(r[:i] + s) 7 | 8 | if __name__ == '__main__': 9 | s = Solution() 10 | print s.shortestPalindrome("a") 11 | print s.shortestPalindrome("abcd") 12 | -------------------------------------------------------------------------------- /xcode-project/leetcode-debugger/leetcode-debugger.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/partitionList/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Partition List 4 | Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. 5 | 6 | You should preserve the original relative order of the nodes in each of the two partitions. 7 | 8 | For example, 9 | Given 1->4->3->2->5->2 and x = 3, 10 | return 1->2->2->4->3->5. -------------------------------------------------------------------------------- /src/subset/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a collection of integers that might contain duplicates, S, return all possible subsets. 4 | 5 | Note: 6 | Elements in a subset must be in non-descending order. 7 | The solution set must not contain duplicate subsets. 8 | 9 | For example, 10 | If S = [1,2,2], a solution is: 11 | [[2], [1], [1,2,2], [2,2], [1,2], [] ] 12 | -------------------------------------------------------------------------------- /src/jumpGame/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean canJump(int[] A) { 3 | int curr = A[0]; 4 | for (int i = 0; i < A.length; i++) 5 | if (i > curr) { 6 | return false; 7 | } else { 8 | curr = Math.max(curr, i + A[i]); 9 | } 10 | return curr >= A.length - 1; 11 | } 12 | } -------------------------------------------------------------------------------- /src/remove_duplicate_letters/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results. 4 | 5 | Example: 6 | Given "bcabc" 7 | Return "abc" 8 | 9 | Given "cbacdcbc" 10 | Return "acdb" 11 | -------------------------------------------------------------------------------- /src/searchInsert/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. 5 | 6 | You may assume no duplicates in the array. 7 | 8 | Here are few examples. 9 | 10 | [1,3,5,6], 5 → 2 11 | [1,3,5,6], 2 → 1 12 | [1,3,5,6], 7 → 4 13 | [1,3,5,6], 0 → 0 -------------------------------------------------------------------------------- /src/searchRange/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a sorted array of integers, find the starting and ending position of a given target value. 5 | 6 | Your algorithm's runtime complexity must be in the order of O(log n). 7 | 8 | If the target is not found in the array, return [-1, -1]. 9 | 10 | For example, 11 | Given [5, 7, 7, 8, 8, 10] and target value 8, 12 | return [3, 4]. -------------------------------------------------------------------------------- /src/h_index_ii/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm? 4 | 5 | Hint: 6 | 7 | Expected runtime complexity is in O(log n) and the input is sorted. 8 | 9 | 10 | Solution 11 | === 12 | 13 | Ternary search for unimodal functions: https://en.wikipedia.org/wiki/Ternary_search 14 | 15 | -------------------------------------------------------------------------------- /src/nutsInOasis/NutsInOasisTest.java: -------------------------------------------------------------------------------- 1 | package leetcode.problem.nutsInOasis; 2 | 3 | import junit.framework.TestCase; 4 | 5 | /** 6 | * Unit test for simple App. 7 | */ 8 | public class NutsInOasisTest extends TestCase { 9 | /** 10 | * Rigourous Test :-) 11 | */ 12 | public void testApp() { 13 | assertEquals(46.67, NutsInOasis.getMaxNuts(100, 20, 50, 1), 0.1); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/removeNthNodeList/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a linked list, remove the nth node from the end of list and return its head. 4 | 5 | For example, 6 | 7 | Given linked list: 1->2->3->4->5, and n = 2. 8 | 9 | After removing the second node from the end, the linked list becomes 1->2->3->5. 10 | 11 | Note: 12 | Given n will always be valid. 13 | Try to do this in one pass. -------------------------------------------------------------------------------- /src/singleNumber/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int singleNumber(int[] A) { 3 | int ret = 0; 4 | for (int a : A) { 5 | ret ^= a; 6 | } 7 | return ret; 8 | } 9 | public static void main(String[] args) { 10 | Solution s = new Solution(); 11 | System.out.println(s.singleNumber(new int[] {1,1,2})); 12 | } 13 | } -------------------------------------------------------------------------------- /src/stock1/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int maxProfit(int[] prices) { 3 | if (prices.length == 0) return 0; 4 | int currMin = prices[0]; 5 | int ans = 0; 6 | for (int p : prices) { 7 | currMin = Math.min(currMin, p); 8 | ans = Math.max(ans, p - currMin); 9 | } 10 | return ans; 11 | } 12 | } -------------------------------------------------------------------------------- /src/contains_duplicate_iii/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. 5 | 6 | Solution 7 | === 8 | 9 | Maintain a treemap of max k nodes, binary search for possible matches. O(n log k) 10 | -------------------------------------------------------------------------------- /src/lengthOfLastWord/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. 4 | 5 | If the last word does not exist, return 0. 6 | 7 | Note: A word is defined as a character sequence consists of non-space characters only. 8 | 9 | For example, 10 | Given s = "Hello World", 11 | return 5. -------------------------------------------------------------------------------- /src/longestSubstringWithoutRepeatingCharacters/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a string, find the length of the longest substring without repeating characters. 5 | 6 | For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1. 7 | 8 | Solution 9 | === 10 | Sliding window -------------------------------------------------------------------------------- /src/reverseWordsInString/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an input string, reverse the string word by word. 4 | 5 | For example, 6 | Given s = "the sky is blue", 7 | return "blue is sky the". 8 | 9 | Solution 10 | === 11 | 12 | Soluton 1. Split by regex, then rearrange and concat into sentence 13 | 14 | Soluton 2. Reverse the sentence by character, then reverse each word by character -------------------------------------------------------------------------------- /src/validNumber/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Validate if a given string is numeric. 4 | 5 | Some examples: 6 | "0" => true 7 | " 0.1 " => true 8 | "abc" => false 9 | "1 a" => false 10 | "2e10" => true 11 | 12 | Note: It is intended for the problem statement to be ambiguous. You 13 | should gather all requirements up front before implementing one 14 | import re 15 | -------------------------------------------------------------------------------- /src/kth_largest_element_in_an_array/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. 4 | 5 | For example, 6 | Given [3,2,1,5,6,4] and k = 2, return 5. 7 | 8 | Note: 9 | You may assume k is always valid, 1 ≤ k ≤ array's length. 10 | 11 | Solution 12 | === 13 | 14 | Quick select -------------------------------------------------------------------------------- /src/postOrderTraversal/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a binary tree, return the postorder traversal of its nodes' values. 4 | 5 | For example: 6 | 7 | Given binary tree {1,#,2,3}, 8 | 1 9 | \ 10 | 2 11 | / 12 | 3 13 | return [3,2,1]. 14 | 15 | 16 | Solution 17 | === 18 | 19 | use a stack and a prev pointer 20 | 21 | solution at leetcode forum is over-complicated -------------------------------------------------------------------------------- /src/jumpGame/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an array of non-negative integers, you are initially positioned 4 | at the first index of the array. 5 | 6 | Each element in the array represents your maximum jump length at that 7 | position. 8 | 9 | Determine if you are able to reach the last index. 10 | 11 | For example: 12 | A = [2,3,1,1,4], return true. 13 | 14 | A = [3,2,1,0,4], return false. -------------------------------------------------------------------------------- /src/remove_invalid_parentheses/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results. 4 | 5 | Note: The input string may contain letters other than the parentheses ( and ). 6 | 7 | Examples: 8 | 9 | "()())()" -> ["()()()", "(())()"] 10 | "(a)())()" -> ["(a)()()", "(a())()"] 11 | ")(" -> [""] 12 | -------------------------------------------------------------------------------- /common_questions/print_one_to_hundred/Print.java: -------------------------------------------------------------------------------- 1 | class Print { 2 | /** 3 | * print 1..10 without using loop and conditional stmts 4 | * @param 5 | * @return 6 | */ 7 | public int print(int a) { 8 | int b = (a == 1) ? 0 : print(a - 1); 9 | System.out.println(a); 10 | return 0; 11 | } 12 | public static void main(String[] args) { 13 | Print p = new Print(); 14 | p.print(10); 15 | } 16 | } -------------------------------------------------------------------------------- /src/excel_sheet_column_title/README.md: -------------------------------------------------------------------------------- 1 | Excel Sheet Column Title 2 | === 3 | Given a positive integer, return its corresponding column title as appear in an Excel sheet. 4 | 5 | For example: 6 | 7 | 1 -> A 8 | 2 -> B 9 | 3 -> C 10 | ... 11 | 26 -> Z 12 | 27 -> AA 13 | 28 -> AB 14 | 15 | Solution 16 | === 17 | 18 | Convert to 26-based number in each step of digit extraction 19 | -------------------------------------------------------------------------------- /src/maximal_square/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. 5 | 6 | For example, given the following matrix: 7 | 8 | 1 0 1 0 0 9 | 1 0 1 1 1 10 | 1 1 1 1 1 11 | 1 0 0 1 0 12 | 13 | Return 4. 14 | 15 | 16 | Solution 17 | === 18 | 19 | Same as 'maximal rectangle', with limitation to be square 20 | -------------------------------------------------------------------------------- /src/shortest_palindrome/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. 5 | 6 | For example: 7 | 8 | Given "aacecaaa", return "aaacecaaa". 9 | 10 | Given "abcd", return "dcbabcd". 11 | 12 | 13 | Solution 14 | === 15 | Expand from center -------------------------------------------------------------------------------- /src/number_of_digit_one/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. 4 | 5 | For example: 6 | Given n = 13, 7 | Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 8 | 9 | Solution 10 | === 11 | 12 | Break number into digits, for each digit, calculate the number of 1's appearances 13 | -------------------------------------------------------------------------------- /src/remove_duplicate_from_sorted_array/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. 5 | 6 | Do not allocate extra space for another array, you must do this in place with constant memory. 7 | 8 | For example, 9 | Given input array A = [1,1,2], 10 | 11 | Your function should return length = 2, and A is now [1,2]. -------------------------------------------------------------------------------- /src/permutationSequence/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | The set [1,2,3,…,n] contains a total of n! unique permutations. 5 | 6 | By listing and labeling all of the permutations in order, 7 | We get the following sequence (ie, for n = 3): 8 | 9 | "123" 10 | "132" 11 | "213" 12 | "231" 13 | "312" 14 | "321" 15 | Given n and k, return the kth permutation sequence. 16 | 17 | Note: Given n will be between 1 and 9 inclusive. -------------------------------------------------------------------------------- /common_questions/intimate_numbers/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | find intimate number under 5e6 5 | 6 | factors of 220: 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110 7 | factors of 284: 1, 2, 4, 71, 142 8 | 9 | 10 | Sum of factors: f(n) 11 | 12 | f(220) => 284 13 | f(f(220)) => f(284) => 220 14 | 15 | Solution 16 | === 17 | 18 | Like prime sieve, do the factors of each number in O(n logn) (result of harmonic sum) 19 | 20 | -------------------------------------------------------------------------------- /src/minimum_size_subarray_sum/README.md: -------------------------------------------------------------------------------- 1 | Minimum Size Subarray Sum 2 | === 3 | 4 | Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. 5 | 6 | For example, given the array [2,3,1,2,4,3] and s = 7, 7 | the subarray [4,3] has the minimal length under the problem constraint. 8 | 9 | Solution 10 | === 11 | Sliding window -------------------------------------------------------------------------------- /src/moving_zeroes/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given 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. 5 | 6 | For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. 7 | 8 | Note: 9 | You must do this in-place without making a copy of the array. 10 | Minimize the total number of operations. 11 | -------------------------------------------------------------------------------- /src/largest_number/README.md: -------------------------------------------------------------------------------- 1 | Largest Number 2 | === 3 | 4 | Given a list of non negative integers, arrange them such that they form the largest number. 5 | 6 | For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. 7 | 8 | Note: The result may be very large, so you need to return a string instead of an integer. 9 | 10 | Solution 11 | === 12 | 13 | Class greedy algorithm question, use sorting and partial results -------------------------------------------------------------------------------- /src/levelOrderII/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given 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). 4 | 5 | For example: 6 | Given binary tree {3,9,20,#,#,15,7}, 7 | 3 8 | / \ 9 | 9 20 10 | / \ 11 | 15 7 12 | return its bottom-up level order traversal as: 13 | [ 14 | [15,7], 15 | [9,20], 16 | [3] 17 | ] -------------------------------------------------------------------------------- /src/remove_duplicate_sorted_list_ii/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. 4 | 5 | For example, 6 | Given 1->2->3->3->4->4->5, return 1->2->5. 7 | Given 1->1->1->2->3, return 2->3. 8 | 9 | 10 | Solution 11 | === 12 | 13 | Iterate through list, look at following node of current node to decide whether keep the curr. -------------------------------------------------------------------------------- /src/strobogrammatic_number_ii/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). 4 | For example, the numbers "69", "88", and "818" are all strobogrammatic. 5 | 6 | Find all strobogrammatic numbers that are of length = n. 7 | 8 | For example, 9 | 10 | Given n = 2, return ["11","69","88","96"]. 11 | 12 | Solution 13 | === 14 | Construct iteratively -------------------------------------------------------------------------------- /src/edit_distance/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) 4 | 5 | You have the following 3 operations permitted on a word: 6 | 7 | a) Insert a character 8 | b) Delete a character 9 | c) Replace a character 10 | 11 | Solution 12 | === 13 | 14 | Example from CLRS "Introduction to Algorithms" -------------------------------------------------------------------------------- /src/containerWithMostWater/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. 4 | 5 | Note: You may not slant the container. 6 | 7 | Solution 8 | ==== 9 | Two pointers sliding -------------------------------------------------------------------------------- /src/delete_node_in_a_linked_list/main.cpp: -------------------------------------------------------------------------------- 1 | struct ListNode { 2 | int val; 3 | ListNode *next; 4 | ListNode(int x) : val(x), next(nullptr) {} 5 | }; 6 | 7 | class Solution { 8 | public: 9 | void deleteNode(ListNode* node) { 10 | if (node->next == nullptr) { 11 | return; 12 | } 13 | node->val = node->next->val; 14 | node->next = node->next->next; 15 | } 16 | }; 17 | 18 | int main() { 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /src/range_sum_query_immutable/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. 4 | 5 | Example: 6 | 7 | Given nums = [-2, 0, 3, -5, 2, -1] 8 | 9 | sumRange(0, 2) -> 1 10 | sumRange(2, 5) -> -1 11 | sumRange(0, 5) -> -3 12 | 13 | Note: 14 | You may assume that the array does not change. 15 | There are many calls to sumRange function. 16 | -------------------------------------------------------------------------------- /src/stock3/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Say you have an array for which the ith element is the price of a given 4 | stock on day i. 5 | 6 | Design an algorithm to find the maximum profit. You may complete at most 7 | two transactions. 8 | 9 | Solution 10 | === 11 | Divide list into two halves. For each half find one best transaction, and combine. 12 | 13 | Note the edge case that one good transaction can make more money than two common ones -------------------------------------------------------------------------------- /src/wordBreak/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a string s and a dictionary of words dict, determine if s can be 4 | segmented into a space-separated sequence of one or more dictionary 5 | words. 6 | 7 | For example, given 8 | s = "leetcode", 9 | dict = ["leet", "code"]. 10 | 11 | Return true because "leetcode" can be segmented as "leet code". 12 | 13 | Solution 14 | === 15 | 16 | classic dynamic programming in 2D, fulfilled with hashmap -------------------------------------------------------------------------------- /src/maxDepthOfBinaryTree/Solution.java: -------------------------------------------------------------------------------- 1 | class TreeNode { 2 | int val; 3 | TreeNode left; 4 | TreeNode right; 5 | TreeNode(int x) { 6 | val = x; 7 | } 8 | } 9 | public class Solution { 10 | public int maxDepth(TreeNode root) { 11 | if (root != null) { 12 | return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); 13 | } else { 14 | return 0; 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /src/mergeSortedArray/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given two sorted integer arrays A and B, merge B into A as one sorted array. 4 | 5 | Note: 6 | You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m and n respectively. 7 | 8 | Solution 9 | === 10 | Start from the end of A, fill bigger of A or B. Loop until pointers reach heads of arrays -------------------------------------------------------------------------------- /src/maximum_gap/README.md: -------------------------------------------------------------------------------- 1 | Maximum Gap 2 | === 3 | Given an unsorted array, find the maximum difference between the successive elements in its sorted form. 4 | 5 | Try to solve it in linear time/space. 6 | 7 | Return 0 if the array contains less than 2 elements. 8 | 9 | You may assume all elements in the array are non-negative integers and fit in the 32-bit signed integer range. 10 | 11 | Solution 12 | === 13 | 14 | radix sort 15 | 16 | scan to find gap -------------------------------------------------------------------------------- /src/reverseWordsInString/solution.py: -------------------------------------------------------------------------------- 1 | import re 2 | class Solution: 3 | # @param s, a string 4 | # @return a string 5 | def reverseWords(self, s): 6 | return ' '.join(re.split('\s+', s)[::-1]).strip() 7 | 8 | if __name__ == '__main__': 9 | s = Solution() 10 | print '___' + s.reverseWords('the sky is blue') + '___' 11 | print '___' + s.reverseWords(' a b ') + '___' 12 | print '___' + s.reverseWords(' ') + '___' 13 | -------------------------------------------------------------------------------- /src/find_k_pairs_with_smallest_sums/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. 5 | 6 | Define a pair (u,v) which consists of one element from the first array and one element from the second array. 7 | 8 | Find the k pairs (u1,v1),(u2,v2) ...(uk,vk) with the smallest sums. 9 | 10 | 11 | Solution 12 | === 13 | 14 | Find all possible pairs and sort 15 | 16 | O(k^2 logk) 17 | -------------------------------------------------------------------------------- /src/mergeSortedArray/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public void merge(int A[], int m, int B[], int n) { 3 | int i = m - 1; 4 | int j = n - 1; 5 | int k = m + n - 1; 6 | 7 | while (k >= 0) { 8 | if (j < 0) break; 9 | if (i >= 0 && A[i] > B[j]) A[k--] = A[i--]; 10 | else A[k--] = B[j--]; 11 | } 12 | } 13 | public static void main(String[] args) { 14 | 15 | } 16 | } -------------------------------------------------------------------------------- /src/min_stack/README.md: -------------------------------------------------------------------------------- 1 | Min Stack 2 | === 3 | 4 | Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. 5 | 6 | push(x) -- Push element x onto stack. 7 | pop() -- Removes the element on top of the stack. 8 | top() -- Get the top element. 9 | getMin() -- Retrieve the minimum element in the stack. 10 | 11 | Solution 12 | === 13 | 14 | Keep an auxilary stack for the min, update as elements are moved around -------------------------------------------------------------------------------- /src/generateParentheses/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. 5 | 6 | For example, given n = 3, a solution set is: 7 | 8 | "((()))", "(()())", "(())()", "()(())", "()()()" 9 | 10 | Solution 11 | === 12 | 13 | Brute force, backtrack and prune impossible branch 14 | 15 | memoize current left and right parentheses count (as parameters) 16 | for fast condition eval -------------------------------------------------------------------------------- /src/setMatrixZero/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 5 | 6 | 7 | Follow up: 8 | Did you use extra space? 9 | A straight forward solution using O(mn) space is probably a bad idea. 10 | A simple improvement uses O(m + n) space, but still not the best solution. 11 | Could you devise a constant space solution? 12 | 13 | Solution 14 | ==== 15 | 16 | Use first row and first col as markers -------------------------------------------------------------------------------- /src/3sum_smaller/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 <= i < j < k < n that satisfy the condition nums[i] + nums[j] + nums[k] < target. 4 | 5 | For example, given nums = [-2, 0, 1, 3], and target = 2. 6 | 7 | Return 2. Because there are two triplets which sums are less than 2: 8 | 9 | [-2, 0, 1] 10 | [-2, 0, 3] 11 | 12 | Follow up: 13 | Could you solve it in O(n^2) runtime? -------------------------------------------------------------------------------- /src/remove_linkedlist_element/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public ListNode removeElements(ListNode head, int val) { 3 | if (head == null) return null; 4 | ListNode hook = new ListNode(0); 5 | hook.next = head; 6 | ListNode prev = hook, curr = head; 7 | while (curr != null) { 8 | if (curr.val == val) { 9 | prev.next = curr.next; 10 | } else { 11 | prev = prev.next; 12 | } 13 | curr = curr.next; 14 | } 15 | return hook.next; 16 | } 17 | } -------------------------------------------------------------------------------- /src/wordBreakII/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. 4 | 5 | Return all such possible sentences. 6 | 7 | For example, given 8 | s = "catsanddog", 9 | dict = ["cat", "cats", "and", "sand", "dog"]. 10 | 11 | A solution is ["cats and dog", "cat sand dog"]. 12 | 13 | Solution 14 | === 15 | 16 | Brute force search with memoization for pruning -------------------------------------------------------------------------------- /src/basic_calculator_ii/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Implement a basic calculator to evaluate a simple expression string. 4 | 5 | The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should truncate toward zero. 6 | 7 | You may assume that the given expression is always valid. 8 | 9 | Some examples: 10 | "3+2*2" = 7 11 | " 3/2 " = 1 12 | " 3+5 / 2 " = 5 13 | 14 | Note: Do not use the eval built-in library function. -------------------------------------------------------------------------------- /src/max_product_subarray/README.md: -------------------------------------------------------------------------------- 1 | Maximum Product Subarray 2 | === 3 | Find the contiguous subarray within an array (containing at least one number) which has the largest product. 4 | 5 | For example, given the array [2,3,-2,4], 6 | the contiguous subarray [2,3] has the largest product = 6. 7 | 8 | Solution 9 | === 10 | Iterate through the array, maintain a running max (positive product) and a running min (negative product). 11 | 12 | If element is zero, clear previous product 13 | -------------------------------------------------------------------------------- /src/bitwiseAndRange/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int rangeBitwiseAnd(int m, int n) { 3 | int count = 0; 4 | while (n > m) { 5 | n >>= 1; 6 | m >>= 1; 7 | count++; 8 | } 9 | return (m & n) << (count); 10 | } 11 | public static void main(String[] args) { 12 | Solution s = new Solution(); 13 | System.out.println(s.rangeBitwiseAnd(1, 3)); 14 | System.out.println(s.rangeBitwiseAnd(5, 7)); 15 | System.out.println(s.rangeBitwiseAnd(3, 3)); 16 | } 17 | } -------------------------------------------------------------------------------- /src/uniquePaths/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). 4 | 5 | The 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). 6 | 7 | How many possible unique paths are there? 8 | 9 | 10 | Above is a 3 x 7 grid. How many possible unique paths are there? 11 | 12 | Note: m and n will be at most 100 -------------------------------------------------------------------------------- /src/longestValidParentheses/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a string containing just the characters '(' and ')', find the 4 | length of the longest valid (well-formed) parentheses substring. 5 | 6 | For "(()", the longest valid parentheses substring is "()", which has 7 | length = 2. 8 | 9 | Another example is ")()())", where the longest valid parentheses 10 | substring is "()()", which has length = 4. 11 | 12 | Solution 13 | === 14 | 15 | Maintain stack of last unmatched index in string -------------------------------------------------------------------------------- /src/maximalRectangle/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 4 | 5 | Solution 6 | === 7 | 8 | Very difficult problem, need to utilize the method in question "largest rectangle histogram" 9 | 10 | 1. make a new matrix, where each value is the number of consecutive '1's above it 11 | 12 | 2. apply "largest rectangle histogram" on each row 13 | 14 | 3. compare and return max from all rows -------------------------------------------------------------------------------- /src/product_of_array_except_self/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i]. 4 | 5 | Solve it without division and in O(n). 6 | 7 | For example, given [1,2,3,4], return [24,12,8,6]. 8 | 9 | Follow up: 10 | Could you solve it with constant space complexity? (Note: The output array does not count as extra space for the purpose of space complexity analysis.) -------------------------------------------------------------------------------- /src/combination_sum_iii/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. 4 | 5 | Ensure that numbers within the set are sorted in ascending order. 6 | 7 | 8 | Example 1: 9 | 10 | Input: k = 3, n = 7 11 | 12 | Output: 13 | 14 | [[1,2,4]] 15 | 16 | Example 2: 17 | 18 | Input: k = 3, n = 9 19 | 20 | Output: 21 | 22 | [[1,2,6], [1,3,5], [2,3,4]] -------------------------------------------------------------------------------- /src/count_complete_tree_nodes/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a complete binary tree, count the number of nodes. 5 | 6 | Definition of a complete binary tree from Wikipedia: 7 | In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h. 8 | 9 | Solution 10 | === 11 | 12 | Binary search + serialization of binary tree 13 | 14 | O(h^2) 15 | -------------------------------------------------------------------------------- /src/longestSubstringWith2UniqueChars/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a string, find the longest substring that contains only two unique characters. For example, given "abcbbbbcccbdddadacb", the longest substring that contains 2 unique character is "bcbbbbcccb". 5 | 6 | Solution 7 | === 8 | Two pointers slider, maintain a valid solution containing only two chars, by: 9 | 10 | 1. inc counter if char is like the two 11 | 12 | 2. remove until string contains the current char and the previous char -------------------------------------------------------------------------------- /src/number_1_bits/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // you need to treat n as an unsigned value 3 | public int hammingWeight(int n) { 4 | int cnt = 0; 5 | while (n != 0) { 6 | n &= n - 1; 7 | cnt++; 8 | } 9 | return cnt; 10 | } 11 | public static void main(String[] args) { 12 | Solution s = new Solution(); 13 | System.out.println(s.hammingWeight(11)); 14 | System.out.println(s.hammingWeight(-1)); 15 | } 16 | } -------------------------------------------------------------------------------- /src/pathSumII/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. 4 | 5 | For example: 6 | Given the below binary tree and sum = 22, 7 | 8 | 5 9 | / \ 10 | 4 8 11 | / / \ 12 | 11 13 4 13 | / \ / \ 14 | 7 2 5 1 15 | 16 | return 17 | 18 | [ 19 | [5,4,11,2], 20 | [5,8,4,5] 21 | ] 22 | 23 | Solution 24 | === 25 | Backtrack -------------------------------------------------------------------------------- /src/power_of_three/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... 5 | 6 | Example: 7 | (1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6]. 8 | (2) Given nums = [1, 3, 2, 2, 3, 1], one possible answer is [2, 3, 1, 3, 1, 2]. 9 | 10 | Note: 11 | You may assume all input has valid answer. 12 | 13 | Follow Up: 14 | Can you do it in O(n) time and/or in-place with O(1) extra space? 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/squareRoot/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @param x, an integer 3 | # @return an integer 4 | 5 | def sqrt(self, x): 6 | if x == 0: 7 | return 0 8 | if x == 1: 9 | return 1 10 | x0 = 1.0 11 | while True: 12 | x1 = (x0 + x / x0) / 2.0 13 | if (abs(x1 - x0) < 1): 14 | return int(x1) 15 | x0 = x1 16 | 17 | if __name__ == '__main__': 18 | s = Solution() 19 | print s.sqrt(9) 20 | -------------------------------------------------------------------------------- /src/stock2/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int maxProfit(int[] prices) { 3 | int prev = prices[0]; 4 | int ans = 0; 5 | for (int p : prices) { 6 | if (p > prev) ans += p - prev; 7 | prev = p; 8 | } 9 | return ans; 10 | } 11 | public static void main(String[] args) { 12 | Solution s = new Solution(); 13 | int res = s.maxProfit(new int[] {1, 2, 15, 4, 9, 2}); 14 | System.out.println(res); 15 | } 16 | } -------------------------------------------------------------------------------- /src/wiggle_sort_ii/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... 5 | 6 | Example: 7 | (1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6]. 8 | (2) Given nums = [1, 3, 2, 2, 3, 1], one possible answer is [2, 3, 1, 3, 1, 2]. 9 | 10 | Note: 11 | You may assume all input has valid answer. 12 | 13 | Follow Up: 14 | Can you do it in O(n) time and/or in-place with O(1) extra space? 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/validPalindrome/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a string, determine if it is a palindrome, considering only 4 | alphanumeric characters and ignoring cases. 5 | 6 | For example, 7 | "A man, a plan, a canal: Panama" is a palindrome. 8 | "race a car" is not a palindrome. 9 | 10 | Note: 11 | Have you consider that the string might be empty? This is a good 12 | question to ask during an interview. 13 | 14 | For the purpose of this problem, we define empty string as valid palindrome. 15 | -------------------------------------------------------------------------------- /src/letterCombinationPhoneNumber/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a digit string, return all possible letter combinations that the number could represent. 4 | 5 | A mapping of digit to letters (just like on the telephone buttons) is given below. 6 | 7 | ![alt tag](200px-Telephone-keypad2.svg.png) 8 | 9 | 10 | Input:Digit string "23" 11 | Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]. 12 | Note: 13 | Although the above answer is in lexicographical order, your answer could be in any order you want. -------------------------------------------------------------------------------- /src/levelOrder/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). 4 | 5 | For example: 6 | Given binary tree {3,9,20,#,#,15,7}, 7 | 3 8 | / \ 9 | 9 20 10 | / \ 11 | 15 7 12 | return its level order traversal as: 13 | [ 14 | [3], 15 | [9,20], 16 | [15,7] 17 | ] 18 | 19 | Solution 20 | === 21 | 22 | BFS, use a counter to track the size of current level, and break output into levels -------------------------------------------------------------------------------- /src/pathSum/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given 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. 4 | 5 | For example: 6 | Given the below binary tree and sum = 22, 7 | 8 | 5 9 | / \ 10 | 4 8 11 | / / \ 12 | 11 13 4 13 | / \ \ 14 | 7 2 1 15 | 16 | return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22. -------------------------------------------------------------------------------- /src/triangle/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. 4 | 5 | For example, given the following triangle 6 | [ 7 | [2], 8 | [3,4], 9 | [6,5,7], 10 | [4,1,8,3] 11 | ] 12 | The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11). 13 | 14 | Note: 15 | Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle. -------------------------------------------------------------------------------- /xcode-project/leetcode-debugger/leetcode-debugger/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | 15 | class Solution { 16 | public: 17 | 18 | }; 19 | 20 | int main(int argc, const char * argv[]) { 21 | Solution sol; 22 | cout << "Hello World" << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /src/expression_add_operators/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a string that contains only digits 0-9 and a target value, return all possibilities to add binary operators (not unary) +, -, or * between the digits so they evaluate to the target value. 5 | 6 | Examples: 7 | "123", 6 -> ["1+2+3", "1*2*3"] 8 | "232", 8 -> ["2*3+2", "2+3*2"] 9 | "105", 5 -> ["1*0+5","10-5"] 10 | "00", 0 -> ["0+0", "0-0", "0*0"] 11 | "3456237490", 9191 -> [] 12 | 13 | Solution 14 | === 15 | backtracking DFS 16 | -------------------------------------------------------------------------------- /src/number_of_islands/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Number of Islands Total 4 | 5 | Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. 6 | 7 | Example 1: 8 | 9 | 11110 10 | 11010 11 | 11000 12 | 00000 13 | Answer: 1 14 | 15 | Example 2: 16 | 17 | 11000 18 | 11000 19 | 00100 20 | 00011 21 | Answer: 3 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/two_sum/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given an array of integers, find two numbers such that they add up to a specific target number. 5 | 6 | The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based. 7 | 8 | You may assume that each input would have exactly one solution. 9 | 10 | Input: numbers={2, 7, 11, 15}, target=9 11 | Output: index1=1, index2=2 -------------------------------------------------------------------------------- /src/uniqueBST/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given n, how many structurally unique BST's (binary search trees) that 4 | store values 1...n? 5 | 6 | For example, 7 | Given n = 3, there are a total of 5 unique BST's. 8 | 9 | 1 3 3 2 1 10 | \ / / / \ \ 11 | 3 2 1 1 3 2 12 | / / \ \ 13 | 2 1 2 3 14 | 15 | Solution 16 | === 17 | Catalan number, search wikipedia for details -------------------------------------------------------------------------------- /src/data_stream_as_disjoint_intervals/README.md: -------------------------------------------------------------------------------- 1 | Problem === Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen so far as a list of disjoint intervals. For example, suppose the integers from the data stream are 1, 3, 7, 2, 6, ..., then the summary will be: [1, 1] [1, 1], [3, 3] [1, 1], [3, 3], [7, 7] [1, 3], [7, 7] [1, 3], [6, 7] Follow up: What if there are lots of merges and the number of disjoint intervals are small compared to the data stream's size? -------------------------------------------------------------------------------- /src/decodeWays/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | A message containing letters from A-Z is being encoded to numbers using the following mapping: 4 | 5 | 'A' -> 1 6 | 'B' -> 2 7 | ... 8 | 'Z' -> 26 9 | Given an encoded message containing digits, determine the total number of ways to decode it. 10 | 11 | For example, 12 | Given encoded message "12", it could be decoded as "AB" (1 2) or "L" (12). 13 | 14 | The number of ways decoding "12" is 2. 15 | 16 | Solution 17 | === 18 | 19 | Simple DP / Iteration -------------------------------------------------------------------------------- /src/simplifyPath/README.MD: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an absolute path for a file (Unix-style), simplify it. 4 | 5 | For example, 6 | path = "/home/", => "/home" 7 | path = "/a/./b/../../c/", => "/c" 8 | click to show corner cases. 9 | 10 | Corner Cases: 11 | Did you consider the case where path = "/../"? 12 | In this case, you should return "/". 13 | Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/". 14 | In this case, you should ignore redundant slashes and return "/home/foo". -------------------------------------------------------------------------------- /src/sameTree/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for binary tree 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution { 11 | public boolean isSameTree(TreeNode p, TreeNode q) { 12 | if (p == null && q == null) return true; 13 | if (p == null || q == null) return false; 14 | return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right); 15 | } 16 | } -------------------------------------------------------------------------------- /xcode-project/leetcode-debugger/leetcode-debugger.xcodeproj/xcuserdata/yubin.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | leetcode-debugger.xcscheme_^#shared#^_ 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/add_digits/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class Solution { 11 | public: 12 | int addDigits(int num) { 13 | if (num == 0) { 14 | return 0; 15 | } 16 | int m = num % 9; 17 | return m == 0 ? 9 : m; 18 | } 19 | }; 20 | 21 | int main() { 22 | Solution sol; 23 | cout << sol.addDigits(0) << endl; 24 | cout << sol.addDigits(121) << endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /src/uniquePathsII/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Follow up for "Unique Paths": 4 | 5 | Now consider if some obstacles are added to the grids. How many unique paths would there be? 6 | 7 | An obstacle and empty space is marked as 1 and 0 respectively in the grid. 8 | 9 | For example, 10 | There is one obstacle in the middle of a 3x3 grid as illustrated below. 11 | 12 | [ 13 | [0,0,0], 14 | [0,1,0], 15 | [0,0,0] 16 | ] 17 | The total number of unique paths is 2. 18 | 19 | Note: m and n will be at most 100. -------------------------------------------------------------------------------- /src/add_two_numbers/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | You are given two linked lists representing two non-negative numbers. 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. 5 | 6 | Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) 7 | Output: 7 -> 0 -> 8 8 | 9 | 10 | Solution 11 | === 12 | 13 | Similar to the 'add binary' problem, this is actually easier as the numbers are stored in reverse 14 | 15 | Just keep track of carry and generate a new node for each digit -------------------------------------------------------------------------------- /src/isValidSudoku/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. 4 | 5 | The Sudoku board could be partially filled, where empty cells are filled with the character '.'. 6 | 7 | ![alt tag](https://raw.githubusercontent.com/baiyubin/leetcode/master/isValidSudoku/250px-Sudoku-by-L2G-20050714.svg.png) 8 | 9 | A partially filled sudoku which is valid. 10 | 11 | Note: 12 | A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated. 13 | -------------------------------------------------------------------------------- /src/sumRootToLeaf/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. 5 | 6 | An example is the root-to-leaf path 1->2->3 which represents the number 123. 7 | 8 | Find the total sum of all root-to-leaf numbers. 9 | 10 | For example, 11 | 12 | 1 13 | / \ 14 | 2 3 15 | The root-to-leaf path 1->2 represents the number 12. 16 | The root-to-leaf path 1->3 represents the number 13. 17 | 18 | Return the sum = 12 + 13 = 25. -------------------------------------------------------------------------------- /src/400_nth_digit/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 400. Nth Digit 4 | Given an integer n, return the nth digit of the infinite integer sequence [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...]. 5 | 6 | 7 | Example 1: 8 | 9 | Input: n = 3 10 | Output: 3 11 | Example 2: 12 | 13 | Input: n = 11 14 | Output: 0 15 | Explanation: The 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which is part of the number 10. 16 | 17 | 18 | Constraints: 19 | 20 | 1 <= n <= 231 - 1 21 | 22 | 23 | Solution 24 | === 25 | integer arithmatic 26 | -------------------------------------------------------------------------------- /src/first_unique_character_in_a_string/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1. 4 | 5 | 6 | 7 | Example 1: 8 | 9 | Input: s = "leetcode" 10 | Output: 0 11 | Example 2: 12 | 13 | Input: s = "loveleetcode" 14 | Output: 2 15 | Example 3: 16 | 17 | Input: s = "aabb" 18 | Output: -1 19 | 20 | 21 | Constraints: 22 | 23 | 1 <= s.length <= 105 24 | s consists of only lowercase English letters. 25 | 26 | Solution 27 | === 28 | counter 29 | -------------------------------------------------------------------------------- /src/invert_binary_tree/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Invert a binary tree. 4 | 5 | 4 6 | / \ 7 | 2 7 8 | / \ / \ 9 | 1 3 6 9 10 | to 11 | 4 12 | / \ 13 | 7 2 14 | / \ / \ 15 | 9 6 3 1 16 | 17 | Trivia: 18 | This problem was inspired by this original tweet by Max Howell: 19 | Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off. -------------------------------------------------------------------------------- /src/invert_binary_tree/Solution.java: -------------------------------------------------------------------------------- 1 | class TreeNode { 2 | int val; 3 | TreeNode left; 4 | TreeNode right; 5 | TreeNode(int x) { val = x; } 6 | } 7 | 8 | public class Solution { 9 | public TreeNode invertTree(TreeNode root) { 10 | if (root == null) return null; 11 | if (root.left != null || root.right != null) { 12 | TreeNode swap = invertTree(root.left); 13 | root.left = invertTree(root.right); 14 | root.right = swap; 15 | } 16 | return root; 17 | } 18 | } -------------------------------------------------------------------------------- /src/longestConsecutiveSequence/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an unsorted array of integers, find the length of the longest consecutive elements sequence. 4 | 5 | For example, 6 | Given [100, 4, 200, 1, 3, 2], 7 | The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. 8 | 9 | Your algorithm should run in O(n) complexity. 10 | 11 | Solution 12 | ==== 13 | Hackish problem, need to use a hash set 14 | 15 | 1. put all numbers into hashset 16 | 17 | 2. retrieve one numbre *x* from hashset, and build a list around *x* -------------------------------------------------------------------------------- /src/palindrome_pairs/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that the concatenation of the two words, i.e. words[i] + words[j] is a palindrome. 4 | 5 | Example 1: 6 | Given words = ["bat", "tab", "cat"] 7 | Return [[0, 1], [1, 0]] 8 | The palindromes are ["battab", "tabbat"] 9 | Example 2: 10 | Given words = ["abcd", "dcba", "lls", "s", "sssll"] 11 | Return [[0, 1], [1, 0], [3, 2], [2, 4]] 12 | The palindromes are ["dcbaabcd", "abcddcba", "slls", "llssssll"] 13 | -------------------------------------------------------------------------------- /src/reverse_bits/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Reverse bits of a given 32 bits unsigned integer. 4 | 5 | For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). 6 | 7 | Follow up: 8 | If this function is called many times, how would you optimize it? 9 | 10 | Related problem: Reverse Integer 11 | 12 | Follow up 13 | === 14 | If needed, cache results of numbers between 0..255 in array. So the inverse of 32 bit int takes only 4 ops -------------------------------------------------------------------------------- /src/firstMissingPositive/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an unsorted integer array, find the first missing positive integer. 4 | 5 | For example, 6 | Given [1,2,0] return 3, 7 | and [3,4,-1,1] return 2. 8 | 9 | Your algorithm should run in O(n) time and uses constant space. 10 | 11 | Solution 12 | === 13 | 14 | Jumping pointer to put number in proper place 15 | 16 | if a number is in correct position, proceeed 17 | if not, replace this number in the correct position, marshal the replaced number 18 | 19 | scan from 1 to n to find missing -------------------------------------------------------------------------------- /src/palindromeNumber/palindrome.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | bool isPalindrome(int x) 5 | { 6 | if (x < 0) return false; 7 | int div = 1; 8 | while (x / div >= 10) 9 | { 10 | div *= 10; 11 | } 12 | while (x != 0) 13 | { 14 | int l = x / div; 15 | int r = x % 10; 16 | if (l != r) return false; 17 | x = (x % div) / 10; 18 | div /= 100; 19 | } 20 | return true; 21 | } 22 | 23 | int main() 24 | { 25 | cout << isPalindrome(101) << endl; 26 | } -------------------------------------------------------------------------------- /src/singleNumberII/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int singleNumber(int[] A) { 3 | int one = 0, two = 0, _one = 0, _two = 0; 4 | for (int a : A) { 5 | _one = (one ^ a) & ~two; 6 | _two = (one & a) | (two & ~a); 7 | // System.out.format("%d %d\n", _one, _two); 8 | one = _one; 9 | two = _two; 10 | } 11 | return one; 12 | } 13 | public static void main(String[] args) { 14 | Solution s = new Solution(); 15 | int[] A; 16 | A = new int[] {3, 3, 3, 2}; 17 | System.out.println(s.singleNumber(A)); 18 | } 19 | } -------------------------------------------------------------------------------- /src/uniqueBST/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int numTrees(int n) { 3 | if (n <= 1) return 1; 4 | int[] catalan = new int[n + 2]; 5 | catalan[0] = catalan[1] = 1; 6 | for (int i = 2; i <= n + 1; i++) { 7 | for (int j = 0; j < i; j++) { 8 | catalan[i] += catalan[j] * catalan[i - j]; 9 | } 10 | } 11 | return catalan[n + 1]; 12 | } 13 | public static void main(String[] args) { 14 | Solution s = new Solution(); 15 | 16 | System.out.println(s.numTrees(2)); 17 | System.out.println(s.numTrees(3)); 18 | } 19 | } -------------------------------------------------------------------------------- /src/uniqueBST2/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. 4 | 5 | For example, 6 | Given n = 3, your program should return all 5 unique BST's shown below. 7 | 8 | 1 3 3 2 1 9 | \ / / / \ \ 10 | 3 2 1 1 3 2 11 | / / \ \ 12 | 2 1 2 3 13 | 14 | Solution 15 | === 16 | 17 | DFS and enumerate all left and right subtrees -------------------------------------------------------------------------------- /src/zigzagConversion/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | The string "PAYPALISHIRING" is written in a zigzag pattern on a given 4 | number of rows like this: (you may want to display this pattern in a 5 | fixed font for better legibility) 6 | 7 | P A H N 8 | A P L S I I G 9 | Y I R 10 | And then read line by line: "PAHNAPLSIIGYIR" 11 | Write the code that will take a string and make this conversion given a 12 | number of rows: 13 | 14 | string convert(string text, int nRows); 15 | convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR". 16 | -------------------------------------------------------------------------------- /src/coin_change/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1. 5 | 6 | Example 1: 7 | coins = [1, 2, 5], amount = 11 8 | return 3 (11 = 5 + 5 + 1) 9 | 10 | Example 2: 11 | coins = [2], amount = 3 12 | return -1. 13 | 14 | Note: 15 | You may assume that you have an infinite number of each kind of coin. 16 | -------------------------------------------------------------------------------- /src/count_primes/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Count the number of prime numbers less than a non-negative number, n 4 | 5 | click to show more hints. 6 | 7 | References: 8 | How Many Primes Are There? 9 | 10 | Sieve of Eratosthenes 11 | 12 | 13 | Solution 14 | === 15 | [Sieve of atkin](http://en.wikipedia.org/wiki/Sieve_of_Atkin) 16 | 17 | [Lehmer method] (http://en.wikipedia.org/wiki/Prime-counting_function) 18 | 19 | [Lehmer method implementation] (http://www.codeitive.com/0QNejUPgXU/feasible-implementation-of-a-prime-counting-function-closed.html) -------------------------------------------------------------------------------- /src/sparse_matrix_multiplication/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given two sparse matrices A and B, return the result of AB. 4 | 5 | You may assume that A's column number is equal to B's row number. 6 | 7 | Example: 8 | 9 | A = [ 10 | [ 1, 0, 0], 11 | [-1, 0, 3] 12 | ] 13 | 14 | B = [ 15 | [ 7, 0, 0 ], 16 | [ 0, 0, 0 ], 17 | [ 0, 0, 1 ] 18 | ] 19 | 20 | 21 | | 1 0 0 | | 7 0 0 | | 7 0 0 | 22 | AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 | 23 | | 0 0 1 | 24 | 25 | -------------------------------------------------------------------------------- /src/substringWithCatAllWords/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters. 4 | 5 | For example, given: 6 | S: "barfoothefoobarman" 7 | L: ["foo", "bar"] 8 | 9 | You should return the indices: [0,9]. 10 | (order does not matter). 11 | 12 | Solution 13 | === 14 | 15 | Backtrack, use hashMap and counter to reduce string comparison -------------------------------------------------------------------------------- /src/binaryTreeZigzag/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a binary tree, print out the tree in zig zag level order (ie, from left to right, then right to left for the next level and alternate between). Output a newline after the end of each level. 4 | 5 | 3 6 | / \ 7 | 9 20 8 | / \ 9 | 15 7 10 | For example, the zig zag level order output of the tree above is: 11 | 12 | 3 13 | 20 9 14 | 15 7 15 | 16 | Solution 17 | === 18 | Use two collections to alternate on each level 19 | 20 | Reverse odd levels -------------------------------------------------------------------------------- /src/house_robber/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int rob(int[] num) { 3 | int m0 = 0, m1 = 0; 4 | for (int a : num) { 5 | int curr = m0 + a; 6 | m0 = Math.max(m0, m1); 7 | m1 = Math.max(curr, m0); 8 | } 9 | return Math.max(m0, m1); 10 | } 11 | public static void main(String[] args) { 12 | Solution s = new Solution(); 13 | int[] houses; 14 | 15 | houses = new int[] {1, 2, 3}; 16 | System.out.println(s.rob(houses)); 17 | 18 | houses = new int[] {1, 20, 3, 20}; 19 | System.out.println(s.rob(houses)); 20 | } 21 | } -------------------------------------------------------------------------------- /src/validBST/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a binary tree, determine if it is a valid binary search tree (BST). 5 | 6 | Assume a BST is defined as follows: 7 | 8 | Solution 9 | === 10 | The left subtree of a node contains only nodes with keys **less than** the node's key. 11 | The right subtree of a node contains only nodes with keys **greater than** the node's key. 12 | Both the left and right subtrees must also be binary search trees. 13 | 14 | Solution 2 15 | === 16 | Use an iterator of the binary search tree, and compare neighboring values. 17 | -------------------------------------------------------------------------------- /src/random_pick_index/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an integer array nums with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array. 4 | 5 | Implement the Solution class: 6 | 7 | Solution(int[] nums) Initializes the object with the array nums. 8 | int pick(int target) Picks a random index i from nums where nums[i] == target. If there are multiple valid i's, then each index should have an equal probability of returning. 9 | 10 | Solution 11 | === 12 | running random 13 | -------------------------------------------------------------------------------- /src/maxSubArray/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int maxSubArray(int[] A) { 3 | int ans = Integer.MIN_VALUE, curr = 0; 4 | for (int a : A) { 5 | curr = Math.max(curr + a, a); 6 | ans = Math.max(ans, curr); 7 | } 8 | return ans; 9 | } 10 | public static void main(String[] args) { 11 | Solution s = new Solution(); 12 | System.out.println(s.maxSubArray(new int[] { -2, 1, -3, 4, -1, 2, 1, -5, 4})); 13 | System.out.println(s.maxSubArray(new int[] { -1})); 14 | } 15 | } -------------------------------------------------------------------------------- /src/readNCharactersGivenRead4/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | The API: int read4(char *buf) reads 4 characters at a time from a file. 4 | The return value is the actual number of characters read. For example, it returns 3 if there is only 3 characters left in the file. 5 | By using the read4 API, implement the function int read(char *buf, int n) that reads n characters from the file. 6 | 7 | Note: 8 | The read function will only be called once for each test case. 9 | 10 | Solution 11 | === 12 | 13 | Hold buffer, keep track of current bufferPos, bufferLength, and eof 14 | -------------------------------------------------------------------------------- /src/super_ugly_number/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Write a program to find the n-th super ugly number. 4 | 5 | Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k. 6 | 7 | For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the first 12 super ugly numbers given primes = [2, 7, 13, 19] of size 4. 8 | 9 | Note: 10 | (1) 1 is a super ugly number for any given primes. 11 | (2) The given numbers in primes are in ascending order. 12 | (3) 0 < k ≤ 100, 0 < n ≤ 10 ** 6, 0 < primes[i] < 1000. 13 | -------------------------------------------------------------------------------- /src/wordSearch/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a 2D board and a word, find if the word exists in the grid. 4 | 5 | The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once. 6 | 7 | For example, 8 | Given board = 9 | 10 | [ 11 | ["ABCE"], 12 | ["SFCS"], 13 | ["ADEE"] 14 | ] 15 | word = "ABCCED", -> returns true, 16 | word = "SEE", -> returns true, 17 | word = "ABCB", -> returns false. -------------------------------------------------------------------------------- /src/binaryTreeRightSideView/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | ==== 3 | Binary Tree Right Side View Total Accepted: 101 Total Submissions: 501 My Submissions Question Solution 4 | Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. 5 | 6 | For example: 7 | Given the following binary tree, 8 | 1 <--- 9 | / \ 10 | 2 3 <--- 11 | \ \ 12 | 5 4 <--- 13 | You should return [1, 3, 4]. 14 | 15 | Solution 16 | === 17 | 18 | Layered order traversal 19 | 20 | -------------------------------------------------------------------------------- /src/longestPalindromeSubstring/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a string S, find the longest palindromic substring 4 | 5 | Solution 6 | === 7 | 8 | For each index in string lenght, expand from center and find palindromes. O(n^2) 9 | 10 | A more advanced solution is Manacher's solution, allowing reuse of previous matching result by mirroring. Complexity is O(n). 11 | 12 | References: 13 | [Leetcode explanation] (http://articles.leetcode.com/2011/11/longest-palindromic-substring-part-ii.html) 14 | [Chinese version] (http://www.felix021.com/blog/read.php?2040) 15 | 16 | -------------------------------------------------------------------------------- /src/reverseKgroup/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. 4 | 5 | If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is. 6 | 7 | You may not alter the values in the nodes, only nodes itself may be changed. 8 | 9 | Only constant memory is allowed. 10 | 11 | For example, 12 | 13 | Given this linked list: 1->2->3->4->5 14 | 15 | For k = 2, you should return: 2->1->4->3->5 16 | 17 | For k = 3, you should return: 3->2->1->4->5 -------------------------------------------------------------------------------- /src/searchInRotatedListII/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Search in Rotated Sorted Array 5 | Suppose a sorted array is rotated at some pivot unknown to you beforehand. 6 | 7 | (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). 8 | 9 | You are given a target value to search. If found in the array return its 10 | index, otherwise return -1. 11 | 12 | Follow up for "Search in Rotated Sorted Array": 13 | What if duplicates are allowed? 14 | 15 | Would this affect the run-time complexity? How and why? 16 | 17 | Write a function to determine if a given target is in the array. -------------------------------------------------------------------------------- /src/singleNumberII/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an array of integers, every element appears three times except for one. 4 | Find that single one. 5 | 6 | Note: 7 | Your algorithm should have a linear runtime complexity. Could you 8 | implement it without using extra memory? 9 | 10 | 11 | Solution 12 | === 13 | Extend the xor trick, keep 'bit count mod 1' and 'bit count mod 2', essentially base-3. 14 | 15 | Think of the final answer as the value that all bits count together the result should be all bits in a certain position mod 3 == 1, the corresponding value 16 | -------------------------------------------------------------------------------- /src/symmetricTree/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 4 | 5 | For example, this binary tree is symmetric: 6 | 7 | 1 8 | / \ 9 | 2 2 10 | / \ / \ 11 | 3 4 4 3 12 | 13 | But the following is not: 14 | 15 | 1 16 | / \ 17 | 2 2 18 | \ \ 19 | 3 3 20 | Note: 21 | Bonus points if you could solve it both recursively and iteratively. 22 | 23 | Solution 24 | === 25 | 26 | For the iterative solution, use level order traversal and compare two queues -------------------------------------------------------------------------------- /src/climbStairs/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int climbStairs(int n) { 3 | if (n <= 1) return 1; 4 | int a = 1, b = 1, t; 5 | for (int i = 1; i < n; i++) { 6 | t = a + b; 7 | a = b; 8 | b = t; 9 | } 10 | return b; 11 | } 12 | public static void main(String[] args) { 13 | Solution s = new Solution(); 14 | for (int i = 2; i < 10; i++) { 15 | int r = s.climbStairs(i); 16 | System.out.format("%d\t%d\n",i, r); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /src/lexicographical_numbers/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an integer n, return all the numbers in the range [1, n] sorted in lexicographical order. 4 | 5 | You must write an algorithm that runs in O(n) time and uses O(1) extra space. 6 | 7 | 8 | 9 | Example 1: 10 | 11 | Input: n = 13 12 | Output: [1,10,11,12,13,2,3,4,5,6,7,8,9] 13 | Example 2: 14 | 15 | Input: n = 2 16 | Output: [1,2] 17 | 18 | 19 | Constraints: 20 | 21 | 1 <= n <= 5 * 104 22 | 23 | 24 | Solution 25 | === 26 | numerical construction. use a lot of examples to construct state transitions. 27 | -------------------------------------------------------------------------------- /src/mergeKSorted/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 4 | 5 | Solution 6 | ==== 7 | Solution 1 8 | --- 9 | Put all lists into a queue, while queue size is bigger than one, pop two lists from queue, merge them to one, and put back to queue. O(n lg k) 10 | 11 | 12 | Solution 2 13 | --- 14 | Maintain a heap of k elements, with invariant of every lists' top node 15 | 16 | For each iteration, pop the top node in the heap at each loop and add a new node from the same original list. O(n lg k) -------------------------------------------------------------------------------- /src/countAndSay/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | The count-and-say sequence is the sequence of integers beginning as follows: 4 | 5 | 1, 11, 21, 1211, 111221, ... 6 | 7 | 1 is read off as "one 1" or 11. 8 | 11 is read off as "two 1s" or 21. 9 | 21 is read off as "one 2, then one 1" or 1211. 10 | 11 | Given an integer n, generate the nth sequence. 12 | 13 | Note: The sequence of integers will be represented as a string. 14 | 15 | Solution 16 | === 17 | 18 | Two pointers sliding 19 | 20 | Use StringBuilder/StringBuffer to build up sequence 21 | 22 | Iterate n times -------------------------------------------------------------------------------- /src/candy/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def candy(self, ratings): 3 | left = [1] * len(ratings) 4 | right = [1] * len(ratings) 5 | for i in range(1, len(ratings)): 6 | if ratings[i - 1] < ratings[i]: 7 | left[i] = left[i - 1] + 1 8 | 9 | for i in range(len(ratings) - 2, -1, -1): 10 | if ratings[i + 1] < ratings[i]: 11 | right[i] = right[i + 1] + 1 12 | 13 | total = 0 14 | for i in range(len(ratings)): 15 | total += max(left[i], right[i]) 16 | return total 17 | -------------------------------------------------------------------------------- /src/reverse_bits/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // you need treat n as an unsigned value 3 | public int reverseBits(int n) { 4 | int ret = 0; 5 | for (int i = 0; i < 32; ++i) { 6 | int curr = 1 & n; 7 | ret <<= 1; 8 | ret |= curr; 9 | n >>= 1; 10 | } 11 | return ret; 12 | } 13 | public static void main(String[] args) { 14 | Solution s = new Solution(); 15 | System.out.println(s.reverseBits(2)); 16 | System.out.println(s.reverseBits(43261596)); 17 | } 18 | } -------------------------------------------------------------------------------- /src/searchInMatrix/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: 4 | 5 | Integers in each row are sorted from left to right. 6 | The first integer of each row is greater than the last integer of the previous row. 7 | For example, 8 | 9 | Consider the following matrix: 10 | 11 | [ 12 | [1, 3, 5, 7], 13 | [10, 11, 16, 20], 14 | [23, 30, 34, 50] 15 | ] 16 | Given target = 3, return true. 17 | 18 | Solution 19 | === 20 | 21 | Transform 2D matrix indices into sequence indices -------------------------------------------------------------------------------- /src/isomorphic_strings/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given two strings s and t, determine if they are isomorphic. 4 | 5 | Two strings are isomorphic if the characters in s can be replaced to get t. 6 | 7 | All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself. 8 | 9 | For example, 10 | Given "egg", "add", return true. 11 | 12 | Given "foo", "bar", return false. 13 | 14 | Given "paper", "title", return true. 15 | 16 | Solution 17 | === 18 | Hashmap 19 | -------------------------------------------------------------------------------- /src/line_reflection/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given set of points. 4 | 5 | Example 1: 6 | Given points = [[1,1],[-1,1]], return true. 7 | 8 | Example 2: 9 | Given points = [[1,1],[-1,-1]], return false. 10 | 11 | Follow up: 12 | Could you do better than O(n2)? 13 | 14 | Hint: 15 | 16 | Find the smallest and largest x-value for all points. 17 | If there is a line then it should be at y = (minX + maxX) / 2. 18 | For each point, make sure that it has a reflected point in the opposite side. 19 | -------------------------------------------------------------------------------- /src/range_sum_query_mutable/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. 5 | 6 | The update(i, val) function modifies nums by updating the element at index i to val. 7 | Example: 8 | Given nums = [1, 3, 5] 9 | 10 | sumRange(0, 2) -> 9 11 | update(1, 2) 12 | sumRange(0, 2) -> 8 13 | Note: 14 | The array is only modifiable by the update function. 15 | You may assume the number of calls to update and sumRange function is distributed evenly. 16 | 17 | 18 | Solution 19 | === 20 | Binary indexed tree 21 | -------------------------------------------------------------------------------- /src/singleNumber/main.py: -------------------------------------------------------------------------------- 1 | # Given an array of integers, every element appears twice except for one. 2 | # Find that single one. 3 | 4 | # Note: 5 | # Your algorithm should have a linear runtime complexity. Could you 6 | # implement it without using extra memory? 7 | import operator 8 | 9 | 10 | class Solution: 11 | # @param A, a list of integer 12 | # @return an integer 13 | 14 | def singleNumber(self, A): 15 | return reduce(operator.xor, A) 16 | 17 | if __name__ == '__main__': 18 | A = [1, 1, 3, 3, 10000] 19 | s = Solution() 20 | print s.singleNumber(A) 21 | -------------------------------------------------------------------------------- /src/smallest_rectangle_enclosing_black_pixels/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (x, y) of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels. 4 | 5 | For example, given the following image: 6 | [ 7 | "0010", 8 | "0110", 9 | "0100" 10 | ] 11 | 12 | and x = 0, y = 2, Return 6. 13 | -------------------------------------------------------------------------------- /src/preOrderTraversal/main.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | 8 | class Solution: 9 | # @param root, a tree node 10 | # @return a list of integers 11 | def preorderTraversal(self, root): 12 | ret = [] 13 | def trans(root): 14 | if root != None: 15 | ret.append(root.val) 16 | trans(root.left) 17 | trans(root.right) 18 | trans(root) 19 | return ret -------------------------------------------------------------------------------- /src/binary_search_tree_iterator/README.md: -------------------------------------------------------------------------------- 1 | Binary Search Tree Iterator 2 | === 3 | 4 | Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. 5 | 6 | Calling next() will return the next smallest number in the BST. 7 | 8 | Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree. 9 | 10 | Solution 11 | === 12 | Keep a stack and a current pointer, keep pushing node in stack when traversing into left tree 13 | 14 | When poping from stack, output node value, and traverse the right subtree -------------------------------------------------------------------------------- /src/find_minimum_in_rotated_sorted_array/README.md: -------------------------------------------------------------------------------- 1 | Find Minimum in Rotated Sorted Array 2 | === 3 | 4 | Suppose a sorted array is rotated at some pivot unknown to you beforehand. 5 | 6 | (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). 7 | 8 | Find the minimum element. 9 | 10 | You may assume no duplicate exists in the array. 11 | 12 | Solution 13 | === 14 | Binary search, discuss the possibility of the segment under watch 15 | 16 | Follow up 17 | === 18 | If duplicates are allowed, shift the windown of search by one, this will definitely harm the performance if a lot of duplicates are found. 19 | -------------------------------------------------------------------------------- /src/reverse_linkedlist/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { val = x; } 7 | * } 8 | */ 9 | public class Solution { 10 | public ListNode reverseList(ListNode head) { 11 | if (head == null) return null; 12 | ListNode newList = head, curr = head.next; 13 | newList.next = null; 14 | while (curr != null) { 15 | ListNode swap = curr; 16 | curr = curr.next; 17 | swap.next = newList; 18 | newList = swap; 19 | } 20 | return newList; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/candy/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | There are N children standing in a line. Each child is assigned a rating value. 4 | 5 | You are giving candies to these children subjected to the following requirements: 6 | 7 | Each child must have at least one candy. 8 | 9 | Children with a higher rating get more candies than their neighbors. 10 | 11 | Solution 12 | === 13 | Give local minimum only 1 14 | 15 | Scan from left to right, find increasing rating and mark as neighbor + 1 16 | 17 | Scan from right to left, find increasing rating and mark as neighbor + 1 18 | 19 | Find max of two candy #s 20 | -------------------------------------------------------------------------------- /src/find_minimum_in_rotated_sorted_array_II/README.md: -------------------------------------------------------------------------------- 1 | Find Minimum in Rotated Sorted Array 2 | === 3 | 4 | Suppose a sorted array is rotated at some pivot unknown to you beforehand. 5 | 6 | (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). 7 | 8 | Find the minimum element. 9 | 10 | You may assume no duplicate exists in the array. 11 | 12 | Solution 13 | === 14 | Binary search, discuss the possibility of the segment under watch 15 | 16 | Follow up 17 | === 18 | If duplicates are allowed, shift the windown of search by one, this will definitely harm the performance if a lot of duplicates are found. 19 | -------------------------------------------------------------------------------- /src/flatten_nested_list_iterator/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a nested list of integers, implement an iterator to flatten it. 4 | 5 | Each element is either an integer, or a list -- whose elements may also be integers or other lists. 6 | 7 | Example 1: 8 | Given the list [[1,1],2,[1,1]], 9 | 10 | By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1]. 11 | 12 | Example 2: 13 | Given the list [1,[4,[6]]], 14 | 15 | By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6]. 16 | 17 | -------------------------------------------------------------------------------- /src/intersection_of_two_arrays_ii/README.md: -------------------------------------------------------------------------------- 1 | Problem === Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. Note: Each element in the result should appear as many times as it shows in both arrays. The result can be in any order. Follow up: What if the given array is already sorted? How would you optimize your algorithm? What if nums1's size is small compared to num2's size? Which algorithm is better? What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once? -------------------------------------------------------------------------------- /src/longest_increasing_subsequence/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given an unsorted array of integers, find the length of longest increasing subsequence. 4 | 5 | For example, 6 | Given [10, 9, 2, 5, 3, 7, 101, 18], 7 | The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than one LIS combination, it is only necessary for you to return the length. 8 | 9 | Your algorithm should run in O(n2) complexity. 10 | 11 | Follow up: Could you improve it to O(n log n) time complexity? 12 | 13 | Solution 14 | === 15 | DP + binary search 16 | 17 | O (n log k) 18 | -------------------------------------------------------------------------------- /src/russian_doll_envelopes/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope. 4 | 5 | What is the maximum number of envelopes can you Russian doll? (put one inside other) 6 | 7 | Example: 8 | Given envelopes = [[5,4],[6,4],[6,7],[2,3]], the maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]). 9 | 10 | Solution 11 | === 12 | 13 | Longest increasing subsequence 14 | -------------------------------------------------------------------------------- /src/word_pattern_ii/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a pattern and a string str, find if str follows the same pattern. 5 | Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty substring in str. 6 | 7 | Examples: 8 | pattern = "abab", str = "redblueredblue" should return true. 9 | pattern = "aaaa", str = "asdasdasdasd" should return true. 10 | pattern = "aabb", str = "xyzabcxzyabc" should return false. 11 | 12 | Notes: You may assume both pattern and str contains only lowercase letters. 13 | 14 | Solution 15 | === 16 | backtrack 17 | -------------------------------------------------------------------------------- /src/copyListWithRandomPointer/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. 4 | 5 | Return a deep copy of the list. 6 | 7 | Solution 8 | === 9 | Solution 1: O(n) time, O(n) space 10 | Create hash map with entries (old\_node, new_node) 11 | 12 | Connect edges afterwards 13 | 14 | Solution 2: O(n) time, O(1) space 15 | 16 | Copy in three passes: 17 | 18 | 1. copy all nodes and insert inside list 19 | 20 | 2. link the random pointer for copied 21 | 22 | 3. separate the copied to a new list 23 | -------------------------------------------------------------------------------- /src/factorial_trailing_zeroes/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int trailingZeroes(int n) { 3 | if (n < 0) return -1; 4 | if (n < 5) return 0; 5 | int ret = 0; 6 | while (n > 0) { 7 | n /= 5; 8 | ret += n; 9 | } 10 | return ret; 11 | } 12 | public static void main(String[] args) { 13 | Solution s = new Solution(); 14 | System.out.format("%d\n", s.trailingZeroes(5)); 15 | System.out.format("%d\n", s.trailingZeroes(10)); 16 | System.out.format("%d\n", s.trailingZeroes(25)); 17 | } 18 | } -------------------------------------------------------------------------------- /src/group_shifted_strings/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a string, we can “shift” each of its letter to its successive letter, for example: “abc” -> “bcd”. We can keep “shifting” which forms the sequence: 5 | 6 | "abc" -> "bcd" -> ... -> "xyz" 7 | 8 | Given a list of strings which contains only lowercase alphabets, group all strings that belong to the same shifting sequence. 9 | 10 | For example, 11 | 12 | given: ["abc", "bcd", "acef", "xyz", "az", "ba", "a", "z"], Return: 13 | 14 | [ 15 | ["abc","bcd","xyz"], 16 | ["az","ba"], 17 | ["acef"], 18 | ["a","z"] 19 | ] 20 | -------------------------------------------------------------------------------- /src/odd_even_linked_list/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. 4 | 5 | You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity. 6 | 7 | Example: 8 | Given 1->2->3->4->5->NULL, 9 | return 1->3->5->2->4->NULL. 10 | 11 | Note: 12 | The relative order inside both the even and odd groups should remain as it was in the input. 13 | The first node is considered odd, the second node even and so on ... 14 | -------------------------------------------------------------------------------- /src/partitionPainter/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | You have to paint N boards of length {A0, A1, A2 … AN-1}. There are K painters available and you are also given how much time a painter takes to paint 1 unit of board. You have to get this job done as soon as possible under the constraints that any painter will only paint continuous sections of board, say board {2, 3, 4} or only board {1} or nothing but not board {2, 4, 5}. 5 | 6 | Solution 7 | === 8 | 9 | Two solutions: 10 | 11 | 1. Binary search for answer, use greedy method to validate each number 12 | 13 | 2. DP, state will be dp[step][number of painters] -------------------------------------------------------------------------------- /src/populateNextRightII/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Follow up for problem "Populating Next Right Pointers in Each Node". 5 | 6 | What if the given tree could be any binary tree? Would your previous solution still work? 7 | 8 | Note: 9 | 10 | You may only use constant extra space. 11 | For example, 12 | Given the following binary tree, 13 | 14 | 1 15 | / \ 16 | 2 3 17 | / \ \ 18 | 4 5 7 19 | After calling your function, the tree should look like: 20 | 21 | 1 -> NULL 22 | / \ 23 | 2 -> 3 -> NULL 24 | / \ \ 25 | 4-> 5 -> 7 -> NULL -------------------------------------------------------------------------------- /src/shortest_word_distance_iii/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. 5 | 6 | word1 and word2 may be the same and they represent two individual words in the list. 7 | 8 | For example, 9 | 10 | Assume that words = ["practice", "makes", "perfect", "coding", "makes"]. 11 | 12 | Given word1 = "makes", word2 = "coding", return 1. Given word1 = "makes", word2 = "makes", return 3. 13 | 14 | Note: 15 | You may assume that word1 does not equal to word2, and word1 and word2 are both in the list. -------------------------------------------------------------------------------- /src/add_digits/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. 5 | 6 | For example: 7 | 8 | Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. 9 | 10 | Follow up: 11 | Could you do it without any loop/recursion in O(1) runtime? 12 | 13 | Hint: 14 | 15 | A naive implementation of the above process is trivial. Could you come up with other methods? 16 | What are all the possible results? 17 | How do they occur, periodically or randomly? 18 | You may find this Wikipedia article useful. -------------------------------------------------------------------------------- /src/happy_number/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Write an algorithm to determine if a number is "happy". 4 | 5 | A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers. 6 | 7 | Example: 19 is a happy number 8 | 9 | 12 + 92 = 82 10 | 82 + 22 = 68 11 | 62 + 82 = 100 12 | 12 + 02 + 02 = 1 13 | 14 | Solution 15 | === 16 | hashset -------------------------------------------------------------------------------- /src/rotate_array/README.md: -------------------------------------------------------------------------------- 1 | Question 2 | ====== 3 | 4 | Rotate an array of n elements to the right by k steps. 5 | 6 | For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 7 | 8 | Note: 9 | Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem. 10 | 11 | 12 | Hint: 13 | Could you do it in-place with O(1) extra space? 14 | Credits: 15 | Special thanks to @Freezen for adding this problem and creating all test cases. 16 | 17 | Solution 18 | ====== 19 | 20 | 1. reverse all array 21 | 2. reverse first k 22 | 3. reverse remaining 23 | -------------------------------------------------------------------------------- /src/different_ways_to_add_parentheses/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *. 5 | 6 | 7 | Example 1 8 | Input: "2-1-1". 9 | 10 | ((2-1)-1) = 0 11 | (2-(1-1)) = 2 12 | Output: [0, 2] 13 | 14 | 15 | Example 2 16 | Input: "2*3-4*5" 17 | 18 | (2*(3-(4*5))) = -34 19 | ((2*3)-(4*5)) = -14 20 | ((2*(3-4))*5) = -10 21 | (2*((3-4)*5)) = -10 22 | (((2*3)-4)*5) = 10 23 | Output: [-34, -14, -10, -10, 10] -------------------------------------------------------------------------------- /src/largest_divisible_subset/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of elements in this subset satisfies: Si % Sj = 0 or Sj % Si = 0. 4 | 5 | If there are multiple solutions, return any subset is fine. 6 | 7 | Example 1: 8 | 9 | nums: [1,2,3] 10 | 11 | Result: [1,2] (of course, [1,3] will also be ok) 12 | Example 2: 13 | 14 | nums: [1,2,4,8] 15 | 16 | Result: [1,2,4,8] 17 | 18 | Solution 19 | === 20 | - similar to 'longest increasing subsequence' 21 | - if (a < b && b % a == 0) && (b < c && c % b == 0), then we have c % a == 0 22 | 23 | -------------------------------------------------------------------------------- /src/triangle/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @param triangle, a list of lists of integers 3 | # @return an integer 4 | def minimumTotal(self, triangle): 5 | if len(triangle) == 0: 6 | return 0 7 | for rowI in range(len(triangle)-1, 0, -1): 8 | r = triangle[rowI] 9 | rr = triangle[rowI - 1] 10 | 11 | for i in range(len(rr)): 12 | rr[i] += min(r[i], r[i + 1]) 13 | 14 | return triangle[0][0] 15 | 16 | if __name__ == '__main__': 17 | tri = [[2], [3,4], [6,5,7], [4,1,8,3] ] 18 | s = Solution() 19 | print s.minimumTotal(tri) -------------------------------------------------------------------------------- /src/flip_game_ii/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip twoconsecutive "++" into "--". The game ends when a person can no longer make a move and therefore the other person will be the winner. 5 | 6 | Write a function to determine if the starting player can guarantee a win. 7 | For example, given s = "++++", return true. The starting player can guarantee a win by flipping the middle "++" to become "+--+". 8 | 9 | Follow up: 10 | Derive your algorithm's runtime complexity. 11 | -------------------------------------------------------------------------------- /src/valid_anagram/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | class Solution { 10 | public: 11 | bool isAnagram(string s, string t) { 12 | vector sv(s.begin(), s.end()); 13 | vector tv(t.begin(), t.end()); 14 | sort(sv.begin(), sv.end()); 15 | sort(tv.begin(), tv.end()); 16 | return sv == tv; 17 | } 18 | }; 19 | 20 | int main() { 21 | Solution sol; 22 | cout << sol.isAnagram("anagram", "nagaram") << endl; 23 | cout << sol.isAnagram("car", "nar") << endl; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /src/find_the_duplicate_number/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. 5 | Note: 6 | You must not modify the array (assume the array is read only). 7 | You must use only constant, O(1) extra space. 8 | Your runtime complexity should be less than O(n^2). 9 | There is only one duplicate number in the array, but it could be repeated more than once. 10 | 11 | 12 | Solution 13 | === 14 | Use binary search and counter functional 15 | -------------------------------------------------------------------------------- /src/fraction_to_recurring_decimal/README.md: -------------------------------------------------------------------------------- 1 | Fraction to Recurring Decimal 2 | === 3 | Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. 4 | 5 | If the fractional part is repeating, enclose the repeating part in parentheses. 6 | 7 | For example, 8 | 9 | Given numerator = 1, denominator = 2, return "0.5". 10 | Given numerator = 2, denominator = 1, return "2". 11 | Given numerator = 2, denominator = 3, return "0.(6)". 12 | 13 | Solution 14 | === 15 | Long division 16 | 17 | Keep a list of seen digits and corresponding numerators, search recurring at first duplicate -------------------------------------------------------------------------------- /src/insertInterval/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). 4 | 5 | You may assume that the intervals were initially sorted according to their start times. 6 | 7 | Example 1: 8 | Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9]. 9 | 10 | Example 2: 11 | Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16]. 12 | 13 | This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10]. 14 | 15 | 16 | Solution 17 | === 18 | 19 | Insert into sorted intervals, merge adjacent ones, O(n) -------------------------------------------------------------------------------- /src/postOrderTraversal/main.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node 2 | # class TreeNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.left = None 6 | # self.right = None 7 | 8 | 9 | class Solution: 10 | # @param root, a tree node 11 | # @return a list of integers 12 | 13 | def postorderTraversal(self, root): 14 | ret = [] 15 | 16 | def traversal(root): 17 | if root != None: 18 | traversal(root.left) 19 | traversal(root.right) 20 | ret.append(root.val) 21 | traversal(root) 22 | return ret 23 | -------------------------------------------------------------------------------- /src/search_a_2d_matrix_ii/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: 5 | 6 | Integers in each row are sorted in ascending from left to right. 7 | Integers in each column are sorted in ascending from top to bottom. 8 | For example, 9 | 10 | Consider the following matrix: 11 | 12 | [ 13 | [1, 4, 7, 11, 15], 14 | [2, 5, 8, 12, 19], 15 | [3, 6, 9, 16, 22], 16 | [10, 13, 14, 17, 24], 17 | [18, 21, 23, 26, 30] 18 | ] 19 | 20 | Given target = 5, return true. 21 | 22 | Given target = 20, return false. -------------------------------------------------------------------------------- /src/excel_sheet_column_number/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int titleToNumber(String s) { 3 | int ret = 0; 4 | for (char c : s.toCharArray()) { 5 | ret *= 26; 6 | ret += c - 'A' + 1; 7 | } 8 | return ret; 9 | } 10 | public static void main(String[] args) { 11 | Solution s = new Solution(); 12 | System.out.format("%d\n", s.titleToNumber("A")); 13 | System.out.format("%d\n", s.titleToNumber("AA")); 14 | System.out.format("%d\n", s.titleToNumber("AB")); 15 | System.out.format("%d\n", s.titleToNumber("AAA")); 16 | } 17 | } -------------------------------------------------------------------------------- /src/remove_duplicate_from_sorted_array/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @param a list of integers 3 | # @return an integer 4 | def removeDuplicates(self, A): 5 | i = 0 6 | ii = 1 7 | N = len(A) 8 | count = 0 9 | while ii < N: 10 | if A[i] == A[ii]: 11 | ii += 1 12 | count += 1 13 | else: 14 | i += 1 15 | A[i] = A[ii] 16 | ii += 1 17 | return len(A) - count 18 | 19 | if __name__ == '__main__': 20 | s = Solution() 21 | print s.removeDuplicates([1,1,2,2, 2, 3, 4, 100]) 22 | 23 | -------------------------------------------------------------------------------- /src/repeated_DNA_sequences/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA. 4 | 5 | Write a function to find all the 10-letter-long sequences (substrings) that occur more than once in a DNA molecule. 6 | 7 | For example, 8 | 9 | Given s = "AAAAACCCCCAAAAACCCCCCAAAAAGGGTTT", 10 | 11 | Return: 12 | ["AAAAACCCCC", "CCCCCAAAAA"]. 13 | 14 | Solution 15 | === 16 | Serialize the sequence of 10 chars into a 20 bits int. 17 | 18 | Use hashset to keep seen and duplicated sequences. -------------------------------------------------------------------------------- /src/anagram/solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @param strs, a list of strings 3 | # @return a list of strings 4 | def anagrams(self, strs): 5 | d = {} 6 | for s in strs: 7 | sortedS = ''.join(sorted(s)) 8 | if sortedS in d: 9 | d[sortedS].append(s) 10 | else: 11 | d[sortedS] = [s] 12 | ret = [] 13 | for k in d: 14 | if len(d[k]) > 1: 15 | ret.extend(d[k]) 16 | return ret 17 | 18 | if __name__ == '__main__': 19 | s = Solution() 20 | strs = ['bac', 'bb', 'bb', 'abc', 'ss'] 21 | print s.anagrams(strs) -------------------------------------------------------------------------------- /src/bomb_enemy/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given a 2D grid, each cell is either a wall 'Y', an enemy 'X' or empty '0' (the number zero), return the maximum enemies you can kill using one bomb. 5 | The bomb kills all the enemies in the same row and column from the planted point until it hits the wall since the wall is too strong to be destroyed. 6 | Note that you can only put the bomb at an empty cell. 7 | 8 | Example: 9 | 10 | For the given grid 11 | 12 | 0 X 0 0 13 | X 0 Y X 14 | 0 X 0 0 15 | 16 | return 3. (Placing a bomb at (1,1) kills 3 enemies) 17 | 18 | Solution 19 | === 20 | Running sum, O(n * m) time and space 21 | -------------------------------------------------------------------------------- /src/first_bad_version/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad. 4 | 5 | Suppose you have n versions [1, 2, ..., n] and you want to find out the first bad one, which causes all the following ones to be bad. 6 | 7 | You are given an API bool isBadVersion(version) which will return whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API. 8 | -------------------------------------------------------------------------------- /src/rectangle_area/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { 3 | int ax = Math.max(A, E); 4 | int ay = Math.max(B, F); 5 | int bx = Math.min(C, G); 6 | int by = Math.min(D, H); 7 | int area1 = (C - A) * (D - B); 8 | int area2 = (G - E) * (H - F); 9 | int overlap = 0; 10 | if (ax < bx && ay < by) { 11 | overlap = (bx - ax) * (by - ay); 12 | } 13 | return area1 + area2 - overlap; 14 | } 15 | public static void main(String[] args) { 16 | Solution s = new Solution(); 17 | System.out.println(s.computeArea(-2, -2, 2, 2, -2, -2, 2, 2)); 18 | } 19 | } -------------------------------------------------------------------------------- /src/sort_transformed_array/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a sorted array of integers nums and integer values a, b and c. Apply a function of the form f(x) = ax2 + bx + c to each element x in the array. 4 | 5 | The returned array must be in sorted order. 6 | 7 | Expected time complexity: O(n) 8 | 9 | Example: 10 | nums = [-4, -2, 2, 4], a = 1, b = 3, c = 5, 11 | 12 | Result: [3, 9, 15, 33] 13 | 14 | nums = [-4, -2, 2, 4], a = -1, b = 3, c = 5 15 | 16 | Result: [-23, -5, 1, 7] 17 | 18 | Solution 19 | === 20 | 21 | Parabola trends 22 | 23 | if a >= 0, bigger results are at both ends of array 24 | otherwise, bigger values appear in middle 25 | -------------------------------------------------------------------------------- /src/stock2/main.py: -------------------------------------------------------------------------------- 1 | # Say you have an array for which the ith element is the price of a given 2 | # stock on day i. 3 | 4 | # unlimited buy and sell 5 | ''' 6 | Created on May 13, 2013 7 | @author: Yubin Bai 8 | ''' 9 | 10 | class Solution: 11 | # @param prices, a list of integer 12 | # @return an integer 13 | def maxProfit(self, prices): 14 | if len(prices) == 0: 15 | return 0 16 | return sum(prices[i] - prices[i - 1] for i in range(1, len(prices)) if prices[i] > prices[i - 1]) 17 | 18 | if __name__ == '__main__': 19 | data = [1, 2, 15, 4, 9, 2] 20 | s = Solution() 21 | print(s.maxProfit(data)) 22 | -------------------------------------------------------------------------------- /src/surroundedRegions/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given a 2D board containing 'X' and 'O', capture all regions surrounded 4 | by 'X'. 5 | 6 | A region is captured by flipping all 'O's into 'X's in that surrounded 7 | region . 8 | 9 | For example, 10 | X X X X 11 | X O O X 12 | X X O X 13 | X O X X 14 | After running your function, the board should be: 15 | 16 | X X X X 17 | X X X X 18 | X X X X 19 | X O X X 20 | 21 | Solution 22 | === 23 | BFS from borders of the matrix, kind of a hackish solution 24 | 25 | Notice some optimizations about the bfs, the frontier contains more nodes than the rest of it combined! -------------------------------------------------------------------------------- /src/wordLadderII/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: 4 | 5 | Only one letter can be changed at a time 6 | Each intermediate word must exist in the dictionary 7 | For example, 8 | 9 | Given: 10 | start = "hit" 11 | end = "cog" 12 | dict = ["hot","dot","dog","lot","log"] 13 | Return 14 | [ 15 | ["hit","hot","dot","dog","cog"], 16 | ["hit","hot","lot","log","cog"] 17 | ] 18 | Note: 19 | All words have the same length. 20 | All words contain only lowercase alphabetic characters. -------------------------------------------------------------------------------- /src/3Sum/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | ==== 3 | Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. 4 | 5 | Note: 6 | ---- 7 | Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) 8 | The solution set must not contain duplicate triplets. 9 | 10 | For example, given array S = {-1 0 1 2 -1 -4}, 11 | 12 | A solution set is: 13 | (-1, 0, 1) 14 | (-1, -1, 2) 15 | 16 | Solution 17 | ==== 18 | 19 | First sort the array, then use two pointers sliding to find unique enumerations of *two sums* 20 | 21 | Complexity O(n^2) 22 | -------------------------------------------------------------------------------- /src/add_and_search_word_data_structure_design/README.md: -------------------------------------------------------------------------------- 1 | Add and Search Word - Data structure design 2 | === 3 | 4 | Design a data structure that supports the following two operations: 5 | 6 | void addWord(word) 7 | bool search(word) 8 | search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter. 9 | 10 | For example: 11 | 12 | addWord("bad") 13 | addWord("dad") 14 | addWord("mad") 15 | search("pad") -> false 16 | search("bad") -> true 17 | search(".ad") -> true 18 | search("b..") -> true 19 | Note: 20 | You may assume that all words are consist of lowercase letters a-z. -------------------------------------------------------------------------------- /src/maxPointsOnALine/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 4 | 5 | Solution 6 | === 7 | 8 | http://rleetcode.blogspot.com/2014/01/max-points-on-linejava.html 9 | 10 | remember that a line can be represented by y=kx+d, if p1 and p2 are in same line, then y1=x1k+d, y2=kx2+d, so y2-y1=kx2-kx1, so k=(y2-y1)/(x2-x1), then we can apply this formular to check if two points are in same line, however how to handle the duplicate points problem? 11 | 12 | So instead to calculate the line with maximum points , we should calculate longest line(maximum points) through same point, -------------------------------------------------------------------------------- /src/trappingRainWater/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | Trapping Rain Water 4 | Given n non-negative integers representing an elevation map where the 5 | width of each bar is 1, compute how much water it is able to trap after 6 | raining. 7 | 8 | For example, 9 | Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. 10 | 11 | Solution 12 | === 13 | 14 | For each bar, the number it can hold depends on its left and right boundaries 15 | 16 | Two scans of the array build these boundaries, and sum the results up 17 | 18 | More, if using the idea of *contraction* in functional programming, this can be done with O(n) work and O(log n) time, if using multiple threads -------------------------------------------------------------------------------- /src/3SumClosest/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | Given an array S of n integers, find three integers in S such that the sum is closest to a given number, *target*. Return the sum of the three integers. You may assume that each input would have exactly one solution. 5 | 6 | For example, given array S = {-1 2 1 -4}, and target = 1. 7 | 8 | The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). 9 | 10 | Solution 11 | ==== 12 | 13 | Same as 3 sum. Complexity O(n^2) 14 | 15 | 1. sort the array 16 | 17 | 2. iterate first selection in [0, length - 2) 18 | 19 | 3. use two pointers sliding to find the smallest in the enumerations of *two sums* 20 | -------------------------------------------------------------------------------- /src/validBST/Solution.java: -------------------------------------------------------------------------------- 1 | class TreeNode { 2 | int val; 3 | TreeNode left; 4 | TreeNode right; 5 | TreeNode(int x) { val = x; } 6 | } 7 | public class Solution { 8 | public boolean isValidBST(TreeNode root) { 9 | return isValid(root, (long) Integer.MIN_VALUE, (long) Integer.MAX_VALUE); 10 | } 11 | public boolean isValid(TreeNode e, long min, long max) { 12 | if (e == null) return true; 13 | if (e.val > max || e.val < min) { 14 | return false; 15 | } else { 16 | return isValid(e.left, min, (long) e.val - 1) && isValid(e.right, (long) e.val + 1, max); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /src/404_sum_of_left_leaves/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | 4 | 404. Sum of Left Leaves 5 | Given the root of a binary tree, return the sum of all left leaves. 6 | 7 | A leaf is a node with no children. A left leaf is a leaf that is the left child of another node. 8 | 9 | Example 1: 10 | Input: root = [3,9,20,null,null,15,7] 11 | Output: 24 12 | Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively. 13 | 14 | Example 2: 15 | Input: root = [1] 16 | Output: 0 17 | 18 | 19 | Constraints: 20 | 21 | The number of nodes in the tree is in the range [1, 1000]. 22 | -1000 <= Node.val <= 1000 23 | 24 | Solution 25 | === 26 | dfs 27 | -------------------------------------------------------------------------------- /src/best_meeting_point/README.md: -------------------------------------------------------------------------------- 1 | Problem 2 | === 3 | A group of two or more people wants to meet and minimize the total travel distance. You are given a 2D grid of values 0 or 1, where each 1 marks the home of someone in the group. The distance is calculated using Manhattan Distance, where distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|. 4 | 5 | For example, given three people living at (0,0), (0,4), and (2,2): 6 | 1 - 0 - 0 - 0 - 1 7 | | | | | | 8 | 0 - 0 - 0 - 0 - 0 9 | | | | | | 10 | 0 - 0 - 1 - 0 - 0 11 | 12 | The point (0,2) is an ideal meeting point, as the total travel distance of 2+2+2=6 is minimal. So return 6. 13 | --------------------------------------------------------------------------------