k个k进制的数字进行不进位加法,结果为0
时空复杂度O(n)
class Solution {
public:
string decTok(int dec,int k){
string ret = "";
while(dec){
ret += char(dec%k + '0');
dec /= k;
}
reverse(ret.begin(),ret.end());
return ret;
}
int kTodec(string ans,int k){
int times = ans.size() - 1;
int res = 0;
for(int i = 0; i < ans.size();i++){
res += (ans[i] - '0')* pow(k,times--);
}
return res;
}
int findNumberAppearingOnce(vector<int>& nums) {
int size = nums.size(),k = 3,maxlen = 0;
string str[size];
for(int i = 0; i < size;i++){
str[i] = decTok(nums[i],k);
}
for(int i = 0; i < size;i++){
int len = str[i].size();
maxlen = max(maxlen,len);
}
for(int i = 0; i < size; i++){
while(str[i].size() < maxlen){
str[i] = "0" + str[i];
}
}
string ans(maxlen,'0');
for(int i = 0;i < size;i++){
for(int j = 0;j < maxlen;j++){
ans[j] = char(((str[i][j] - '0') + (ans[j] - '0')) % k + '0');
}
}
return kTodec(ans,k);
}
};
标签:maxlen,Acwing74,++,int,str,ans,size
From: https://www.cnblogs.com/poteitoutou/p/17077298.html