From f81c33917ac34a1c190bfb3a8dd52f401f3fbabe Mon Sep 17 00:00:00 2001 From: yaha <1143990204@qq.com> Date: Tue, 26 Mar 2024 19:18:25 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- include/string_array.h | 19 +++++ src/jianzhi_offer/string.cpp | 36 ++++---- src/string_array/majority_element.cpp | 31 +++++++ src/string_array/max_profit.cpp | 20 +++++ src/string_array/merge_two_sorted_arrays.cpp | 19 +++++ src/string_array/remove_duplicates.cpp | 42 +++++++++ src/string_array/remove_element.cpp | 19 +++++ src/string_array/rotate.cpp | 19 +++++ tests/arrays_test.cpp | 90 ++++++++++++++++++++ tests/jianzhi_offer/string_test.cpp | 16 ++++ 11 files changed, 294 insertions(+), 20 deletions(-) create mode 100644 include/string_array.h create mode 100644 src/string_array/majority_element.cpp create mode 100644 src/string_array/max_profit.cpp create mode 100644 src/string_array/merge_two_sorted_arrays.cpp create mode 100644 src/string_array/remove_duplicates.cpp create mode 100644 src/string_array/remove_element.cpp create mode 100644 src/string_array/rotate.cpp create mode 100644 tests/arrays_test.cpp diff --git a/.gitignore b/.gitignore index 11d1f09..ce3036f 100644 --- a/.gitignore +++ b/.gitignore @@ -34,4 +34,5 @@ .idea/ cmake-build-debug/ -build/ \ No newline at end of file +build/ +.DS_Store \ No newline at end of file diff --git a/include/string_array.h b/include/string_array.h new file mode 100644 index 0000000..631bb4c --- /dev/null +++ b/include/string_array.h @@ -0,0 +1,19 @@ +#pragma once +#include +#include + +namespace yaha { +class Array { +public: + void merge(std::vector& nums1, int m, std::vector& nums2, int n); + int remove_element(std::vector& nums, int val); + int remove_duplicates(std::vector& nums); + int remove_duplicates2(std::vector& nums); + + int majority_element(std::vector& nums); + + void rotate(std::vector& nums, int k); + + int max_profit(std::vector& prices); +}; +} \ No newline at end of file diff --git a/src/jianzhi_offer/string.cpp b/src/jianzhi_offer/string.cpp index 68011e2..dc2613d 100644 --- a/src/jianzhi_offer/string.cpp +++ b/src/jianzhi_offer/string.cpp @@ -1,22 +1,21 @@ #include "jianzhi_offer.h" namespace yaha { - std::string String::reverse_left_words(std::string s, int n) { -// 方法1: 遍历 -// std::string cur_str; -// std::string new_str; -// for (int i = 0; i < s.length(); ++i) { -// if (i < n) { -// cur_str += s[i]; -// } else { -// new_str += s[i]; -// } -// } -// -// return new_str + cur_str; + // 方法1: 遍历 + // std::string cur_str; + // std::string new_str; + // for (int i = 0; i < s.length(); ++i) { + // if (i < n) { + // cur_str += s[i]; + // } else { + // new_str += s[i]; + // } + // } + // + // return new_str + cur_str; -// 方法2: 内置函数 + // 方法2: 内置函数 return s.substr(n, s.size()) + s.substr(0, n); } @@ -47,7 +46,7 @@ bool String::is_number(std::string s) { } int String::str_to_int(std::string str) { - int res = 0, bndry = INT_MAX/10; + int res = 0, bndry = INT_MAX / 10; int i = 0, sign = 1, length = str.size(); if (length == 0) return 0; while (str[i] == ' ') @@ -58,9 +57,8 @@ int String::str_to_int(std::string str) { if (str[j] < '0' || str[j] > '9') break; if (res > bndry || res == bndry && str[j] > '7') return sign == 1 ? INT_MAX : INT_MIN; - res = res*10 + (str[j] - '0'); + res = res * 10 + (str[j] - '0'); } - return sign*res; + return sign * res; +} } - -} \ No newline at end of file diff --git a/src/string_array/majority_element.cpp b/src/string_array/majority_element.cpp new file mode 100644 index 0000000..e277c73 --- /dev/null +++ b/src/string_array/majority_element.cpp @@ -0,0 +1,31 @@ +// +// Created by yangzuhao on 2024/3/25. +// + +#include "string_array.h" + +#include + +namespace yaha { +int Array::majority_element(std::vector &nums) { + std::map m; + for (int num : nums) { + if (m.find(num)->second) { + m[num]++; + } else { + m[num] = 1; + } + } + + int res = 0; + int idx = 0; + for (auto it : m) { + if (it.second > res) { + idx = it.first; + res = it.second; + } + } + + return idx; +} +} diff --git a/src/string_array/max_profit.cpp b/src/string_array/max_profit.cpp new file mode 100644 index 0000000..9979e5c --- /dev/null +++ b/src/string_array/max_profit.cpp @@ -0,0 +1,20 @@ +// +// Created by yangzuhao on 2024/3/26. +// +#include "string_array.h" + +namespace yaha { +int Array::max_profit(std::vector &prices) { + int max = 0; + for (int i = 0; i < prices.size(); i++) { + for (int j = i + 1; j < prices.size(); j++) { + int a = prices[j] - prices[i]; + if (max < a) { + max = a; + } + } + } + + return max; +} +} diff --git a/src/string_array/merge_two_sorted_arrays.cpp b/src/string_array/merge_two_sorted_arrays.cpp new file mode 100644 index 0000000..3d6a6a3 --- /dev/null +++ b/src/string_array/merge_two_sorted_arrays.cpp @@ -0,0 +1,19 @@ +#include "string_array.h" + +namespace yaha { +void Array::merge(std::vector &nums1, int m, std::vector &nums2, int n) { + int p1 = 0, p2 = 0; + nums1.resize(m); + while (p1 < m && p2 < n) { + if (nums1[p1] > nums2[p2]) { + nums1.insert(nums1.begin() + p1, nums2[p2++]); + } else { + p1++; + } + } + + while (p2 < n) { + nums1.push_back(nums2[p2++]); + } +} +} diff --git a/src/string_array/remove_duplicates.cpp b/src/string_array/remove_duplicates.cpp new file mode 100644 index 0000000..9f367eb --- /dev/null +++ b/src/string_array/remove_duplicates.cpp @@ -0,0 +1,42 @@ +// +// Created by yangzuhao on 2024/3/25. +// +#include "string_array.h" + +namespace yaha { +int Array::remove_duplicates(std::vector &nums) { + if (nums.size() < 1) { + return 1; + } + + // 用于记录当前位置 + int idx = 0; + for (int i = 1; i < nums.size(); i++) { + // 如果当前位置值和下一个一样,则直接跳过 + if (nums[idx] != nums[i]) { + idx++; + nums[idx] = nums[i]; + } + } + + return idx++; +} + +int Array::remove_duplicates2(std::vector &nums) { + int n = nums.size(); + if (nums.size() < 2) { + return n; + } + + // 用于记录当前位置 + int slow = 2; + for (int fast = 2; fast < n; fast++) { + // 如果当前位置值和下一个一样,则直接跳过 + if (nums[slow - 2] != nums[fast]) { + nums[slow++] = nums[fast]; + } + } + + return slow; +} +} diff --git a/src/string_array/remove_element.cpp b/src/string_array/remove_element.cpp new file mode 100644 index 0000000..d119d0a --- /dev/null +++ b/src/string_array/remove_element.cpp @@ -0,0 +1,19 @@ +#include "string_array.h" + +namespace yaha { +int Array::remove_element(std::vector &nums, int val) { + if (nums.size() < 1) { + return 0; + } + + int idx = 0; + for (int it : nums) { + if (it != val) { + nums[idx] = it; + idx++; + } + } + + return idx; +} +} diff --git a/src/string_array/rotate.cpp b/src/string_array/rotate.cpp new file mode 100644 index 0000000..1428bfc --- /dev/null +++ b/src/string_array/rotate.cpp @@ -0,0 +1,19 @@ +// +// Created by yangzuhao on 2024/3/26. +// +#include + +#include "string_array.h" + +namespace yaha { +void Array::rotate(std::vector &nums, int k) { + int n = nums.size(); + k = k % n; + std::vector a(n); + for (int i = 0; i < n; ++i) { + a[(i + k) % n] = nums[i]; + } + + nums = a; +} +} diff --git a/tests/arrays_test.cpp b/tests/arrays_test.cpp new file mode 100644 index 0000000..d566adc --- /dev/null +++ b/tests/arrays_test.cpp @@ -0,0 +1,90 @@ +// +// Created by yangzuhao on 2024/3/21. +// +#include +#include "string_array.h" + +TEST(ArrayTest, MergeTwoSortedArraysTest) { + std::vector nums1{4, 0, 0, 0, 0, 0}; + int m = 1; + std::vector nums2{1, 2, 3, 5, 6}; + int n = 3; + yaha::Array a; + a.merge(nums1, m, nums2, n); + + for (int it : nums1) { + std::cout << "val: " << it << std::endl; + } +} + +TEST(ArrayTest, RemoveElementTest) { + std::vector nums{3, 2, 2, 3}; + int val = 3; + + yaha::Array a; + int res = a.remove_element(nums, val); + + std::cout << "val: " << val << " , res: " << res << std::endl; + for (int num : nums) { + std::cout << "num: " << num << std::endl; + } +} + +TEST(ArrayTest, RemoveDuplicatesTest) { + std::vector nums{0, 0, 1, 1, 1, 1, 2, 3, 3}; + + yaha::Array a; + int res = a.remove_duplicates(nums); + + std::cout << "res: " << res << std::endl; + for (int num : nums) { + std::cout << "num: " << num << std::endl; + } +} + +TEST(ArrayTest, RemoveDuplicates2Test) { + std::vector nums{0, 0, 1, 1, 1, 1, 2, 3, 3}; + + yaha::Array a; + int res = a.remove_duplicates2(nums); + + std::cout << "res: " << res << std::endl; + for (int num : nums) { + std::cout << "num: " << num << std::endl; + } +} + +TEST(ArrayTest, MajorityElementTest) { + std::vector nums{2, 2, 1, 1, 1, 2, 2}; + + yaha::Array a; + int res = a.majority_element(nums); + + std::cout << "res: " << res << std::endl; + for (int num : nums) { + std::cout << "num: " << num << std::endl; + } +} + +TEST(ArrayTest, RotateTest) { + std::vector nums{1, 2, 3, 4, 5, 6, 7}; + // std::vector nums{1,2}; + int k = 3; + + yaha::Array a; + a.rotate(nums, k); + + for (int num : nums) { + std::cout << "num: " << num << std::endl; + } +} + +TEST(ArrayTest, MaxProfitTest) { + std::vector nums{7, 1, 5, 3, 6, 4}; + // std::vector nums{1,2}; + // int k = 5; + + yaha::Array a; + int res = a.max_profit(nums); + std::cout << "res: " << res << std::endl; +} diff --git a/tests/jianzhi_offer/string_test.cpp b/tests/jianzhi_offer/string_test.cpp index dd12759..c01d594 100644 --- a/tests/jianzhi_offer/string_test.cpp +++ b/tests/jianzhi_offer/string_test.cpp @@ -31,4 +31,20 @@ TEST(StringToIntTest, BasicAssertions) { ASSERT_EQ(-42, s.str_to_int(" -42")); ASSERT_EQ(4193, s.str_to_int("4193 with words")); ASSERT_EQ(0, s.str_to_int("words and 987")); +} + +TEST(UriTest, BasicAssertions) { + std::string url = "http://pdxo.poms.bae.baidu.com/rest/2.0/poms/object?method=download&consume_idc=pdxo&service=pcsImageTranr"; + + // Find the position of the first '/' character + size_t pos = url.find("/rest"); + if (pos != std::string::npos) { + // Extract the substring starting from the found position + std::string extracted = url.substr(pos); + + std::cout << "Extracted URL: " << extracted << std::endl; + } else { + std::cout << "Substring not found." << std::endl; + } + } \ No newline at end of file