华为OD机试 2024E卷题库疯狂收录中,刷题点这里
本专栏收录于《华为OD机试真题(Python/JS/C/C++)》。
刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新。
给定一个字符串的摘要算法,请输出给定字符串的摘要值:
一行字符串。
摘要字符串。
abcccbbaa
c3a2a2b2b2
如果当前字符的连续次数大于1,表示是连续字符,输出即可
如果只有1个,则要获取后面字符串中该字符的个数
abcccbbaa
字母和紧随的数字作为一组进行排序,数字大的在前,数字相同时,按字母进行排序,字母小的在前
输出c3a2a2b2b2
。
abcccbbabaaccbca
a4b4a3c3a2b2c2b1a0b0c0
如果当前字符的连续次数大于1,表示是连续字符,输出即可
如果只有1个,则要获取后面字符串中该字符的个数
a4b4c3b2a3b1a2c2b0c0a0
字母和紧随的数字作为一组进行排序,数字大的在前,数字相同时,按字母进行排序,字母小的在前
a4b4a3c3a2b2c2b1a0b0c0
def summarize_string(s):
# 去除非字母字符并转换为小写
s = ''.join([c.lower() for c in s if c.isalpha()])
# 统计每个字符的总出现次数
ch_count = {}
for c in s:
ch_count[c] = ch_count.get(c, 0) + 1
# 统计连续字符和非连续字符
char_list = []
continuous_count = 1
for i in range(len(s)):
current = s[i]
next_char = s[i + 1] if i + 1 < len(s) else ''
ch_count[current] -= 1
if current == next_char:
continuous_count += 1
else:
num = continuous_count if continuous_count > 1 else ch_count[current]
char_list.append((current, num))
continuous_count = 1
# 排序
char_list.sort(key=lambda x: (-x[1], x[0]))
# 构建结果字符串
result = ''.join([f"{c}{n}" for c, n in char_list])
return result
function summarizeString(s) {
// 去除非字母字符并转换为小写
s = s.toLowerCase().replace(/[^a-z]/g, '');
// 统计每个字符的总出现次数
let chCount = {};
for (let c of s) {
chCount[c] = (chCount[c] || 0) + 1;
}
// 统计连续字符和非连续字符
let charList = [];
let continuousCount = 1;
for (let i = 0; i < s.length; i++) {
let current = s[i];
let nextChar = s[i + 1] || '';
chCount[current] -= 1;
if (current === nextChar) {
continuousCount += 1;
} else {
let num = continuousCount > 1 ? continuousCount : chCount[current];
charList.push({char: current, num: num});
continuousCount = 1;
}
}
// 排序
charList.sort((a, b) => b.num - a.num || a.char.localeCompare(b.char));
// 构建结果字符串
let result = '';
for (let item of charList) {
result += item.char + item.num;
}
return result;
}
#include
#include
#include
#include
typedef struct {
char ch;
int num;
} CharCount;
int compare(const void *a, const void *b) {
CharCount *x = (CharCount *)a;
CharCount *y = (CharCount *)b;
if (x->num != y->num) return y->num - x->num;
return x->ch - y->ch;
}
void summarizeString(char *s) {
// 去除非字母字符并转换为小写
char cleaned[1000];
int j = 0;
for (int i = 0; s[i]; i++) {
if (isalpha(s[i])) {
cleaned[j++] = tolower(s[i]);
}
}
cleaned[j] = '\0';
// 统计每个字符的总出现次数
int chCount[128] = {0};
for (int i = 0; cleaned[i]; i++) {
chCount[cleaned[i]]++;
}
// 统计连续字符和非连续字符
CharCount charList[1000];
int listSize = 0;
int continuousCount = 1;
for (int i = 0; cleaned[i]; i++) {
char current = cleaned[i];
char nextChar = cleaned[i + 1];
chCount[current]--;
if (current == nextChar) {
continuousCount++;
} else {
int num = continuousCount > 1 ? continuousCount : chCount[current];
charList[listSize++] = (CharCount){current, num};
continuousCount = 1;
}
}
// 排序
qsort(charList, listSize, sizeof(CharCount), compare);
// 构建结果字符串
for (int i = 0; i < listSize; i++) {
printf("%c%d", charList[i].ch, charList[i].num);
}
printf("\n");
}
#include
#include
#include
#include
using namespace std;
struct CharCount {
char ch;
int num;
};
bool compare(const CharCount &a, const CharCount &b) {
if (a.num != b.num) return a.num > b.num;
return a.ch < b.ch;
}
void summarizeString(const string &s) {
// 去除非字母字符并转换为小写
string cleaned;
for (char c : s) {
if (isalpha(c)) {
cleaned += tolower(c);
}
}
// 统计每个字符的总出现次数
int chCount[128] = {0};
for (char c : cleaned) {
chCount[c]++;
}
// 统计连续字符和非连续字符
vector<CharCount> charList;
int continuousCount = 1;
for (size_t i = 0; i < cleaned.size(); i++) {
char current = cleaned[i];
char nextChar = i + 1 < cleaned.size() ? cleaned[i + 1] : '\0';
chCount[current]--;
if (current == nextChar) {
continuousCount++;
} else {
int num = continuousCount > 1 ? continuousCount : chCount[current];
charList.push_back({current, num});
continuousCount = 1;
}
}
// 排序
sort(charList.begin(), charList.end(), compare);
// 构建结果字符串
for (const auto &item : charList) {
cout << item.ch << item.num;
}
cout << endl;
}
下一篇:华为OD机试真题 - 简易内存池(Python/JS/C/C++ 2024 E卷 200分)
本文收录于,华为OD机试真题(Python/JS/C/C++)
刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新。