随笔分类 - 剑指Offer(第2版)
摘要:思路 方法:快慢指针 初始时,p,q都指向链表头节点,让q比p先走k步,之后p,q一起走,每次一步,当q为NULL的时候,p指向的就是倒数第k个节点。 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int
阅读全文
摘要:思路 方法:首尾双指针 定义头指针 i,尾指针 j.i 一直往右移,直到它指向的值为偶数j 一直往左移, 直到它指向的值为奇数交换 nums[i] 和 nums[j]重复上述操作,直到 i >= j 1 class Solution { 2 public: 3 vector<int> exchang
阅读全文
摘要:思路 方法一:递归 以下代码改成c++中string的写法,提交到C++中会超时,可能string比指针更慢吧。 以下C语言代码参考《剑指offer(第2版)》书中的代码,可以在leetcode中提交通过。 这种递归方法效率比较低下。 1 bool matchCore(const char* str
阅读全文
摘要:思路 转载自: 面试题68 - II. 二叉树的最近公共祖先(后序遍历 DFS ,清晰图解) 方法:后序遍历 (自底向上)
阅读全文
摘要:思路 方法一:迭代 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x)
阅读全文
摘要:思路 方法:模拟 本题难点在于怎么判断数字是否超过int的范围,这里有两个方法解决: (1) 使用long long直接判断 1 class Solution { 2 public: 3 int strToInt(string str) { 4 if(str.empty()) 5 return 0;
阅读全文
摘要:思路 方法:对称遍历 1 class Solution { 2 public: 3 vector<int> constructArr(vector<int>& a) { 4 if(a.empty()) 5 return vector<int>(); 6 7 vector<int> b(a.size(
阅读全文
摘要:思路 方法:使用位运算实现二进制加法 Java实现 1 class Solution { 2 public int add(int a, int b) { 3 int sum, carry; 4 while(b != 0) //运算到没进位为止 5 { 6 sum = a ^ b; // 用异或运算
阅读全文
摘要:思路 方法:使用逻辑&&运算符的短路特性终止递归 1 class Solution { 2 public: 3 int sumNums(int n) { 4 n > 1 && (n += sumNums(n - 1)); 5 return n; 6 } 7 };
阅读全文
摘要:思路 方法一:暴力法 1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) { 4 int n = (int)prices.size(), ans = 0; 5 for (int i = 0; i < n; ++i){ 6
阅读全文
摘要:思路 这是经典的约瑟夫环问题。 方法一:用链表模拟 用链表模拟整个游戏过程。如果单纯用链表模拟的话,每次需要从1数到m,才能踢除1个数,所以踢除n-1个数一共需要遍历(n-1)*m次,时间复杂度就是O(n*m),这种方法会超时。 方法二:数学递推公式 1 class Solution { 2 pub
阅读全文
摘要:思路 1. 先排序 2. 如果最大值-最小值>=5,返回false 3. 如果有重复数字,返回false 1 class Solution { 2 public: 3 bool isStraight(vector<int>& nums) { 4 //先排序 5 sort(nums.begin(),
阅读全文
摘要:思路 方法:动态规划 用dp[i][j]表示掷完 i 个骰子之后其点数之和为 j 的总次数,这可以由 投掷完 n-1 枚骰子后,对应点数 j-1, j-2, j-3, ... , j-6 出现的次数之和转化过来。 即: 1 class Solution { 2 public: 3 vector<do
阅读全文
摘要:思路 方法:维护一个单调的双端队列 1 class MaxQueue { 2 private: 3 queue<int> A; 4 deque<int> B; 5 public: 6 MaxQueue() { 7 8 } 9 10 int max_value() { 11 if(B.empty())
阅读全文
摘要:思路 方法一:暴力法 遍历每一个数nums[i],之后在[i, i+k]中顺序寻找最大值。 时间复杂度:O(k*n) 1 class Solution { 2 public: 3 vector<int> maxSlidingWindow(vector<int>& nums, int k) { 4 i
阅读全文
摘要:思路 方法一:库函数rotate() 1 class Solution { 2 public: 3 string reverseLeftWords(string s, int n) { 4 rotate(s.begin(), s.begin()+n, s.end()); 5 return s; 6
阅读全文
摘要:思路 方法一:分割 + 倒序 时间复杂度:O(n),n为s的长度。 1 class Solution { 2 public: 3 string reverseWords(string s) { 4 string t = ""; 5 stack<string> strStack; 6 for(int
阅读全文
摘要:思路 方法一:枚举 + 暴力 1 class Solution { 2 public: 3 vector<vector<int>> findContinuousSequence(int target) { 4 vector<vector<int>> res; 5 for(int i = 1; i <
阅读全文
摘要:思路 方法一:二分 遍历每个数字num,然后再在后面的数字中使用二分查找target-num。 复杂度分析 时间复杂度:O(nlogn) 空间复杂度:O(1) 1 class Solution { 2 public: 3 vector<int> twoSum(vector<int>& nums, i
阅读全文
摘要:思路 方法:位运算 遍历统计 1 class Solution { 2 private: 3 int cnt[32] = {0}; 4 public: 5 int singleNumber(vector<int>& nums) { 6 for(int num: nums) { 7 for(int i
阅读全文