Skip to content

Commit b6a3de2

Browse files
committed
Start solving
1 parent a65b4af commit b6a3de2

File tree

242 files changed

+390
-251
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

242 files changed

+390
-251
lines changed

src/model_solution/mod.rs

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
mod s0001_two_sum;
2+
mod s0002_add_two_numbers;
3+
mod s0003_longest_substring_without_repeating_characters;
4+
mod s0004_median_of_two_sorted_arrays;
5+
mod s0005_longest_palindromic_substring;
6+
mod s0006_zigzag_conversion;
7+
mod s0007_reverse_integer;
8+
mod s0008_string_to_integer_atoi;
9+
mod s0009_palindrome_number;
10+
mod s0010_regular_expression_matching;
11+
mod s0011_container_with_most_water;
12+
mod s0012_integer_to_roman;
13+
mod s0013_roman_to_integer;
14+
mod s0014_longest_common_prefix;
15+
mod s0015_3sum;
16+
mod s0016_3sum_closest;
17+
mod s0017_letter_combinations_of_a_phone_number;
18+
mod s0018_4sum;
19+
mod s0019_remove_nth_node_from_end_of_list;
20+
mod s0020_valid_parentheses;
21+
mod s0021_merge_two_sorted_lists;
22+
mod s0022_generate_parentheses;
23+
mod s0023_merge_k_sorted_lists;
24+
mod s0024_swap_nodes_in_pairs;
25+
mod s0025_reverse_nodes_in_k_group;
26+
mod s0026_remove_duplicates_from_sorted_array;
27+
mod s0027_remove_element;
28+
mod s0028_implement_strstr;
29+
mod s0029_divide_two_integers;
30+
mod s0030_substring_with_concatenation_of_all_words;
31+
mod s0031_next_permutation;
32+
mod s0032_longest_valid_parentheses;
33+
mod s0033_search_in_rotated_sorted_array;
34+
mod s0034_find_first_and_last_position_of_element_in_sorted_array;
35+
mod s0035_search_insert_position;
36+
mod s0036_valid_sudoku;
37+
mod s0037_sudoku_solver;
38+
mod s0038_count_and_say;
39+
mod s0039_combination_sum;
40+
mod s0040_combination_sum_ii;
41+
mod s0041_first_missing_positive;
42+
mod s0042_trapping_rain_water;
43+
mod s0043_multiply_strings;
44+
mod s0044_wildcard_matching;
45+
mod s0045_jump_game_ii;
46+
mod s0046_permutations;
47+
mod s0047_permutations_ii;
48+
mod s0048_rotate_image;
49+
mod s0049_group_anagrams;
50+
mod s0050_powx_n;
51+
mod s0051_n_queens;
52+
mod s0052_n_queens_ii;
53+
mod s0053_maximum_subarray;
54+
mod s0054_spiral_matrix;
55+
mod s0055_jump_game;
56+
mod s0056_merge_intervals;
57+
mod s0057_insert_interval;
58+
mod s0058_length_of_last_word;
59+
mod s0059_spiral_matrix_ii;
60+
mod s0060_permutation_sequence;
61+
mod s0061_rotate_list;
62+
mod s0062_unique_paths;
63+
mod s0063_unique_paths_ii;
64+
mod s0064_minimum_path_sum;
65+
mod s0065_valid_number;
66+
mod s0066_plus_one;
67+
mod s0067_add_binary;
68+
mod s0068_text_justification;
69+
mod s0069_sqrtx;
70+
mod s0070_climbing_stairs;
71+
mod s0071_simplify_path;
72+
mod s0072_edit_distance;
73+
mod s0073_set_matrix_zeroes;
74+
mod s0074_search_a_2d_matrix;
75+
mod s0075_sort_colors;
76+
mod s0076_minimum_window_substring;
77+
mod s0077_combinations;
78+
mod s0078_subsets;
79+
mod s0079_word_search;
80+
mod s0080_remove_duplicates_from_sorted_array_ii;
81+
mod s0081_search_in_rotated_sorted_array_ii;
82+
mod s0082_remove_duplicates_from_sorted_list_ii;
83+
mod s0083_remove_duplicates_from_sorted_list;
84+
mod s0084_largest_rectangle_in_histogram;
85+
mod s0085_maximal_rectangle;
86+
mod s0086_partition_list;
87+
mod s0087_scramble_string;
88+
mod s0088_merge_sorted_array;
89+
mod s0089_gray_code;
90+
mod s0090_subsets_ii;
91+
mod s0091_decode_ways;
92+
mod s0092_reverse_linked_list_ii;
93+
mod s0093_restore_ip_addresses;
94+
mod s0094_binary_tree_inorder_traversal;
95+
mod s0095_unique_binary_search_trees_ii;
96+
mod s0096_unique_binary_search_trees;
97+
mod s0097_interleaving_string;
98+
mod s0098_validate_binary_search_tree;
99+
mod s0099_recover_binary_search_tree;
100+
mod s0100_same_tree;
101+
mod s0101_symmetric_tree;
102+
mod s0102_binary_tree_level_order_traversal;
103+
mod s0103_binary_tree_zigzag_level_order_traversal;
104+
mod s0104_maximum_depth_of_binary_tree;
105+
mod s0105_construct_binary_tree_from_preorder_and_inorder_traversal;
106+
mod s0106_construct_binary_tree_from_inorder_and_postorder_traversal;
107+
mod s0107_binary_tree_level_order_traversal_ii;
108+
mod s0108_convert_sorted_array_to_binary_search_tree;
109+
mod s0109_convert_sorted_list_to_binary_search_tree;
110+
mod s0110_balanced_binary_tree;
111+
mod s0111_minimum_depth_of_binary_tree;
112+
mod s0112_path_sum;
113+
mod s0113_path_sum_ii;
114+
mod s0114_flatten_binary_tree_to_linked_list;
115+
mod s0115_distinct_subsequences;
116+
mod s0118_pascals_triangle;
117+
mod s0119_pascals_triangle_ii;
118+
mod s0120_triangle;
119+
mod s0121_best_time_to_buy_and_sell_stock;
120+
mod s0122_best_time_to_buy_and_sell_stock_ii;
121+
mod s0123_best_time_to_buy_and_sell_stock_iii;
122+
mod s0124_binary_tree_maximum_path_sum;
123+
mod s0125_valid_palindrome;
124+
mod s0126_word_ladder_ii;
125+
mod s0127_word_ladder;
126+
mod s0128_longest_consecutive_sequence;
127+
mod s0129_sum_root_to_leaf_numbers;
128+
mod s0130_surrounded_regions;
129+
mod s0131_palindrome_partitioning;
130+
mod s0132_palindrome_partitioning_ii;
131+
mod s0134_gas_station;
132+
mod s0135_candy;
133+
mod s0136_single_number;
134+
mod s0137_single_number_ii;
135+
mod s0139_word_break;
136+
mod s0140_word_break_ii;
137+
mod s0143_reorder_list;
138+
mod s0144_binary_tree_preorder_traversal;
139+
mod s0145_binary_tree_postorder_traversal;
140+
mod s0146_lru_cache;
141+
mod s0147_insertion_sort_list;
142+
mod s0148_sort_list;
143+
mod s0149_max_points_on_a_line;
144+
mod s0150_evaluate_reverse_polish_notation;
145+
mod s0151_reverse_words_in_a_string;
146+
mod s0152_maximum_product_subarray;
147+
mod s0153_find_minimum_in_rotated_sorted_array;
148+
mod s0154_find_minimum_in_rotated_sorted_array_ii;
149+
mod s0155_min_stack;
150+
mod s0162_find_peak_element;
151+
mod s0164_maximum_gap;
152+
mod s0165_compare_version_numbers;
153+
mod s0166_fraction_to_recurring_decimal;
154+
mod s0167_two_sum_ii_input_array_is_sorted;
155+
mod s0168_excel_sheet_column_title;
156+
mod s0169_majority_element;
157+
mod s0171_excel_sheet_column_number;
158+
mod s0172_factorial_trailing_zeroes;
159+
mod s0173_binary_search_tree_iterator;
160+
mod s0174_dungeon_game;
161+
mod s0179_largest_number;
162+
mod s0187_repeated_dna_sequences;
163+
mod s0188_best_time_to_buy_and_sell_stock_iv;
164+
mod s0189_rotate_array;
165+
mod s0198_house_robber;
166+
mod s0199_binary_tree_right_side_view;
167+
mod s0200_number_of_islands;
168+
mod s0201_bitwise_and_of_numbers_range;
169+
mod s0202_happy_number;
170+
mod s0203_remove_linked_list_elements;
171+
mod s0204_count_primes;
172+
mod s0205_isomorphic_strings;
173+
mod s0206_reverse_linked_list;
174+
mod s0207_course_schedule;
175+
mod s0208_implement_trie_prefix_tree;
176+
mod s0209_minimum_size_subarray_sum;
177+
mod s0210_course_schedule_ii;
178+
mod s0211_add_and_search_word_data_structure_design;
179+
mod s0212_word_search_ii;
180+
mod s0213_house_robber_ii;
181+
mod s0214_shortest_palindrome;
182+
mod s0215_kth_largest_element_in_an_array;
183+
mod s0216_combination_sum_iii;
184+
mod s0217_contains_duplicate;
185+
mod s0218_the_skyline_problem;
186+
mod s0219_contains_duplicate_ii;
187+
mod s0220_contains_duplicate_iii;
188+
mod s0221_maximal_square;
189+
mod s0222_count_complete_tree_nodes;
190+
mod s0223_rectangle_area;
191+
mod s0224_basic_calculator;
192+
mod s0225_implement_stack_using_queues;
193+
mod s0226_invert_binary_tree;
194+
mod s0227_basic_calculator_ii;
195+
mod s0228_summary_ranges;
196+
mod s0229_majority_element_ii;
197+
mod s0230_kth_smallest_element_in_a_bst;
198+
mod s0231_power_of_two;
199+
mod s0232_implement_queue_using_stacks;
200+
mod s0233_number_of_digit_one;
201+
mod s0238_product_of_array_except_self;
202+
mod s0239_sliding_window_maximum;
203+
mod s0241_different_ways_to_add_parentheses;
204+
mod s0242_valid_anagram;
205+
mod s0257_binary_tree_paths;
206+
mod s0258_add_digits;
207+
mod s0260_single_number_iii;
208+
mod s0263_ugly_number;
209+
mod s0264_ugly_number_ii;
210+
mod s0268_missing_number;
211+
mod s0273_integer_to_english_words;
212+
mod s0274_h_index;
213+
mod s0275_h_index_ii;
214+
mod s0279_perfect_squares;
215+
mod s0282_expression_add_operators;
216+
mod s0283_move_zeroes;
217+
mod s0287_find_the_duplicate_number;
218+
mod s0289_game_of_life;
219+
mod s0290_word_pattern;
220+
mod s0292_nim_game;
221+
mod s0295_find_median_from_data_stream;
222+
mod s0299_bulls_and_cows;
223+
mod s0300_longest_increasing_subsequence;
224+
mod s0301_remove_invalid_parentheses;
225+
mod s0303_range_sum_query_immutable;
226+
mod s0304_range_sum_query_2d_immutable;
227+
mod s0306_additive_number;
228+
mod s0307_range_sum_query_mutable;
229+
mod s0309_best_time_to_buy_and_sell_stock_with_cooldown;
230+
mod s0310_minimum_height_trees;
231+
mod s0312_burst_balloons;
232+
mod s0313_super_ugly_number;
233+
mod s0509_fibonacci_number;
234+
mod s0704_binary_search;
235+
mod s0969_pancake_sorting;
236+
mod s1018_binary_prefix_divisible_by_5;
237+
mod s1046_last_stone_weight;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* [1] Two Sum
3+
*
4+
* Given an array of integers, return indices of the two numbers such that they
5+
* add up to a specific target.
6+
*
7+
* You may assume that each input would have exactly one solution, and you may
8+
* not use the same element twice.
9+
*
10+
* Example:
11+
*
12+
*
13+
* Given nums = [2, 7, 11, 15], target = 9,
14+
*
15+
* Because nums[0] + nums[1] = 2 + 7 = 9,
16+
* return [0, 1].
17+
*
18+
*/
19+
pub struct Solution {}
20+
21+
// problem: https://leetcode.com/problems/two-sum/
22+
// discuss: https://leetcode.com/problems/two-sum/discuss/?currentPage=1&orderBy=most_votes&query=
23+
24+
// submission codes start here
25+
26+
use std::collections::HashMap;
27+
impl Solution {
28+
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
29+
let mut map = HashMap::with_capacity(nums.len());
30+
for (index, num) in nums.iter().enumerate() {
31+
match map.get(&(target - num)) {
32+
None => {
33+
map.insert(num, index);
34+
}
35+
Some(sub_index) => return vec![*sub_index as i32, index as i32],
36+
}
37+
}
38+
vec![]
39+
}
40+
}
41+
42+
// submission codes end
43+
44+
#[cfg(test)]
45+
mod tests {
46+
use super::*;
47+
48+
#[test]
49+
fn test_1() {
50+
assert_eq!(vec![0, 1], Solution::two_sum(vec![2, 7, 11, 15], 9));
51+
assert_eq!(vec![1, 2], Solution::two_sum(vec![3, 2, 4], 6));
52+
}
53+
}

0 commit comments

Comments
 (0)