剑指offer打卡 week2
24. 机器人的运动范围
https://www.acwing.com/problem/content/22/
class Solution {
public:
int get_sum(pair<int, int> p) {
int s = 0;
while (p.first) {
s += p.first % 10;
p.first /= 10;
}
while (p.second) {
s += p.second % 10;
p.second /= 10;
}
return s;
}
int movingCount(int threshold, int rows, int cols)
{
if (!rows || !cols) return 0;
queue<pair<int,int>> q;
vector<vector<bool>> st(rows, vector<bool>(cols, false));
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int res = 0;
q.push({0, 0});
while (q.size()) {
auto t = q.front();
q.pop();
if (st[t.first][t.second] || get_sum(t) > threshold) continue;
res ++ ;
st[t.first][t.second] = true;
for (int i = 0; i < 4; i ++ ) {
int x = t.first + dx[i], y = t.second + dy[i];
if (x >= 0 && x < rows && y >= 0 && y < cols) q.push({x, y});
}
}
return res;
}
};
25. 剪绳子
https://www.acwing.com/problem/content/24/

class Solution {
public:
int maxProductAfterCutting(int n) {
if(n<=3) return 1*(n-1);
long long res=1;
if(n%3==1){
res=4;
n-=4;
}
else if(n%3==2){
res=2;
n-=2;
}
while(n){
n-=3;
res*=3;
}
return res;
}
};
26. 二进制中1的个数
https://www.acwing.com/problem/content/25/

class Solution {
public:
int fun(int n){
int x=0;
long long temp=1;
while(n){
if(temp>n){
n-=temp/2;
temp=1;
x++;
}
temp*=2;
}
return x;
}
int NumberOf1(int n) {
int ans=0;
if(n==0) return 0;
else if(n>0) ans=fun(n);
else ans=32-fun(n*-1-1);
return ans;
}
};
27. 数值的整数次方
https://www.acwing.com/problem/content/26/
class Solution {
public:
double Power(double x, int n) {
typedef long long LL;
bool is_minus = n < 0;
double res = 1;
for (LL k = abs(LL(n)); k; k >>= 1) {
if (k & 1) res *= x;
x *= x;
}
if (is_minus) res = 1 / res;
return res;
}
};
28. 在O(1)时间删除链表结点
https://www.acwing.com/problem/content/85/
思路:
由于找不到前继节点,可以换个思路 复制下一个的值然后删除下一个节点达到目的
class Solution {
public:
void deleteNode(ListNode* node) {
auto nextptr=node->next;
node->val=nextptr->val;
node->next=nextptr->next;
delete nextptr;
}
};
29. 删除链表中重复的节点
https://www.acwing.com/problem/content/27/
class Solution {
public:
ListNode* deleteDuplication(ListNode* head) {
auto headptr=new ListNode(-1);
headptr->next=head;
auto p=headptr;
while(p->next){
auto q=p->next;
while(q && p->next->val==q->val) q=q->next;
if(p->next->next==q) p=p->next;
else p->next=q;
}
return headptr->next;
}
};
32. 调整数组顺序使奇数位于偶数前面
https://www.acwing.com/problem/content/30/
class Solution {
public:
void reOrderArray(vector<int> &array) {
int l=0,r=array.size()-1;
while(l<r){
while(l<r && array[l]%2==1) l++;
while(l<r && array[r]%2==0) r--;
if(l<r){
swap(array[l],array[r]);
}
}
}
};
33. 链表中倒数第k个节点
https://www.acwing.com/problem/content/32/
class Solution {
public:
ListNode* findKthToTail(ListNode* pListHead, int k) {
if(pListHead==NULL) return NULL;
auto head=pListHead;
int len=1;
while(pListHead->next){
len++;
pListHead=pListHead->next;
}
if(k>len) return NULL;
len=len-k;
pListHead=head;
while(len--){
pListHead=pListHead->next;
}
return pListHead;
}
};
34. 链表中环的入口结点
https://www.acwing.com/problem/content/86/
class Solution {
public:
ListNode *entryNodeOfLoop(ListNode *head) {
if (!head || !head->next) return 0;
auto first=head;
auto second=head;
while(first->next && first->next->next){
first=first->next->next;
second=second->next;
if(first==second) {
second=head;
while(first!=second){
first=first->next;
second=second->next;
}
return second;
}
}
return NULL;
}
};

浙公网安备 33010602011771号