Velikost videa: 1280 X 720853 X 480640 X 360
Zobrazit ovladače přehrávání
Automatické přehrávání
Přehrát
just watched the approach and code it myself... hats off, thanks for sharing the approach and for a very beautiful explanation, for each edge case
Glad it was helpful!
bhai everything is good but explanation thoda aur clear chaihiye tha , you should use hindi also to explain clearly , but thankyou so much
solution ->class Solution{public: bool allnine(int num[], int n) { for (int i = 0; i < n; i++) if (num[i] != 9) return false; return true; } vector generateNextPalindrome(int num[], int n) { if (allnine(num, n)) { vector ans; ans.push_back(1); for (int i = 0; i < n - 1; i++) { ans.push_back(0); } ans.push_back(1); return ans; } else if (n & 1) { bool flag = false; for (int i = 0; i < n / 2; i++) { if (num[i] > num[n - i - 1]) { flag = true; } else if ((num[i] == num[n - i - 1])) { continue; } else { flag = false; } num[n - i - 1] = num[i]; } if (flag == false) { int j = 0; for (; j < n / 2 - 1; j++) { if (num[n / 2 - j] == 9) { num[n / 2 - j] = 0; num[n / 2 + j] = 0; } else break; } if (j == 0) { num[n / 2 - j]++; } else { num[n / 2 - j]++; num[n / 2 + j]++; } } } else { bool flag = false; for (int i = 0; i < n / 2; i++) { if (num[i] > num[n - i - 1]) flag = true; else if ((num[i] == num[n - i - 1])) continue; else flag = false; num[n - i - 1] = num[i]; } if (flag == false) { int j = 0; for (; j < n / 2 - 1; j++) { if (num[n / 2 - j - 1] == 9) { num[n / 2 - j - 1] = 0; num[n / 2 + j] = 0; } else break; } num[n / 2 - j - 1]++; num[n / 2 + j]++; } } vector ans(n); for (int i = 0; i < n; i++) { ans[i] = num[i]; } return ans; }};
just watched the approach and code it myself... hats off, thanks for sharing the approach and for a very beautiful explanation, for each edge case
Glad it was helpful!
bhai everything is good but explanation thoda aur clear chaihiye tha , you should use hindi also to explain clearly , but thankyou so much
solution ->
class Solution
{
public:
bool allnine(int num[], int n)
{
for (int i = 0; i < n; i++)
if (num[i] != 9)
return false;
return true;
}
vector generateNextPalindrome(int num[], int n)
{
if (allnine(num, n))
{
vector ans;
ans.push_back(1);
for (int i = 0; i < n - 1; i++)
{
ans.push_back(0);
}
ans.push_back(1);
return ans;
}
else if (n & 1)
{
bool flag = false;
for (int i = 0; i < n / 2; i++)
{
if (num[i] > num[n - i - 1])
{
flag = true;
}
else if ((num[i] == num[n - i - 1]))
{
continue;
}
else
{
flag = false;
}
num[n - i - 1] = num[i];
}
if (flag == false)
{
int j = 0;
for (; j < n / 2 - 1; j++)
{
if (num[n / 2 - j] == 9)
{
num[n / 2 - j] = 0;
num[n / 2 + j] = 0;
}
else
break;
}
if (j == 0)
{
num[n / 2 - j]++;
}
else
{
num[n / 2 - j]++;
num[n / 2 + j]++;
}
}
}
else
{
bool flag = false;
for (int i = 0; i < n / 2; i++)
{
if (num[i] > num[n - i - 1])
flag = true;
else if ((num[i] == num[n - i - 1]))
continue;
else
flag = false;
num[n - i - 1] = num[i];
}
if (flag == false)
{
int j = 0;
for (; j < n / 2 - 1; j++)
{
if (num[n / 2 - j - 1] == 9)
{
num[n / 2 - j - 1] = 0;
num[n / 2 + j] = 0;
}
else
break;
}
num[n / 2 - j - 1]++;
num[n / 2 + j]++;
}
}
vector ans(n);
for (int i = 0; i < n; i++)
{
ans[i] = num[i];
}
return ans;
}
};