算法
This commit is contained in:
parent
0c024b85fd
commit
f81c33917a
3
.gitignore
vendored
3
.gitignore
vendored
@ -34,4 +34,5 @@
|
||||
|
||||
.idea/
|
||||
cmake-build-debug/
|
||||
build/
|
||||
build/
|
||||
.DS_Store
|
||||
19
include/string_array.h
Normal file
19
include/string_array.h
Normal file
@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
namespace yaha {
|
||||
class Array {
|
||||
public:
|
||||
void merge(std::vector<int>& nums1, int m, std::vector<int>& nums2, int n);
|
||||
int remove_element(std::vector<int>& nums, int val);
|
||||
int remove_duplicates(std::vector<int>& nums);
|
||||
int remove_duplicates2(std::vector<int>& nums);
|
||||
|
||||
int majority_element(std::vector<int>& nums);
|
||||
|
||||
void rotate(std::vector<int>& nums, int k);
|
||||
|
||||
int max_profit(std::vector<int>& prices);
|
||||
};
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
31
src/string_array/majority_element.cpp
Normal file
31
src/string_array/majority_element.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
//
|
||||
// Created by yangzuhao on 2024/3/25.
|
||||
//
|
||||
|
||||
#include "string_array.h"
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace yaha {
|
||||
int Array::majority_element(std::vector<int> &nums) {
|
||||
std::map<int, int> 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;
|
||||
}
|
||||
}
|
||||
20
src/string_array/max_profit.cpp
Normal file
20
src/string_array/max_profit.cpp
Normal file
@ -0,0 +1,20 @@
|
||||
//
|
||||
// Created by yangzuhao on 2024/3/26.
|
||||
//
|
||||
#include "string_array.h"
|
||||
|
||||
namespace yaha {
|
||||
int Array::max_profit(std::vector<int> &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;
|
||||
}
|
||||
}
|
||||
19
src/string_array/merge_two_sorted_arrays.cpp
Normal file
19
src/string_array/merge_two_sorted_arrays.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include "string_array.h"
|
||||
|
||||
namespace yaha {
|
||||
void Array::merge(std::vector<int> &nums1, int m, std::vector<int> &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++]);
|
||||
}
|
||||
}
|
||||
}
|
||||
42
src/string_array/remove_duplicates.cpp
Normal file
42
src/string_array/remove_duplicates.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
//
|
||||
// Created by yangzuhao on 2024/3/25.
|
||||
//
|
||||
#include "string_array.h"
|
||||
|
||||
namespace yaha {
|
||||
int Array::remove_duplicates(std::vector<int> &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<int> &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;
|
||||
}
|
||||
}
|
||||
19
src/string_array/remove_element.cpp
Normal file
19
src/string_array/remove_element.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include "string_array.h"
|
||||
|
||||
namespace yaha {
|
||||
int Array::remove_element(std::vector<int> &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;
|
||||
}
|
||||
}
|
||||
19
src/string_array/rotate.cpp
Normal file
19
src/string_array/rotate.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by yangzuhao on 2024/3/26.
|
||||
//
|
||||
#include <queue>
|
||||
|
||||
#include "string_array.h"
|
||||
|
||||
namespace yaha {
|
||||
void Array::rotate(std::vector<int> &nums, int k) {
|
||||
int n = nums.size();
|
||||
k = k % n;
|
||||
std::vector<int> a(n);
|
||||
for (int i = 0; i < n; ++i) {
|
||||
a[(i + k) % n] = nums[i];
|
||||
}
|
||||
|
||||
nums = a;
|
||||
}
|
||||
}
|
||||
90
tests/arrays_test.cpp
Normal file
90
tests/arrays_test.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
//
|
||||
// Created by yangzuhao on 2024/3/21.
|
||||
//
|
||||
#include <gtest/gtest.h>
|
||||
#include "string_array.h"
|
||||
|
||||
TEST(ArrayTest, MergeTwoSortedArraysTest) {
|
||||
std::vector<int> nums1{4, 0, 0, 0, 0, 0};
|
||||
int m = 1;
|
||||
std::vector<int> 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<int> 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;
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user