随笔分类 - 剑指offer
算法学习
摘要:题目链接:https://leetcode-cn.com/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/ 数学 先确定第n位所在的数字是几位数; n - 10 - 90 * 2 - 900 * 3 - 9000 * 4 ... 确定第n
阅读全文
摘要:题目链接:https://leetcode cn.com/problems/er cha sou suo shu de hou xu bian li xu lie lcof/ 递归
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/shu-ju-liu-zhong-de-zhong-wei-shu-lcof/ 没有排序的数组 同面试题39题,利用分治的思想找出数组的中位数 插入 找中位数 时间复杂度 O(1) O(n) 排序数组 利用一个排序的数组,数
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/zui-chang-bu-han-zhong-fu-zi-fu-de-zi-zi-fu-chuan-lcof/ 双指针算法 i在前,j在后,分别指向不重复子串的头和尾;使用一个哈希表每个字符出现的次数,当遍历到j时,发现s[
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/1nzheng-shu-zhong-1chu-xian-de-ci-shu-lcof/ 按位统计 以abcdef为例,固定其中一位为1,然后统计: 0 ~ ab - 1: 左边:ab:00 ~ ab - 1, ab个 右边:
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/ba-shu-zi-fan-yi-cheng-zi-fu-chuan-lcof/ 递归 class Solution { public: int translateNum(int num) { if(num <= 9) re
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof/ 递归实现1 二叉搜索树进行中序遍历会得到递增排序的序列; 将递增排序的序列中每个元素的left和right修改,用两个指针
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/zi-fu-chuan-de-pai-lie-lcof/ 回溯1 class Solution { public: vector<string> ans; string path; vector<string> permut
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/chou-shu-lcof/ 常规解法 从0开始依次判断是不是丑数,判断到第n个返回; 算法直观,代码简介,但是效率不高,而且直接超时了 class Solution { public: bool isUgly(int n)
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof/ 哈希 用哈希表做一个映射,hashmap的key是原先的节点,val是复制的节点。先初始化hashmap,然后分别next指针和random指针。 哈希表的作用
阅读全文
摘要:自定义排序规则 假设nums任意两个数字的字符串格式x和y,则 若拼接字符串 x + y > y + x, 则 m > n; 反之,x + y < y + x, 则 n < m; 时间复杂度:O(nlogn) 空间复杂度:O(n) class Solution { public: string mi
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-ii-lcof/ 思路 关键点 还是从位运算的角度,考虑数字的二进制形式,各二进制位出现的次数都是3的倍数。说明出现了三次;因此,统计所有数字的各
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/qiu-12n-lcof/ 位运算 本题要求不使用乘除法,for/while/if/else/switch/case等关键字及条件判断语句; 使用递归来做,通常递归中条件的结束需要用到if,本题可以利用逻辑与的“短路”完成本
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/da-yin-cong-1dao-zui-da-de-nwei-shu-lcof/ class Solution { public: vector<int> printNumbers(int n) { vector<int>
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/bu-ke-pai-zhong-de-shun-zi-lcof/ 思路 时间复杂度:O(nlogn) 空间复杂度: O(1) 将数据排序 第一次遍历数组(过滤掉有重复元素的),如果遇到非0元素,并且前后两个数相等,说明不是顺
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/gou-jian-cheng-ji-shu-zu-lcof/ 时间复杂度O(n) 空间复杂度O(1) class Solution { public: vector<int> constructArr(const vecto
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/bu-yong-jia-jian-cheng-chu-zuo-jia-fa-lcof/ 位运算 分析十进制是如何做加法的?以5 + 17为例说明 1. 只做各位相加不进位 个位:5 + 7 = 12, 不要进位是2,十位:
阅读全文
摘要:题目链接:https://leetcode cn.com/problems/di yi ge zhi chu xian yi ci de zi fu lcof/ 哈希
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/shu-zu-zhong-chu-xian-ci-shu-chao-guo-yi-ban-de-shu-zi-lcof/ 循环抵消 因为数组中有一个数字出现的次数超过数组长度的一半,它出现的次数比所有其它数字出现的次数和还要
阅读全文
摘要:题目链接:https://leetcode-cn.com/problems/shun-shi-zhen-da-yin-ju-zhen-lcof/ 模拟 顺时针定义四个方向:上右下左。 从左上角开始遍历,先往右走,走到不能走为止,然后更改到下个方向,再走到不能走为止,依次类推,遍历 nm 个格子后停止
阅读全文