剑指 Offer 35. 复杂链表的复制
class Solution
{
public:
Node* copyRandomList(Node* head)
{
if (head == nullptr)
{
return nullptr;
}
Node* cur = head;
unordered_map<Node*, Node*> mp;
while (cur != nullptr)
{
mp[cur] = new Node(cur->val);
cur = cur->next;
}
cur = head;
while (cur != nullptr)
{
mp[cur]->next = mp[cur->next];
mp[cur]->random = mp[cur->random];
cur = cur->next;
}
return mp[head];
}
};
剑指 Offer 05. 替换空格
class Solution
{
public:
string replaceSpace(string s)
{
int old_size = s.size();
int blank = 0;
for (char& s1 : s)
{
if (s1 == ' ')
{
blank++;
}
}
s.resize(old_size + 2 * blank);
int new_size = s.size();
for (int i = old_size - 1, j = new_size - 1; i >= 0 && j >= 0; i--, j--)
{
if (s[i] != ' ')
{
s[j] = s[i];
}
else
{
s[j] = '0';
s[j - 1] = '2';
s[j - 2] = '%';
j -= 2;
}
}
return s;
}
};
剑指 Offer 58 - II. 左旋转字符串
class Solution
{
public:
string reverseLeftWords(string s, int n)
{
string res1(s.begin() + n, s.end());
string res2(s.begin(), s.begin() + n);
string res = res1 + res2;
return res;
}
};
剑指 Offer 03. 数组中重复的数字
class Solution
{
public:
int findRepeatNumber(vector<int>& nums)
{
vector<bool> res(nums.size(), false);
for (int i = 0; i < nums.size(); i++)
{
if (res[nums[i]] == false)
{
res[nums[i]] = true;
}
else
{
return nums[i];
}
}
return -1;
}
};