连续子数组:(难)
示例 1: 输入: nums = [0,1]
输出: 2
说明: [0, 1] 是具有相同数量 0 和 1 的
最长连续子数组。
示例 2:
输入: nums = [0,1,0]
输出: 2
说明: [0, 1] (或 [1, 0]) 是具有相同数量0和1的最长连续子数组。
需要理解的知识:
//count0(i,j) = count0(0,j) - count0(0,i-1);
例如: count0(3,5) = count0(0,5)-count0(0,2);
#include <stdio.h>
int cindex(int j){
return j + 1;
}
int findex(int j, int numsSize) {
return j + numsSize;
}
int findMaxLength(int* nums, int numsSize) {
int countDiffSize = numsSize + 1;
int countDiff[countDiffSize];
countDiff[cindex(-1)] = 0;
for (int k = 0; k < numsSize; k++){
if (nums[k] == 0){
countDiff[cindex(k)] = countDiff[cindex(k - 1)] + 1;
}else{
countDiff[cindex(k)] = countDiff[cindex(k - 1)] - 1;
}
}
int findMaxJSize = 2 * numsSize + 1;
int findMaxJ[findMaxJSize];
for (int k = -numsSize; k < numsSize; k++){
findMaxJ[findex(k, numsSize)] = -1;
}
for (int j = 0; j < numsSize; j++)
{
findMaxJ[findex(countDiff[cindex(j)], numsSize)] = j;
}
int maxLength = 0;
for (int i = 0; i < numsSize; i++)
{
int target = countDiff[cindex(i-1)];
int length = findMaxJ[findex(target, numsSize)] - i + 1;
if (length > maxLength)
{
maxLength = length;
}
}
return maxLength;
}
int main() {
int nums1[] = {0, 1};
int nums2[] = {0, 1, 0, 1};
int nums3[] = {0, 1, 1, 0, 1, 0};
int nums4[] = {1, 1, 1, 0, 0, 0};
int nums5[] = {1, 0, 1, 1, 0};
int nums6[] = {1, 1, 1};
printf("Test 1: %d\n", findMaxLength(nums1, 2)); // Expected: 2
printf("Test 2: %d\n", findMaxLength(nums2, 4)); // Expected: 4
printf("Test 3: %d\n", findMaxLength(nums3, 6)); // Expected: 6
printf("Test 4: %d\n", findMaxLength(nums4, 6)); // Expected: 6
printf("Test 5: %d\n", findMaxLength(nums5, 5)); // Expected: 4
printf("Test 6: %d\n", findMaxLength(nums6, 3)); // Expected: 0
return 0;
}
今天晚上再看一遍把。难理解。。绕。。。
统计最大字符:
输入:aaaabbbbbbbbbbbbbbccc
输出:14 b
C:
#include <stdio.h>
#include <string.h>
#define MAX_LEN 100
int main() {
char str [MAX_LEN];
int count[26] = { 0 };
int max_count = 0;
char result_char;
//输入:
fgets(str,MAX_LEN,stdin);
//统计次数:
for (int i = 0; str[i] != '\0'; i++){
if (str[i] >= 'a' && str[i] <= 'z'){
count[str[i] - 'a']++;
}
}
//输出:
for (int i = 0; i < 26; i++){
if (count[i] > max_count){
max_count = count[i];
result_char = 'a' + i;
}else if (count[i] == max_count){
if ('a' + i <result_char){
result_char = 'a' + i;
}
}
}
printf("%d %c",max_count,result_char);
}
C++:
C++还是好用一些,在sort排序之后,即使两者的数量相同了,因为我们排序的原因,就可以使输出的仍然是较小值的i
C语言输入字符串
char s[100];
fgets(s,100,stdin);
但是C++输入一个字符串:
// 读取输入 cout << "Enter a string: ";
getline(cin, s); // 使用 getline 读取包含空格的字符串
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
string s; //输入的字符串
int maxn = 0;
char ch = '\0';//初始化频率最高的字符为'\0';
void Max() {
for (int i = 0; i < s.size(); i++){
//利用 sort 计算字符 s[i] 在字符串中的出现次数
int cnt = count(s.begin(),s.end(),s[i]);
//保证先 sort() 再 Max() 这样就不用判断相等输出最小值i了
if (cnt > maxn){
maxn = cnt;
ch = s[i];
}
}
}
int main() {
//读取输入:
cout << "Enter a string:~";
getline(cin,s);
sort(s.begin(),s.end());
Max();
cout << "character: " << ch << " frequency:" << maxn << "" << endl;
}
出现次数超过一半的数:
给出一个含有n(0<n<= 1000)个整数的数组,请找出其中出现次数超过一半的数。数组中的数大于-50月小于50。
【输入】
第一行包含一个整数n,表示数组大小;
第二行包含n个整数,分别是数组中的每个元素,相邻两个元素之间用单个空格隔开。【输入样例】
1 2 2
【输出样例】
2
C++用count if (count(a,a+n,a[i])>n/2) return true;
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
const int N = 1001;
int a[N], n;
void Input(){
cin >> n;
for (int i = 0; i < n; i++){ // 这里应该是 i < n,避免读取多余元素
cin >> a[i];
}
}
void Output(){
for (int i = 0; i < n; i++){ // 这里应该是 i < n,避免检查多余元素
if (count(a, a + n, a[i]) > n / 2){
cout << a[i];
return;
}
}
cout << "0";
}
int main() {
Input();
Output();
return 0;
}
明明的随机数:
#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1001;
int arr[N];
int n;
void Input() {
cin >> n;
for (int i = 0; i < n; i++){
cin >> arr[i];
}
return;
}
void Output() {
int index = unique(arr, arr + n) - arr;
int cnt = index;
cout << cnt << endl;
for (int i = 0; i < cnt; i++){
cout << arr[i] << ' ';
}
}
int main() {
Input();
sort(arr,arr + n);
Output();
}