diff --git a/Cargo.lock b/Cargo.lock index 3961f0f2..dc3e4d79 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -346,10 +346,18 @@ version = "0.1.0" name = "lc-0027-remove-element" version = "0.1.0" +[[package]] +name = "lc-0034-find-first-and-last-position-of-element-in-sorted-array" +version = "0.1.0" + [[package]] name = "lc-0035-search-insert-position" version = "0.1.0" +[[package]] +name = "lc-0036-valid-sudoku" +version = "0.1.0" + [[package]] name = "lc-0053-maximum-subarray" version = "0.1.0" @@ -358,10 +366,34 @@ version = "0.1.0" name = "lc-0083-remove-duplicates-from-sorted-list" version = "0.1.0" +[[package]] +name = "lc-0128-longest-consecutive-sequence" +version = "0.1.0" + +[[package]] +name = "lc-0136-single-number" +version = "0.1.0" + +[[package]] +name = "lc-0137-single-number-ii" +version = "0.1.0" + [[package]] name = "lc-0191-number-of-1-bits" version = "0.1.0" +[[package]] +name = "lc-0217-contains-duplicate" +version = "0.1.0" + +[[package]] +name = "lc-0219-contains-duplicate-ii" +version = "0.1.0" + +[[package]] +name = "lc-0220-contains-duplicate-iii" +version = "0.1.0" + [[package]] name = "lc-1114-print-in-order" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 8e19a918..1b99083a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,6 @@ members = [ # Custome "leetcode/*.*", "robert", - "weiss", ] exclude = [ diff --git a/weiss/.gitignore b/weiss/.gitignore deleted file mode 100644 index ca1d8071..00000000 --- a/weiss/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -/cmake-build-debug -/cmake-build-release -/build diff --git a/weiss/CMakeLists.txt b/weiss/CMakeLists.txt deleted file mode 100644 index 5847b64f..00000000 --- a/weiss/CMakeLists.txt +++ /dev/null @@ -1,5 +0,0 @@ - -cmake_minimum_required(VERSION 3.10) -project(weiss C) - -add_subdirectory(c) diff --git a/weiss/Cargo.toml b/weiss/Cargo.toml deleted file mode 100644 index b5d8ec94..00000000 --- a/weiss/Cargo.toml +++ /dev/null @@ -1,23 +0,0 @@ -[package] -name = "weiss" -version = "0.1.0" -edition = "2021" -publish = false - -[[bin]] -name = "weiss_gcd" -path = "src/gcd.rs" - -[[bin]] -name = "weiss_binary_search" -path = "src/binary_search.rs" - -[[bin]] -name = "weiss_pow" -path = "src/pow.rs" - -[[bin]] -name = "weiss_subsequence_of_sum" -path = "src/subsequence_of_sum.rs" - -[dependencies] diff --git a/weiss/README.md b/weiss/README.md deleted file mode 100644 index f4bf8fec..00000000 --- a/weiss/README.md +++ /dev/null @@ -1,3 +0,0 @@ - -# About -Data structures and Algorithms in C, by Mark Allen Weiss. diff --git a/weiss/c/CMakeLists.txt b/weiss/c/CMakeLists.txt deleted file mode 100644 index d07e2880..00000000 --- a/weiss/c/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ - -add_library(linked_list STATIC - linked_list.c - linked_list.h -) - -add_library(polynomial_array STATIC - polynomial_array.c - polynomial_array.h -) diff --git a/weiss/c/linked_list.c b/weiss/c/linked_list.c deleted file mode 100644 index 2840b18d..00000000 --- a/weiss/c/linked_list.c +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include "linked_list.h" - -#include -#include -#include - -bool list_is_empty(node_t* list) { - assert(list != NULL); - return list->next == NULL; -} - -bool list_is_last(node_t* list, node_t* current) { - assert(list != NULL); - assert(current != NULL); - return current->next == NULL; -} - -node_t* list_find(node_t* list, element_type value) { - assert(list != NULL); - - while ((list != NULL) && (list->value != value)) { - list = list->next; - } - return list; -} - -node_t* list_find_previous(node_t* list, element_type value) { - assert(list != NULL); - - while (list->next != NULL) { - if (list->next->value == value) { - return list; - } - list = list->next; - } - return NULL; -} - -bool list_delete(node_t** list, element_type value) { - assert(list != NULL); - - // Delete the first node. - if ((*list)->value == value) { - node_t* tmp = *list; - *list = tmp->next; - free(tmp); - return true; - } - - node_t* prev = list_find_previous(*list, value); - if (prev == NULL) { - // Not found. - return false; - } - - node_t* tmp = prev->next; - prev->next = tmp->next; - free(tmp); - return true; -} - -node_t* list_insert(node_t** list, node_t* position, element_type value) { - assert(list != NULL); - - node_t* new_node = (node_t*) malloc(sizeof(node_t)); - assert(new_node != NULL); - new_node->value = value; - - if (position == NULL) { - new_node->next = *list; - *list = new_node; - } else { - new_node->next = position->next; - position->next = new_node; - } - - return new_node; -} - -void list_clear(node_t** list) { - assert(list != NULL); - while (*list != NULL) { - node_t* tmp = *list; - *list = tmp->next; - free(tmp); - } -} - -void list_map(node_t* list, void apply(node_t* node, void* user_data), void* user_data) { - assert(list != NULL); - while (list != NULL) { - apply(list, user_data); - list = list->next; - } -} - -size_t list_length(node_t* list) { - assert(list != NULL); - - size_t count = 0; - while (list != NULL) { - count += 1; - list = list->next; - } - return count; -} - -static void print_node(node_t* node, void*) { - printf("%d, ", node->value); -} - -void list_debug_print(node_t* list) { - list_map(list, print_node, NULL); - printf("\n"); -} diff --git a/weiss/c/linked_list.h b/weiss/c/linked_list.h deleted file mode 100644 index f64018b5..00000000 --- a/weiss/c/linked_list.h +++ /dev/null @@ -1,120 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#ifndef WEISS_C03_LINKED_LIST_H_ -#define WEISS_C03_LINKED_LIST_H_ - -#include -#include -#include - -// NOTE(Shaohua): node value type in list is defined as integer for simplicity. -typedef int element_type; - -struct node_s { - element_type value; - struct node_s* next; -}; - -typedef struct node_s node_t; - -/** - * Check whether list is empty. - * - * @param list - * @return - */ -extern bool list_is_empty(node_t* list); - -/** - * To find if |current| node is the last one in the list. - * - * @param list - * @param current - * @return - */ -extern bool list_is_last(node_t* list, node_t* current); - -/** - * Find node with |value| in |list|. - * - * Returns a null pointer if not found. - * - * @param list - * @param value - * @return - */ -extern node_t* list_find(node_t* list, element_type value); - -/** - * Find node whose next node is with |value| in |list|. - * - * Returns a null pointer if not found. - * - * Note that if the head node is with this element value, it also returns NULL, - * as head node has no previous node. - * - * @param list - * @param value - * @return - */ -extern node_t* list_find_previous(node_t* list, element_type value); - -/** - * Delete the first node with |value| from list. - * - * Returns true if that node is found and deleted. - * - * @param list - * @param value - * @return - */ -extern bool list_delete(node_t** list, element_type value); - -/** - * Insert a new node with |value| after specific node at |position| into |list|. - * - * And returns position of that new node. - * - * If |position| is null, insert that new node to head of list. - * - * @param list - * @param position - * @param value - * @return - */ -extern node_t* list_insert(node_t** list, node_t* position, element_type value); - -/** - * Delete all nodes in a list and reset it to null. - * - * @param list - */ -extern void list_clear(node_t** list); - -/** - * Calls |apply| function for each node in list. - * - * @param list - * @param apply - */ -extern void list_map(node_t* list, void apply(node_t* node, void* user_data), - void* user_data); - -/** - * Get number of nodes in list. - * - * @param list - * @return - */ -extern size_t list_length(node_t* list); - -/** - * Print all elements in a list. - * - * @param list - */ -extern void list_debug_print(node_t* list); - -#endif // WEISS_C03_LINKED_LIST_H_ diff --git a/weiss/c/polynomial_array.c b/weiss/c/polynomial_array.c deleted file mode 100644 index 5736e88a..00000000 --- a/weiss/c/polynomial_array.c +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#include "polynomial_array.h" - -#include -#include - -#ifndef max -#define max(x, y) ((x) > (y)) ? (x) : (y) -#endif - -void polynomial_zero(polynomial_t* poly) { - assert(poly != NULL); - - for (size_t i = 0; i < MAX_DEGREE; ++i) { - poly->coeff_array[i] = 0; - } - poly->high_power = 0; -} - -void polynomial_add(polynomial_t* poly1, polynomial_t* poly2, - polynomial_t* poly_sum) { - assert(poly1 != NULL); - assert(poly2 != NULL); - assert(poly_sum != NULL); - - polynomial_zero(poly_sum); - poly_sum->high_power = max(poly1->high_power, poly2->high_power); - for (uint32_t i = poly_sum->high_power; i >= 0; --i) { - poly_sum->coeff_array[i] = poly1->coeff_array[i] + poly2->coeff_array[i]; - } -} - -void polynomial_mul(polynomial_t* poly1, polynomial_t* poly2, - polynomial_t* poly_prod) { - assert(poly1); - assert(poly2); - assert(poly_prod); - - polynomial_zero(poly_prod); - poly_prod->high_power = poly1->high_power + poly2->high_power; - if (poly_prod->high_power > MAX_DEGREE) { - fprintf(stderr, "Exceeded array size\n"); - return; - } - - for (uint32_t i = 0; i < poly1->high_power; ++i) { - for (uint32_t j = 0; j < poly2->high_power; ++j) { - poly_prod->coeff_array[i + j] = poly1->coeff_array[i] + poly2->coeff_array[j]; - } - } -} diff --git a/weiss/c/polynomial_array.h b/weiss/c/polynomial_array.h deleted file mode 100644 index 37297e62..00000000 --- a/weiss/c/polynomial_array.h +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -#ifndef WEISS_C03_POLYNOMIAL_ARRAY_H_ -#define WEISS_C03_POLYNOMIAL_ARRAY_H_ - -#include -#include - -#define MAX_DEGREE 32 - -struct polynomial_s { - int32_t coeff_array[MAX_DEGREE + 1]; - uint32_t high_power; -}; - -typedef struct polynomial_s polynomial_t; - -/** - * Reset all fields in polynomial to zero. - * - * @param poly - */ -extern void polynomial_zero(polynomial_t* poly); - -/** - * Add two polynomials. - * - * @param poly1 - * @param poly2 - * @param poly_sum - */ -extern void polynomial_add(polynomial_t* poly1, polynomial_t* poly2, - polynomial_t* poly_sum); - -extern void polynomial_mul(polynomial_t* poly1, polynomial_t* poly2, - polynomial_t* poly_prod); - -#endif // WEISS_C03_POLYNOMIAL_ARRAY_H_ diff --git a/weiss/src/binary_search.rs b/weiss/src/binary_search.rs deleted file mode 100644 index 15b3260c..00000000 --- a/weiss/src/binary_search.rs +++ /dev/null @@ -1,23 +0,0 @@ -#[allow(clippy::comparison_chain)] -fn binary_search(nums: &[i32], target: i32) -> Option { - let mut low = 0; - let mut high = nums.len() - 1; - while low <= high { - // FIXME(Shaohua): overflow - let mid = (low + high) / 2; - if nums[mid] < target { - low = mid + 1; - } else if nums[mid] > target { - high = mid - 1; - } else { - return Some(mid); - } - } - None -} - -fn main() { - const NUMS: &[i32] = &[-7, -3, -2, -2, -1, 2, 4, 5, 6]; - let pos = binary_search(NUMS, 2); - println!("pos: {pos:?}"); -} diff --git a/weiss/src/gcd.rs b/weiss/src/gcd.rs deleted file mode 100644 index ce48697c..00000000 --- a/weiss/src/gcd.rs +++ /dev/null @@ -1,22 +0,0 @@ -// Euclid's algorithm -fn gcd(mut m: u64, mut n: u64) -> u64 { - if m == 0 || n == 0 { - return 0; - } - if m < n { - return gcd(n, m); - } - - let mut rem; - while n > 0 { - rem = m % n; - m = n; - n = rem; - } - m -} - -fn main() { - let r = gcd(1989, 1590); - println!("r: {r}"); -} diff --git a/weiss/src/pow.rs b/weiss/src/pow.rs deleted file mode 100644 index b0a8d768..00000000 --- a/weiss/src/pow.rs +++ /dev/null @@ -1,24 +0,0 @@ -#[must_use] -#[inline] -const fn is_even(n: usize) -> bool { - n % 2 == 0 -} - -fn pow(x: i32, n: usize) -> i32 { - if n == 0 { - return 1; - } - if n == 1 { - return x; - } - if is_even(n) { - pow(x * x, n / 2) - } else { - pow(x * x, n / 2) * x - } -} - -fn main() { - let r = pow(3, 4); - println!("r: {r}"); -} diff --git a/weiss/src/subsequence_of_sum.rs b/weiss/src/subsequence_of_sum.rs deleted file mode 100644 index 55febd8d..00000000 --- a/weiss/src/subsequence_of_sum.rs +++ /dev/null @@ -1,154 +0,0 @@ -// Copyright (c) 2023 Xu Shaohua . All rights reserved. -// Use of this source is governed by General Public License that can be -// found in the LICENSE file. - -//! Maximum of subsequence sum problem: -//! -//! Given (possibly negative) integers a1 , a2, . . . , an , find the maximum value of -//! maximum subsequence sum is 0 if all the integers are negative.) -//! -//! Example: -//! -//! For input -2, 11, -4, 13, -5, -2, the answer is 20 (a 2 through a4). - -#![allow(clippy::needless_range_loop)] - -fn method1(nums: &[i32]) -> (i32, usize, usize) { - let mut max_sum = 0; - let mut best_i = 0; - let mut best_j = 0; - let len = nums.len(); - for i in 0..len { - for j in i..len { - let this_sum = nums[i..=j].iter().sum(); - max_sum = max_sum.max(this_sum); - if this_sum > max_sum { - max_sum = this_sum; - best_i = i; - best_j = j; - } - } - } - - (max_sum, best_i, best_j) -} - -fn method2(nums: &[i32]) -> (i32, usize, usize) { - let mut max_sum = 0; - let mut best_i = 0; - let mut best_j = 0; - let len = nums.len(); - for i in 0..len { - let mut this_sum = 0; - for j in i..len { - this_sum += nums[j]; - if this_sum > max_sum { - max_sum = this_sum; - best_i = i; - best_j = j; - } - } - } - - (max_sum, best_i, best_j) -} - -// devide and conquer -fn method3(nums: &[i32]) -> (i32, usize, usize) { - method3_helper(nums, 0, nums.len() - 1) -} - -fn method3_helper(nums: &[i32], left: usize, right: usize) -> (i32, usize, usize) { - if left == right { - if nums[left] > 0 { - return (nums[left], left, left); - } else { - return (0, left, left); - } - } - - // FIXME(Shaohua): Overflow - let center_index = (left + right) / 2; - let (max_left_sum, _, _) = method3_helper(nums, left, center_index); - let (max_right_sum, _, _) = method3_helper(nums, center_index + 1, right); - - let mut max_left_border_sum = 0; - let mut left_border_sum = 0; - let mut left_border = 0; - for i in (left..=center_index).rev() { - left_border_sum += nums[i]; - if left_border_sum > max_left_border_sum { - max_left_border_sum = left_border_sum; - left_border = i; - } - } - - let mut max_right_border_sum = 0; - let mut right_border_sum = 0; - let mut right_border = 0; - for i in center_index + 1..=right { - right_border_sum += nums[i]; - if right_border_sum > max_right_border_sum { - max_right_border_sum = right_border_sum; - right_border = i; - } - } - let border_sum = max_left_border_sum + max_right_border_sum; - let new_sum: i32 = max_left_sum.max(max_right_sum.max(border_sum)); - - if new_sum == max_left_sum { - (new_sum, left, center_index) - } else if new_sum == max_right_sum { - (new_sum, center_index + 1, right) - } else { - (new_sum, left_border, right_border) - } -} - -// Online algorithm -fn method4(nums: &[i32]) -> (i32, usize, usize) { - let mut max_sum = 0; - let mut best_i = 0; - let mut best_j = 0; - let len = nums.len(); - - let mut this_sum = 0; - let mut i = 0; - for j in 0..len { - this_sum += nums[j]; - if this_sum > max_sum { - max_sum = this_sum; - best_i = i; - best_j = j; - } else if this_sum < 0 { - i = j + 1; - this_sum = 0; - } - } - (max_sum, best_i, best_j) -} - -fn main() { - const NUMS: &[i32] = &[4, -3, 5, -2, -1, 2, 6, -2]; - let (r, best_i, best_j) = method1(NUMS); - println!("best sum: {r}, ({best_i}, {best_j})"); - let (r, best_i, best_j) = method2(NUMS); - println!("best sum: {r}, ({best_i}, {best_j})"); - let (r, best_i, best_j) = method3(NUMS); - println!("best sum: {r}, ({best_i}, {best_j})"); - let (r, best_i, best_j) = method4(NUMS); - println!("best sum: {r}, ({best_i}, {best_j})"); -} - -#[cfg(test)] -mod tests { - use super::method1; - - const NUMS: &[i32] = &[4, -3, 5, -2, -1, 2, 6, -2]; - - #[test] - fn test_method1() { - let (r, _, _) = method1(NUMS); - assert_eq!(r, 11); - } -}