剑指offer(C++)——其他算法

本系列笔记供自学算法用,仅记载题解代码和部分题解思路,所有题目版权归LeeCode及《剑指offer》所有;推荐一超棒的刷题笔记系列博客,本系列的题型分类均有所参考:剑指Offer系列刷题笔记汇总

注:题目序号以现官网中排序为准,或和其他参考有所出入;

15.二进制中1的个数

编写一个函数,输入是一个无符号整数(以二进制串的形式),返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量)。

输入:n = 11 (控制台输入 00000000000000000000000000001011)
输出:3
解释:输入的二进制串 00000000000000000000000000001011 中,共有三位为 ‘1’。

解答:

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int ans = 0;
        while (n != 0)
        {
            ++ans;
            n &= (n - 1);//消除n的最低位1
            //n = (n - 1) & n;
        }
        return ans;
    }
};
16.数值的整数次方

实现 pow(x, n),即计算 x 的 n 次幂函数(即x^n)。不得使用库函数,同时不需要考虑大数问题。

输入:x = 2.00000, n = 10
输出:1024.00000

提示:
-100.0 < x < 100.0
-231 <= n <= 231-1
-104 <= xn <= 104

解答:

···> 图源及题解原文地址
在这里插入图片描述

class Solution {
public:
    double myPow(double x, int n) {
        if (x == 1 || n == 0) return 1; // 特殊情况
        double ret = 1; // 运算结果
        long exp = long(n);
        // 若指数为负数,就转换成多个底数的倒数进行运算
        if (n < 0) {
            x = 1/ x;
            exp = -exp;
        }
        // 进行快速幂
        while (exp) {  
            if (exp & 1) ret *= x; //编译后所有数据都是二进制表示故可运用右移
            x *= x;//每次代表x的1,2,4,8次方
            exp >>= 1;
        }
        return ret;
    }
};
//作者:RyanWangllng
//链接:https://leetcode-cn.com/problems/shu-zhi-de-zheng-shu-ci-fang-lcof/solution/zhu-shi-xing-ti-jie-kuai-su-mi-jian-zhi-rc5mo/


class Solution {  //另一种迭代解法
public:
    double myPow(double x, int n) {
        if(n==0) return 1;
        if(n==-1) return 1/x;
        if(n&1) return myPow(x*x,n>>1)*x;
        else return  myPow(x*x,n>>1);
    }
};
//来源用户:https://leetcode-cn.com/u/luo-an/
29.顺时针打印矩阵

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。

输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]

限制:
0 <= matrix.length <= 100
0 <= matrix[i].length <= 100

解答:

class Solution {		//模拟打印路径
private:
    static constexpr int directions[4][2] = {
  {0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        if (matrix.size() == 0 || matrix[0].size() == 0) {
            return {};
        }
        
        int rows = matrix.size(), columns = matrix[0].size();
        vector<vector<bool>> visited(rows, vector<bool>(columns));
        int total = rows * columns;
        vector<int> order(total);

        int row = 0, column = 0;
        int directionIndex = 0;
        for (int i = 0; i < total; i++) {
            order[i] = matrix[row][column];
            visited[row][column] = true;
            int nextRow = row + directions[directionIndex][0], nextColumn = column + directions[directionIndex][1];
            if (nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || visited[nextRow][nextColumn]) {
                directionIndex = (directionIndex + 1) % 4;
            }
            row += directions[directionIndex][0];
            column += directions[directionIndex][1];
        }
        return order;
    }
};

//作者:LeetCode-Solution
//链接:https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/solution/shun-shi-zhen-da-yin-ju-zhen-by-leetcode-solution/
class Solution {		//按层模拟
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        if (matrix.size() == 0 || matrix[0].size() == 0) {
            return {};
        }

        int rows = matrix.size(), columns = matrix[0].size();
        vector<int> order;
        int left = 0, right = columns - 1, top = 0, bottom = rows - 1;
        while (left <= right && top <= bottom) {
            for (int column = left; column <= right; column++) {
                order.push_back(matrix[top][column]);
            }
            for (int row = top + 1; row <= bottom; row++) {
                order.push_back(matrix[row][right]);
            }
            if (left < right && top < bottom) {
                for (int column = right - 1; column > left; column--) {
                    order.push_back(matrix[bottom][column]);
                }
                for (int row = bottom; row > top; row--) {
                    order.push_back(matrix[row][left]);
                }
            }
            left++;
            right--;
            top++;
            bottom--;
        }
        return order;
    }
};

//作者:LeetCode-Solution
//链接:https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/solution/shun-shi-zhen-da-yin-ju-zhen-by-leetcode-solution/

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        vector<int> v;
        int rows = matrix.size();
        if(rows==0) return v;
        int cols = matrix[0].size();
        if(cols==0) return v;
        int up = 0,down = rows-1,left = 0,right = cols-1;
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值