剑指offer67题-No51.构建乘积数组
法1:暴力 时间复杂度为On^2 vector<int> multiply(vector<int>& A) { // write code here vector<int> res; int len = A.size(); for(int i = 0; i < len; ++i){ int b = 1; for(in…
2025-6-05 13:53
|
|
2025-6-05 13:53
剑指offer67题-No50.数组中重复的数字
在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。 法1:哈希表,时间复杂度On,空间复杂度On bool duplicate(int numbers[], int length, int* duplication…
2025-6-05 13:51
|
|
2025-6-05 13:52
剑指offer67题-No49.字符串转化为整数
模拟题,感觉挺复杂的 class Solution { public: int myAtoi(string str) { int len = str.size(); int i = 0, flag = 1; long long base = 0; // 跳过前导空格 while (i < len && str[i] == ' …
2025-6-05 13:50
|
|
2025-6-05 13:52
剑指offer67题-No48.求两个数相加
一般都是位运算,记住怎么用位运算求+吧。 可以参考:【每日算法Day 66】经典面试题:不用四则运算如何做加法? - 知乎 1)两个数做^运算,得到各位相加不进位的计算结果 2)两个数做&运算,得到进位。因为进位是要加到左一位的,所以需要左移1 3)循环操作,直到进位为0 int Add(int num1, int num2) { // 最…
2025-6-05 13:49
|
|
2025-6-05 13:49
剑指offer67题-No47.求1+2+3+…+n
求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。 用短路特性求解。 int Sum_Solution(int n) { n && (n = n + Sum_Solution(n-1)); return n; }
2025-6-02 15:24
|
|
2025-6-02 15:24
剑指offer67题-No46.孩子们的游戏(圆圈中最后剩下的数)
法1:约瑟夫环递归 n个数相后去掉第m个数,还剩下n−1个数,依然要继续去掉第m个数。由此,从(n,m)的问题变成了(n−1,m)的子问题。时间复杂度和空间复杂度都是On 说实话不理解,先背住吧 int f(int n,int m){ if(n == 1) return 0; else return (f(n-1,m) + m) % n; } in…
2025-6-02 15:23
|
|
2025-6-02 15:23
剑指offer67题-No45.扑克牌顺子
模拟写法,时间复杂度Onlogn,花在快排身上。 bool IsContinuous(vector<int>& numbers) { if(numbers.size() == 0) return false; sort(numbers.begin(), numbers.end()); int cnt = 0; // 统计赖子个数 fo…
2025-6-02 15:22
|
|
2025-6-02 15:22
剑指offer67题-No43-44.左旋转字符串、翻转单词序列
No43:简单的字符串处理。 string LeftRotateString(string str, int n) { // write code here if(str.size() == 0) return {}; n = n % str.size(); if(n == 0) return str; string res; for(int i …
2025-6-02 15:20
|
|
2025-6-02 15:20