cpp-algo-cases/chapter_array_and_linkedlist/array.cpp

126 lines
2.8 KiB
C++
Raw Permalink Normal View History

2024-02-21 09:31:34 +08:00
/**
* File: array.cpp
* Created Time: 2022-11-25
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
2024-02-21 10:10:16 +08:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
int randomAccess(int *nums, int size)
{ // *nums Ϊ<><CEAA><EFBFBD><EFBFBD><EFBFBD>׵<EFBFBD>ַ
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> [0, size) <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡһ<C8A1><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-02-21 09:31:34 +08:00
int randomIndex = rand() % size;
2024-02-21 10:10:16 +08:00
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
int randomNum = nums[randomIndex]; // nums[randomIndex] <20>ȼ<EFBFBD><C8BC><EFBFBD> *(nums + randomIndex)
2024-02-21 09:31:34 +08:00
return randomNum;
}
2024-02-21 10:10:16 +08:00
/* <20><>չ<EFBFBD><D5B9><EFBFBD><EFBFBD><E9B3A4> */
int *extend(int *nums, int size, int enlarge)
{
// <20><>ʼ<EFBFBD><CABC>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>չ<EFBFBD><D5B9><EFBFBD>Ⱥ<EFBFBD><C8BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int *res = new int[size + enlarge]; // <20><><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5>ڴ<EFBFBD><DAB4>ռ<EFBFBD>
// <20><>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><D8B8>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int i = 0; i < size; i++)
{
2024-02-21 09:31:34 +08:00
res[i] = nums[i];
}
2024-02-21 10:10:16 +08:00
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
delete[] nums; // <20>ͷ<EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4>ռ<EFBFBD>
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չ<EFBFBD><D5B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-02-21 09:31:34 +08:00
return res;
}
2024-02-21 10:10:16 +08:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> index <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> num */
void insert(int *nums, int size, int num, int index)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> index <20>Լ<EFBFBD>֮<EFBFBD><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>һλ
for (int i = size - 1; i > index; i--)
{
2024-02-21 09:31:34 +08:00
nums[i] = nums[i - 1];
}
2024-02-21 10:10:16 +08:00
// <20><> num <20><><EFBFBD><EFBFBD> index <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
2024-02-21 09:31:34 +08:00
nums[index] = num;
}
2024-02-21 10:10:16 +08:00
/* ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD> index <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
void remove(int *nums, int size, int index)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> index ֮<><D6AE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>ǰ<EFBFBD>ƶ<EFBFBD>һλ
for (int i = index; i < size - 1; i++)
{
2024-02-21 09:31:34 +08:00
nums[i] = nums[i + 1];
}
}
2024-02-21 10:10:16 +08:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
void traverse(int *nums, int size)
{
2024-02-21 09:31:34 +08:00
int count = 0;
2024-02-21 10:10:16 +08:00
// ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for (int i = 0; i < size; i++)
{
2024-02-21 09:31:34 +08:00
count += nums[i];
}
}
2024-02-21 10:10:16 +08:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>в<EFBFBD><D0B2><EFBFBD>ָ<EFBFBD><D6B8>Ԫ<EFBFBD><D4AA> */
int find(int *nums, int size, int target)
{
for (int i = 0; i < size; i++)
{
2024-02-21 09:31:34 +08:00
if (nums[i] == target)
return i;
}
return -1;
}
2024-02-21 10:10:16 +08:00
/* Driver Code */ // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int main()
{
/* <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
2024-02-21 09:31:34 +08:00
int size = 5;
int *arr = new int[size];
2024-02-21 10:10:16 +08:00
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD> arr = "; // û<><C3BB>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
2024-02-21 09:31:34 +08:00
printArray(arr, size);
2024-02-21 10:10:16 +08:00
int *nums = new int[size]{1, 3, 2, 5, 4}; // []{} <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ֳ<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ʽ(<28><>ʼ<EFBFBD><CABC><EFBFBD>б<EFBFBD>)
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD> nums = ";
2024-02-21 09:31:34 +08:00
printArray(nums, size);
2024-02-21 10:10:16 +08:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
2024-02-21 09:31:34 +08:00
int randomNum = randomAccess(nums, size);
2024-02-21 10:10:16 +08:00
cout << "<EFBFBD><EFBFBD> nums <20>л<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> " << randomNum << endl;
2024-02-21 09:31:34 +08:00
2024-02-21 10:10:16 +08:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չ */
2024-02-21 09:31:34 +08:00
int enlarge = 3;
nums = extend(nums, size, enlarge);
size += enlarge;
2024-02-21 10:10:16 +08:00
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չ<EFBFBD><EFBFBD> 8 <20><><EFBFBD>õ<EFBFBD> nums = ";
2024-02-21 09:31:34 +08:00
printArray(nums, size);
2024-02-21 10:10:16 +08:00
/* <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
2024-02-21 09:31:34 +08:00
insert(nums, size, 6, 3);
2024-02-21 10:10:16 +08:00
cout << "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 3 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 6 <20><><EFBFBD>õ<EFBFBD> nums = ";
2024-02-21 09:31:34 +08:00
printArray(nums, size);
2024-02-21 10:10:16 +08:00
/* ɾ<><C9BE>Ԫ<EFBFBD><D4AA> */
2024-02-21 09:31:34 +08:00
remove(nums, size, 2);
2024-02-21 10:10:16 +08:00
cout << "ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 2 <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD>أ<EFBFBD><D8A3>õ<EFBFBD> nums = ";
2024-02-21 09:31:34 +08:00
printArray(nums, size);
2024-02-21 10:10:16 +08:00
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
2024-02-21 09:31:34 +08:00
traverse(nums, size);
2024-02-21 10:10:16 +08:00
/* <20><><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA> */
2024-02-21 09:31:34 +08:00
int index = find(nums, size, 3);
2024-02-21 10:10:16 +08:00
cout << "<EFBFBD><EFBFBD> nums <20>в<EFBFBD><D0B2><EFBFBD>Ԫ<EFBFBD><D4AA> 3 <20><><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD> = " << index << endl;
2024-02-21 09:31:34 +08:00
2024-02-21 10:10:16 +08:00
// <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
2024-02-21 09:31:34 +08:00
delete[] arr;
delete[] nums;
return 0;
}