problemscpp
A collection of my answers to algorithm problems in c++.
静态 Public 成员函数 | 所有成员列表
leetcode::find_palindrome_with_fixed_length::Solution类 参考

#include <leetcode.h>

静态 Public 成员函数

static vector< long long > kthPalindrome (vector< int > &queries, int intLength)
 

详细描述

在文件 leetcode.h1940 行定义.

成员函数说明

◆ kthPalindrome()

vector< long long > leetcode::find_palindrome_with_fixed_length::Solution::kthPalindrome ( vector< int > &  queries,
int  intLength 
)
static

在文件 leetcode.cpp5093 行定义.

5093 {
5094 vector<long long> ans;
5095 if(intLength == 1) {
5096 for(auto query: queries) {
5097 if(query < 10) {
5098 ans.push_back(query);
5099 } else {
5100 ans.push_back(-1);
5101 }
5102 }
5103 return ans;
5104 }
5105 if(intLength == 2) {
5106 for(auto query: queries) {
5107 if(query < 10) {
5108 ans.push_back(query * 10 + query);
5109 } else {
5110 ans.push_back(-1);
5111 }
5112 }
5113 return ans;
5114 }
5115 int n = (intLength + 1) / 2;
5116 vector<unsigned long long> q10(n + 1);
5117 for(int i = 1; i <= n; i++) {
5118 q10[i] = qmi(10, n - i);
5119 }
5120 for(auto query: queries) {
5121 vector<unsigned long long> num;
5122 int n1 = query / q10[1] + 1;
5123 if(n1 == 10 && query % q10[1] == 0) {
5124 stringstream ss;
5125 for(int i = 0; i < intLength; i++) {
5126 ss << 9;
5127 }
5128 long long res;
5129 ss >> res;
5130 ans.push_back(res);
5131 continue;
5132 }
5133 if(n1 > 9) {
5134 ans.push_back(-1);
5135 continue;
5136 }
5137 if(n1 < 10 && query % q10[1] == 0) {
5138 n1--;
5139 query = (query - 1) % q10[1] + 1;
5140 } else {
5141 query %= q10[1];
5142 }
5143 num.push_back(n1);
5144 for(int i = 2; i <= n; i++) {
5145 int digit = query / q10[i];
5146 if(i == n) {
5147 digit--;
5148 } else if(query != q10[i] && query % q10[i] == 0) {
5149 digit--;
5150 } else if(query == q10[i]) {
5151 digit = 0;
5152 }
5153 query %= q10[i];
5154 num.push_back((digit + 10) % 10);
5155 }
5156 stringstream ss;
5157 for(int digit: num) {
5158 ss << digit;
5159 }
5160 int i = num.size() - 1;
5161 if(intLength % 2 != 0) {
5162 i--;
5163 }
5164 while(i >= 0) {
5165 ss << num[i];
5166 --i;
5167 }
5168 long long res;
5169 ss >> res;
5170 ans.push_back(res);
5171 }
5172 return ans;
5173 }
unsigned long long qmi(unsigned long long m, unsigned long long k)
Definition: leetcode.cpp:5080

引用了 leetcode::find_palindrome_with_fixed_length::qmi().

被这些函数引用 leetcode::find_palindrome_with_fixed_length::TEST().


该类的文档由以下文件生成: