题目描述
春节期间小明使用微信收到很多个红包,非常开心。在查看领取红包记录时发现,某个红包金额出现的次数超过了红包总数的一半。请帮小明找到该红包金额。写出具体算法思路和代码实现,要求算法尽可能高效。
给定一个红包的金额数组gifts及它的大小n,请返回所求红包的金额。
若没有金额超过总数的一半,返回0。
测试样例:
[1,2,3,2,2],5
返回:2
class Gift {
public:
int getValue(vector<int> gifts, int n) {
// write code here
int res = gifts[0];
int count = 1;
for(int i = 1; i < n; ++i)
{
if(count == 0)
{
res == gifts[i];
count = 1;
}
else
{
if(gifts[i] == res)
count++;
else
count--;
}
}
count = 0;
for(int i = 0; i < n; ++i)
{
if(res == gifts[i])
count++;
}
return (count > (n/2)) ? res : 0;
}
};
题目描述
Levenshtein 距离,又称编辑距离,指的是两个字符串之间,由一个转换成另一个所需的最少编辑操作次数。许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。编辑距离的算法是首先由俄国科学家Levenshtein提出的,故又叫Levenshtein Distance。
Ex:
字符串A:abcdefg
字符串B: abcdef
通过增加或是删掉字符”g”的方式达到目的。这两种方案都需要一次操作。把这个操作所需要的次数定义为两个字符串的距离。
要求:
给定任意两个字符串,写出一个算法计算它们的编辑距离。
请实现如下接口
/* 功能:计算两个字符串的距离
-
输入: 字符串A和字符串B
-
输出:无
-
返回:如果成功计算出字符串的距离,否则返回-1
*/
public static int calStringDistance (String charA, String charB)
{
return 0;
}
输入描述:
输入两个字符串
输出描述:
得到计算结果
示例1
输入
abcdefg
abcdef
输出
1
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string str1, str2;
while(getline(cin,str1)&&getline(cin,str2))
{
int row = str1.size();
int col = str2.size();
vector<vector<int>> dp(row + 1,vector<int>(col + 1,0));
for (int i = 0; i <= row; ++i)
dp[i][0] = i;
for (int i = 0; i <= col; ++i)
dp[0][i] = i;
for (int i = 1; i <= row; ++i)
{
for (int j = 1; j <= col; ++j)
{
dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + 1;
if (str1[i - 1] == str2[j - 1])
{
dp[i][j] = min(dp[i - 1][j - 1],dp[i][j]);
}
else
{
dp[i][j] = min(dp[i - 1][j - 1] + 1, dp[i][j]);
}
}
}
cout << dp[row][col] << endl;
}
return 0;
}