A
int count[] = new int[1001]
已经默认为0,int b[1001]
定义在全局变量
map[k,v]求最大值用替代法 求数组中出现次数最多的数值先得到最大次数然后遍历得原数
map<int,int> a;a[nums[i]]++;
for (map<int,int>::iterator it=mp.begin();it!=mp.end();it++)
{
if (it->second>nn)
{
ans=it->first;
nn=it->second;
}
}
ntmdkkbr
int n=a.size(),i,j,k;
map<int,int> cnt;
int mx=0;
for (i=0;i+1<n;i++) if (a[i]==key) ++cnt[a[i+1]];
for (auto [x,y]:cnt) mx=max(mx,y);
for (auto [x,y]:cnt) if (y==mx) return x;
return 0;
int b[1100];
int mostFrequent(vector<int>& nums, int key) {
int x=0;
for(int i=1;i<nums.size();i++){
if(nums[i-1]==key&& ++b[nums[i]]>b[x])x=nums[i];
}
return x;
}
//无语亲亲
int b[1100];
int mostFrequent(vector<int>& nums, int key) {
vector<int> a;
int t=0;
for(int i=0;i<nums.size()-1;i++){
if(nums[i]==key)a.push_back(nums[i+1]);
}
for(int i=0;i<a.size();i++){
b[a[i]]++;
}
int maxcount = b[a[0]];
for(int j = 1; j < a.size(); j++)
{
if(b[a[j]] > maxcount){
t = a[j];
maxcount=b[a[j]];
}
}
return t;
B
struct Node {
int idx, l, r;
bool operator <(const Node& o) const {
if (r == o.r) {
return idx < o.idx;
}
return r < o.r;
}
};
class Solution {
public:
vector<int> sortJumbled(vector<int>& m, vector<int>& a) {
auto calc = [&](int x) -> int {
string s = to_string(x);
for (auto& c: s) {
c = (char)m[c - '0'] + '0';
}
int res = 0;
for (auto& c: s) {
res = res * 10 + (c - '0');
}
return res;
};
vector<Node> b;
for (int i = 0; i < a.size(); ++i) b.push_back({i, a[i], calc(a[i])});
sort(b.begin(), b.end());
vector<int> ans;
for (auto& it: b) ans.push_back(it.l);
return ans;
}
};
auto f = [&](int x) {
if (x == 0) return mapping[x];
int y = 0;
for (int p = 1; x; p *= 10) {
y += mapping[x % 10] * p;
x /= 10;
}
return y;
};
stable_sort(nums.begin(), nums.end(), [&](int x, int y){
return f(x) < f(y);
});
return nums;
int n=a.size(),i,j,k;
vector<tuple<int,int,int>> v(n);
for (i=0;i<n;i++)
{
string s=to_string(a[i]);
for (auto &c:s) c=mp[c-'0']+'0';
v[i]={stoi(s),i,a[i]};
}
sort(all(v));
vector<int> b(n);
for (i=0;i<n;i++) tie(j,k,b[i])=v[i];
return b;
vector<pair<int,int>>v;
for(int a:nums){
string s=to_string(a);
for(char&c:s)c='0'+mapping[c-'0'];
v.push_back({stoi(s),a});
}
stable_sort(v.begin(),v.end(),[](auto&x,auto&y){return x.first<y.first;});
vector<int>ans;
for(auto [a,b]:v)ans.push_back(b);
return ans;
D
int ans = 0;
while (s.size() > 1) {
int r = s.size() - 1;
while (r and s[r] != s[0]) r -= 1;
if (r == 0) {
ans += s.size() / 2;
s = s.substr(1);
}
else {
ans += s.size() - r - 1;
s = s.substr(1, r - 1) + s.substr(r + 1);
}
}
return ans;
int n = s.length();
int j = n - 1;
int cnt = 0; //记录交换次数
int flag = 0;//标记是否已经存在个数为奇数的字符
//从右端j开始寻找与s[i]相同的s[k]
for(int i = 0;i < j;i++){
for(int k = j;k >= i;k--){
if(k == i){//找到了单独的一个字符,
cnt += n / 2 - i;
}else{
if(s[i] == s[k]){ //把找到的s[k]移到最右端
for(int l = k;l < j;l++){
swap(s[l],s[l+1]);
cnt++;
}
j--;
break;
}
}
}
}
return cnt;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(x) (x).begin(),(x).end()
#define pb(x) push_back(x)
#define eb(x) emplace_back(x)
const int N=1e5+2;
char a[N];
class Solution {
public:
int minMovesToMakePalindrome(string s) {
int n=s.size(),i,j,l=1,r=n,ans=0;
for (i=1;i<=n;i++) a[i]=s[i-1];
while (l<r)
{
if (a[l]!=a[r])
{
for (i=r;;i--) if (a[i]==a[l]) break;
for (j=l;;j++) if (a[j]==a[r]) break;
if (r-i<j-l)
{
ans+=r-i;
rotate(a+i,a+i+1,a+r+1);
}
else
{
ans+=j-l;
rotate(a+l,a+j,a+j+1);
}
}
++l;--r;
}
return ans;
}
};