随笔分类 - 剑指offer
摘要:题目: 解答: 本题的简单解法: 1 class Solution { 2 public: 3 vector<int> printNumbers(int n) 4 { 5 vector<int> res; 6 7 if (n == 0) 8 { 9 return res; 10 } 11 12 //
阅读全文
摘要:题目: 解答: 在无重复字符代码的基础上先对字符串进行排序,这样重复字符必然相邻,然后在回溯过程中加一句判断条件去除重复排列。 1 class Solution 2 { 3 public: 4 vector<string> permutation(string S) 5 { 6 vector<str
阅读全文
摘要:题目: 解答: 1 class Solution { 2 vector<string>ans; 3 void backtracking(string &s,int start) 4 { 5 if(start==s.size()) 6 { 7 ans.emplace_back(s); 8 } 9 fo
阅读全文
摘要:题目: 解答: class Solution { public: int convertInteger(int A, int B) { int res = 0; int temp = A ^ B; while (temp!=0) { int lowbit = temp & (-temp); res+
阅读全文
摘要:题目: 解答: 通过中序遍历进行比较。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : v
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) :
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) :
阅读全文
摘要:题目: 解答: 利用两个栈s1,s2,其中s1存放排序数据,当执行push()时,若s1.top()<val,将所有小于val的元素放入s2,再将val压入s1中,最后将s2中的元素放入s1中,实现s1的排序push。 1 class SortedStack { 2 public: 3 stack<
阅读全文
摘要:题目: 解答: 第一个栈保存正常push,另一个保存逆序栈,也就是队列顺序。第二个优先于第一个栈。 1 class MyQueue { 2 public: 3 stack<int> que; 4 stack<int> temp; 5 /** Initialize your data structur
阅读全文
摘要:题目: 解答: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {
阅读全文
摘要:题目: 解答: 常量空间的话,第一可以考虑是不是固定数量的几个变量能搞定;否则可以考虑是不是问题本身已经提供了足够的空间。 这道题目属于后者,就是利用矩阵的第一行和第一列来作为辅助空间使用。不用开辟新的存储空间。方法就是: A. 先确定第一行和第一列是否需要清零; B. 扫描剩下的矩阵元素,如果遇到
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int maxValue(vector<vector<int>>& grid) 4 { 5 int m = grid.size(); 6 int n = grid[0].size(); 7 8 for (int i = 0
阅读全文
摘要:题目: 解答: 思路:滑动窗口。 我们可以使用哈希表记录每个字符的下一个索引,然后尽量向右移动尾指针来拓展窗口,并更新窗口的最大长度。如果尾指针指向的元素重复,则将头指针直接移动到窗口中重复元素的右侧。 1 class Solution { 2 public: 3 int lengthOfLonge
阅读全文
摘要:题目: 解法: /* * 位运算 * * 因为不能使用加减乘除四则运算,所以只能想到使用二进制的位运算实现相加操作。 * 二进制位运算中,异或操作: 1^1=0 0^0=0 1^0=1 0^1=1,可以模拟无进位的加操作; * 与操作:1&1=1 0&1=0 1&0=0 0&0=0,可以模拟进位的情
阅读全文
摘要:题目: 解答: 思路描述: 计算除 A[i] 以外所有元素的乘积,如果我们使用两层遍历就会重复计算很多次乘法, 其实就相当于求 A[i] 左边所有元素的乘积之和和右边所有元素的乘积 如果我们每次只考虑一边的乘积的话,比如left[i]代表i左侧的乘积,那么left[i + 1] = A[i] * l
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 vector<int> constructArr(vector<int>& a) 、 4 { 5 int n = a.size(); 6 vector<int> ret(n, 1); 7 8 int left = 1; 9
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int sumNums(int n) 4 { 5 if(n == 1) 6 { 7 return 1; 8 } 9 n += sumNums(n - 1); 10 return n; 11 } 12 };
阅读全文
摘要:题目: 解答: 1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) 4 { 5 int cost = INT_MAX; 6 int profit = 0; 7 for (int price: prices) 8 { 9
阅读全文
摘要:题目: 解答: 1 class Solution { 2 int f(int n, int m) 3 { 4 if (n == 1) 5 { 6 return 0; 7 } 8 int x = f(n - 1, m); 9 return (m + x) % n; 10 } 11 public: 12
阅读全文
摘要:题目: 解答: 思路描述: 这个就可以先排一下序,然后计算出来 0 的个数 num1,以及需要多少张牌才能够连续,比如: 1 2 4 需要一张牌才能连续,如果数目不大于 num1,那么就是顺子。其中注意出现相同的牌也不是顺子! 1 class Solution { 2 public: 3 bool
阅读全文

浙公网安备 33010602011771号