面試常見程式設計題目
阿新 • • 發佈:2019-01-05
相關問題總結:
- 二分查詢程式中,思想簡單,但是要注意邊界條件。
#include <iostream>
#include <vector>
using namespace std;
// 非遞迴版本
int Binary_search1(vector<int> num, int target)
{
int size = num.size();
if (size <= 0)
return -1;
int left = 0;
int right = size;
int mid;
while (left < right)
{
mid = left + ((right - left) >> 1 );
if (num[mid] > target)
right = mid;
else if (num[mid] < target)
left = mid + 1;
else
return mid;
}
return -1;
}
// 非遞迴版本
int Binary_Search(vector<int> num, int target)
{
int size = num.size();
if (size <= 0)
return -1;
int left = 0;
int right = size - 1; // 此處控制迴圈結束的條件
int mid;
while (left <= right) // 依據right的值的變化來確定的
{
mid = left + ((right - left) >> 1); // 防止溢位,同時移位更高效。注意每次迴圈都需要更新
if (num[mid] > target)
right = mid - 1; // right的賦值,隨著right的初值進行改變。
else if (num[mid] < target)
left = mid + 1;
else
return mid;
}
return -1;
}
int main()
{
vector<int> nums;
nums.push_back(1);
nums.push_back(3);
nums.push_back(5);
nums.push_back(5);
nums.push_back(5);
nums.push_back(7);
nums.push_back(10);
int target = 5;
int result = Binary_Search(nums, target);
cout << result << endl;
}
- 最長遞增子序列問題,利用動態規劃實現
#include <iostream>
#include <vector>
using namespace std;
int findLongest(vector<int> num, int n)
{
if (n <= 0)
return 0;
vector<int> dp(n, 0);
int result = INT_MIN;
for (int i = 0; i < n; i++)
{
dp[i] = 1;
for (int j = 0; j < i; j++)
{
if (num[i] >= num[j])
dp[i] = max(dp[i], dp[j] + 1);
}
if (dp[i] > result)
result = dp[i];
}
return result;
}
int main()
{
vector<int> num;
num.push_back(2);
num.push_back(1);
num.push_back(4);
num.push_back(3);
num.push_back(1);
num.push_back(5);
num.push_back(6);
int n = 7;
int result = findLongest(num, n);
cout << result << endl;
}
- 最長公共子序列問題,利用dp實現
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int findLCS(string A, int n, string B, int m)
{
vector< vector<int> > dp(n, vector<int> (m, 0));
if (A[0] == B[0])
dp[0][0] = 1;
else
dp[0][0] = 0;
// 初始化第一列
for (int i = 1; i < n; i++)
dp[i][0] = max(dp[i - 1][0], A[i] == B[0] ? 1 : 0);
// 初始化第一行
for (int j = 1; j < m; j++)
dp[0][j] = max(dp[0][j - 1], A[0] == B[j] ? 1 : 0);
for (int i = 1; i < n; i++)
{
for (int j = 1; j < m; j++)
{
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
if (A[i] == B[j])
dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
}
}
return dp[n - 1][m - 1];
}
int main()
{
string A = "1A2C3D4B56";
string B = "B1D23CA45B6A";
int n = 10;
int m = 12;
int result = findLCS(A, n, B, m);
cout << result << endl;
}
- 最長公共子串。利用動態規劃求解
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int findLongest(string A, int n, string B, int m)
{
vector< vector<int> > dp(n, vector<int>(m, 0));
int max_result = 0;
// 初始化dp
for (int i = 0; i < n; i++)
{
if (A[i] == B[0])
dp[i][0] = 1;
}
for (int j = 1; j < m; j++)
{
if (B[j] == A[0])
dp[0][j] = 1;
}
for (int i = 1; i < n; i++)
{
for (int j = 1; j < m; j++)
{
if (A[i] == B[j])
max_result = max(dp[i][j] = dp[i - 1][j - 1] + 1, max_result);
}
}
return max_result;
}
int main()
{
string A = "1AB2345CD";
string B = "12345EF";
int n = 9;
int m = 7;
int result = findLongest(A, n, B, m);
cout << result << endl;
return 0;
}
- 最小編輯距離,dp方法求解
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int findMinCost(string A, int n, string B, int m, int c0, int c1, int c2)
{
int min_result = INT_MAX;
vector< vector<int> > dp(n + 1, vector<int> (m + 1, 0));
// 初始化dp
for (int i = 1; i < m + 1; i++)
{
dp[0][i] += dp[0][i - 1] + c0;
}
for (int j = 1; j < n + 1; j++)
{
dp[j][0] += dp[j - 1][0] + c1;
}
// 中間動態規劃求解
for (int i = 1; i < n + 1; i++)
{
for (int j = 1; j < m + 1; j++)
{
if (A[i - 1] == B[j - 1])
dp[i][j] = dp[i - 1][j - 1];
else
dp[i][j] = dp[i - 1][j - 1] + c2;
dp[i][j] = min(dp[i][j], min(dp[i][j - 1] + c0, dp[i - 1][j] + c1));
}
}
return dp[n][m];
}
int main()
{
string A = "abc";
int n = 3;
string B = "adc";
int m = 3;
int c0 = 5;
int c1 = 3;
int c2 = 100;
int result = findMinCost(A, n, B, m, c0, c1, c2);
cout << result << endl;
return 0;
}
- 最長迴文子串
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int getLongestPalindrome(string A, int n)
{
int maxlen = 0;
for (int i = 0; i < n; i++)
{
for (int j = 1; j < n - i + 1; j++)
{
string temp = A.substr(i, j);
reverse(temp.begin(), temp.end());
if (temp == A.substr(i, j))
{
if (maxlen < j)
maxlen = j;
}
}
}
return maxlen;
}
int main()
{
string A = "abc1234321ab";
int n = 12;
int result = getLongestPalindrome(A, n);
cout << result << endl;
return 0;
}
- 求解模式匹配演算法
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int findAppearance1(string A, int lena, string B, int lenb)
{
int i = 0;
int j = 0;
while (j < B.length() && i < A.length())
{
if (A[i] == B[j])
{
i++;
j++;
}
else
{
i = i - j + 1;
j = 0;
}
}
if (j == B.length())
return i - j;
return -1;
}
void GextNextVal(string B, vector<int>& next)
{
int nlen = B.length();
next[0] = -1;
int k = -1;
int j = 0;
while (j < nlen - 1)
{
if (k == -1 || B[j] == B[k])
{
j++;
k++;
if (B[j] != B[k])
next[j] = k;
else
next[j] = next[k];
}
else
k = next[k];
}
}
int findAppearance(string A, int lena, string B, int lenb)
{
int i = 0;
int j = 0;
vector<int> next(lenb, 0);
GextNextVal(B, next);
while (i < lena && j < lenb)
{
if (j == -1 || A[i] == B[j])
{
i++;
j++;
}
else
{
j = next[j];
}
if (j == lenb)
return i - j;
}
return -1;
}
int main()
{
string A = "acbc";
int n = 4;
string B = "bc";
int m = 2;
int result = findAppearance(A, n, B, m);
cout << result << endl;
return 0;
}
- 字串轉整數
int myAtoi(char* str)
{
if(str == NULL)
return 0;
bool sign = true;
while (*str == ' ')
str++;
if(*str == '+' || *str == '-')
{
if(*str == '+')
sign = true;
else
sign = false;
str++;
}
long long ans = 0;
while(*str >= '0' && *str <= '9')
{
ans = ans*10 + (*str - '0');
if(ans > INT_MAX)
return sign == true ? INT_MAX : INT_MIN;
str++;
}
if(sign == false)
return (int)(-ans);
else
return (int)ans;
}