C++算法20例
1、求两个数的最大公约数
int gcd(int a, int b) { 2 return b == 0 ? a : gcd(b, a % b); 3}
2、判断素数
bool isPrime(int n)
{
if (n <= 1) return false;
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0) return false;
}
return true;
}
3、冒泡排序
void bubbleSort(vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
}
}
}
}
4、二分查找
int binarySearch(vector<int>& arr, int target) {
int left = 0, right = arr.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}
字符串算法
5、字符串反转
string reverseString(string s) {
reverse(s.begin(), s.end());
return s;
}
6、回文数判断
bool isPalindrome(int x) {
string s = to_string(x);
int left = 0, right = s.length() - 1;
while (left < right) {
if (s[left] != s[right]) return false;
left++;
right--;
}
return true;
}
7、最长公共前缀
string longestCommonPrefix(vector<string>& strs) {
if (strs.empty()) return "";
string prefix = strs[0];
for (int i = 1; i < strs.size(); i++) {
while (strs[i].find(prefix) != 0) {
prefix = prefix.substr(0, prefix.length() - 1);
if (prefix.empty()) return "";
}
}
return prefix;
}
数组算法
8、找出数组中的重复元素
vector<int> findDuplicates(vector<int>& nums) {
vector<int> result;
for (int num : nums) {
int index = abs(num) - 1;
if (nums[index] < 0) {
result.push_back(abs(num));
}
nums[index] = -nums[index];
}
return result;
}
9、数组中的第K个最大元素
int findKthLargest(vector<int>& nums, int k) {
priority_queue<int, vector<int>, greater<int>> pq;
for (int num : nums) {
pq.push(num);
if (pq.size() > k) {
pq.pop();
}
}
return pq.top();
}
10、合并两个有序数组
vector<int> mergeSortedArrays(vector<int>& arr1, vector<int>& arr2) {
vector<int> result;
int i = 0, j = 0;
while (i < arr1.size() && j < arr2.size()) {
if (arr1[i] < arr2[j]) {
result.push_back(arr1[i++]);
} else {
result.push_back(arr2[j++]);
}
}
while (i < arr1.size()) result.push_back(arr1[i++]);
while (j < arr2.size()) result.push_back(arr2[j++]);
return result;
}
递归算法
11、斐波那契数列
int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
12、汉诺塔问题
void hanoi(int n, char from, char aux, char to) {
if (n == 1) {
cout << "Move disk 1 from " << from << " to " << to << endl;
return;
}
hanoi(n - 1, from, to, aux);
cout << "Move disk " << n << " from " << from << " to " << to << endl;
hanoi(n - 1, aux, from, to);
}
动态规划
13、最大子数组和
int maxSubArray(vector<int>& nums) {
int maxSum = nums[0], currentSum = nums[0];
for (int i = 1; i < nums.size(); i++) {
currentSum = max(nums[i], currentSum + nums[i]);
maxSum = max(maxSum, currentSum);
}
return maxSum;
}
14、爬楼梯问题
int climbStairs(int n) {
if (n <= 2) return n;
vector<int> dp(n + 1);
dp[1] = 1;
dp[2] = 2;
for (int i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
图论算法
15、深度优先搜索(DFS)
void dfs(vector<vector<int>>& graph, int node, vector<bool>& visited) {
visited[node] = true;
cout << node << " ";
for (int neighbor : graph[node]) {
if (!visited[neighbor]) {
dfs(graph, neighbor, visited);
}
}
}
16、广度优先搜索(BFS)
void bfs(vector<vector<int>>& graph, int start) {
vector<bool> visited(graph.size(), false);
queue<int> q;
q.push(start);
visited[start] = true;
while (!q.empty()) {
int node = q.front();
q.pop();
cout << node << " ";
for (int neighbor : graph[node]) {
if (!visited[neighbor]) {
q.push(neighbor);
visited[neighbor] = true;
}
}
}
}
数学算法
17、水仙花数
bool isNarcissisticNumber(int num) {
int original = num, sum = 0, digits = to_string(num).length();
while (num > 0) {
sum += pow(num % 10, digits);
num /= 10;
}
return sum == original;
}
18、素数筛选(埃氏筛法)
vector<int> sieveOfEratosthenes(int n) {
vector<bool> isPrime(n + 1, true);
vector<int> primes;
isPrime[0] = isPrime[1] = false;
for (int i = 2; i <= n; i++) {
if (isPrime[i]) {
primes. push_back(i);
for (int j = 2 * i; j <= n; j += i) {
isPrime[j] = false;
}
}
}
return primes;
}
排序算法
19、选择排序
void selectionSort(vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
swap(arr[i], arr[minIndex]);
}
}
20、插入排序
void insertionSort(vector<int>& arr) {
int n = arr.size();
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}