y như cái solution của mình, mình gặp digit sẽ tính số item trước nó, thằng sau gặp digit thì lấy số items trước đó * với digit value.em còn định gắn chuỗi nào gặp số thì nhân chuỗi trước đó theo số lần
xong nhìn chuỗi dài quá, tắt điện)
Test cases tới 2^63 mà concat gì mai fencePass hết test case nhưng mà dính Memory Limit Exceeded, concat string là cách sai lầm![]()
mình đọc constrains là biết phải có 1 cách O(n) mới giải quyết được vấn đề, medium mà khó vl
hèn gì gần đây ko có thằng nào hỏi lúc phỏng vấn
function decodeAtIndex(s: string, k: number): string {
let curSize = 0;
let i = 0;
while (curSize < k) {
if (!isNaN(Number(s[i]))) {
curSize *= Number(s[i]);
} else curSize++;
i++;
}
for (let j = i - 1; j >= 0; j--) {
if (!isNaN(Number(s[j]))) {
curSize /= Number(s[j]);
k %= curSize;
} else {
if (k === 0 || k === curSize) {
return s[j];
}
curSize--;
}
}
return "bai nhu cc";
};
data type mới là điểm thêm khó cho cái bài ấy chứ thím. Chứ cứ như Python với JS thì bao giờ mới thấy khóHọc python code alogrithm thôi. Code C# như cái cc cay quá, trash language. Để ý bao nhiêu lần rồi, chắc lần này bỏ hẳn C# cmnl.
Code O(n) mà nhìn run time + memory đúng chán. Lại còn dính edge cases về data type tùm lum
Xem tệp đính kèm 2094812

return thế kia auto pass tier 1 ở VN rồi, tại hạ bái phụcChắc đ ai thèm làm bằng TS luôn, beat 100% TC, SC
JavaScript:function decodeAtIndex(s: string, k: number): string { let curSize = 0; let i = 0; while (curSize < k) { if (!isNaN(Number(s[i]))) { curSize *= Number(s[i]); } else curSize++; i++; } for (let j = i - 1; j >= 0; j--) { if (!isNaN(Number(s[j]))) { curSize /= Number(s[j]); k %= curSize; } else { if (k === 0 || k === curSize) { return s[j]; } curSize--; } } return "bai nhu cc"; };

class Solution:
def decodeAtIndex(self, S: str, K: int) -> str:
size = 0
for char in S:
if char.isdigit():
size *= int(char)
else:
size += 1
for char in reversed(S):
K %= size
if K == 0 and char.isalpha():
return char
if char.isdigit():
size //= int(char)
else:
size -= 1
class S {
inner = null;
times = 0;
suffix = '';
_length = null;
at(idx) {
const innerLength = this.inner?.length() ?? 0;
const prefixLength = innerLength ? innerLength * this.times : 0;
if (idx >= prefixLength) {
return this.suffix[idx - prefixLength];
} else {
return this.inner.at(idx % innerLength);
}
}
length() {
return this._length ??= (() =>
(this.inner ? this.inner.length() * this.times : 0) + this.suffix.length
)();
}
}
/**
* @param {string} s
* @param {number} k
* @return {string}
*/
var decodeAtIndex = function (s, k) {
let t = new S();
for (const ch of s) {
if (ch.match(/[a-z]/)) {
t.suffix += ch;
} else {
t = Object.assign(new S(), {
inner: t,
times: Number(ch),
});
}
}
return t.at(k - 1);
};
hic vậy mà em nghĩ nó chỉ cần a lần gọi nhanh hơn a*2-1 lần for; code giấy k test time đcBacktracking thường có time complexity tệ nhất trong tất cả các dạng rồi, nên nhìn vào constrain trước khi quyết định xài backtracking nha
via theNEXTvoz for iPhone
class Solution:
def decodeAtIndex(self, s: str, k: int) -> str:
current_length = 1
current_index = 1
while current_index < len(s):
if s[current_index].isalpha():
current_length += 1
else:
current_length *= int(s[current_index])
if current_length >= k:
break
current_index += 1
while current_index >= 0:
if s[current_index].isdigit():
current_length //= int(s[current_index])
k = current_length if k % current_length == 0 else k % current_length
elif k == current_length:
return s[current_index]
else:
current_length -= 1
current_index -= 1
return ""
class Solution {
public:
string decodeAtIndex(string_view s, int k) {
for (char lastChar{};;) {
for (uint64_t len = 0; char c : s) {
uint64_t newLen = len;
if (isalpha(c)) {
++newLen;
lastChar = c;
} else {
newLen *= c - '0';
}
if (newLen == k) return string(1, lastChar);
if (newLen > k) {
k %= len;
if (k == 0) return string(1, lastChar);
break;
}
len = newLen;
}
}
return {};
}
};
bài nay dễ quápublic class Solution {
public int[] SortArrayByParity(int[] nums) {
var left = 0;
var right = nums.Length -1;
while(left < right)
{
while(nums[left]%2 == 0 && left < right)
{
left++;
}
while(nums[right]%2 == 1 && left < right)
{
right--;
}
var temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
left++;
right--;
}
return nums;
}
}
Kêu code bừa vẫn ăn thành ra sỉ nhục anh em tôi quámém tí quên làm, bài này code bừa cũng ăn mà![]()
C++:class Solution { public: string decodeAtIndex(string_view s, int k) { for (char lastChar{};;) { for (uint64_t len = 0; char c : s) { uint64_t newLen = len; if (isalpha(c)) { ++newLen; lastChar = c; } else { newLen *= c - '0'; } if (newLen == k) return string(1, lastChar); if (newLen > k) { k %= len; if (k == 0) return string(1, lastChar); break; } len = newLen; } } return {}; } };

class Solution {
public:
vector<int> sortArrayByParity(vector<int>& nums) {
sort(nums.begin(), nums.end(), [](int a, int b){
if(a % 2 == 0 && b % 2 == 1)
return true;
return false;
});
return nums;
}
};
function sortArrayByParity(nums: number[]): number[] {
for (let i = 0, j = 0; j < nums.length; j++) {
if (nums[j] % 2 === 0) {
let temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i++;
}
}
return nums;
}
std::ranges::sort
struct Solution {
vector<int> sortArrayByParity(vector<int>& nums) {
ranges::sort(nums, less{}, [](int n){ return n % 2; });
return move(nums);
}
};