剑指offer第九天

1.数组中只出现一次的两个数

cpp 复制代码
#include <vector>
class Solution {
public:
    vector<int> FindNumsAppearOnce(vector<int>& nums) {
        // write code here
        int v = 0;
        for(int&x:nums)
            v^=x;
        int cnt = 0;
        while(v)
        {
            v>>=1;
            cnt++;
        }
        int a = 0,b = 0;
        for(int&x:nums)
        {
            if(x&(1<<(cnt-1)))a^=x;
            else b^=x;
        }
        return vector<int>{min(a,b),max(a,b)};
    }
};

2.求1+2+...+n

cpp 复制代码
class Solution {
public:
    int getSum(int n) {
        return 1ll*n*(n+1)/2;
    }
};

3.顺时针打印矩阵

cpp 复制代码
class Solution {
public:
    vector<int> printMatrix(vector<vector<int> > matrix) {
        vector<int> res;
        int n = matrix.size();
        //先排除特殊情况
        if(n == 0) 
            return res;
        //左边界
        int left = 0; 
        //右边界
        int right = matrix[0].size() - 1; 
        //上边界
        int up = 0; 
        //下边界
        int down = n - 1; 
        //直到边界重合
        while(left <= right && up <= down){ 
            //上边界的从左到右
            for(int i = left; i <= right; i++) 
                res.push_back(matrix[up][i]); 
            //上边界向下
            up++; 
            if(up > down)
                break;
            //右边界的从上到下
            for(int i = up; i <= down; i++) 
                res.push_back(matrix[i][right]);
            //右边界向左
            right--; 
            if(left > right)
                break;
            //下边界的从右到左
            for(int i = right; i >= left; i--) 
                res.push_back(matrix[down][i]);
            //下边界向上
            down--; 
            if(up > down)
                break; 
            //左边界的从下到上
            for(int i = down; i >= up; i--) 
                res.push_back(matrix[i][left]);
            //左边界向右
            left++; 
            if(left > right)
                break;
        }
        return res;
    }
};

4.扑克牌顺子

cpp 复制代码
class Solution {
    bool hash[14] = { 0 };
  public:
    bool IsContinuous(vector<int>& numbers) {
        int maxVal = 0, minVal = 14;
        for (int& x : numbers) {
            if (x) {
                if (hash[x]) return false;
                hash[x] = true;
                maxVal = max(maxVal, x);
                minVal = min(minVal, x);
            }
        }
        return maxVal - minVal <= 4;
    }
};

5.构建乘积数组

cpp 复制代码
class Solution {
public:
    vector<int> multiply(vector<int>& A) {
        // write code here
        //看起来数值比较小,可以直接使用暴力的做法去乘
        int n = A.size();
        vector<int>ans(n,1);
        for(int i = 0;i<n;++i)
        {
            for(int j = 0;j<n;++j)
            {
                if(j == i)continue;
                ans[i] *= A[j];
            }
        }
        return ans;
    }
};

6.字符串中第一次只出现一次的字符

cpp 复制代码
class Solution {
    int cnt[128]{};
public:
    char firstNotRepeatingChar(string s) {
        for(char&c:s)
            cnt[c]++;
        for(char&c:s)
            if(cnt[c]==1)return c;
        return '#';
    }
};

7.替换空格

python 复制代码
# -*- coding:utf-8 -*-
class Solution:
    # s 源字符串
    def replaceSpace(self, s):
        return "%20".join(list(s.split(' ')))
相关推荐
灵感__idea1 小时前
Hello 算法:贪心的世界
前端·javascript·算法
澈2072 小时前
深入浅出C++滑动窗口算法:原理、实现与实战应用详解
数据结构·c++·算法
ambition202423 小时前
从暴力搜索到理论最优:一道任务调度问题的完整算法演进历程
c语言·数据结构·c++·算法·贪心算法·深度优先
cmpxr_3 小时前
【C】原码和补码以及环形坐标取模算法
c语言·开发语言·算法
qiqsevenqiqiqiqi3 小时前
前缀和差分
算法·图论
代码旅人ing3 小时前
链表算法刷题指南
数据结构·算法·链表
Yungoal3 小时前
常见 时间复杂度计算
c++·算法
不爱吃炸鸡柳4 小时前
单链表专题(完整代码版)
数据结构·算法·链表
CylMK4 小时前
题解:AT_abc382_d [ABC382D] Keep Distance
算法
Dfreedom.4 小时前
计算机视觉全景图
人工智能·算法·计算机视觉·图像算法