摘要:
题目: 解答: 在无重复字符代码的基础上先对字符串进行排序,这样重复字符必然相邻,然后在回溯过程中加一句判断条件去除重复排列。 1 class Solution 2 { 3 public: 4 vector<string> permutation(string S) 5 { 6 vector<str 阅读全文
posted @ 2020-05-09 23:37
梦醒潇湘
阅读(484)
评论(0)
推荐(0)
摘要:
题目: 解答: 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 阅读全文
posted @ 2020-05-09 23:31
梦醒潇湘
阅读(356)
评论(0)
推荐(0)
摘要:
题目: 解答: class Solution { public: int convertInteger(int A, int B) { int res = 0; int temp = A ^ B; while (temp!=0) { int lowbit = temp & (-temp); res+ 阅读全文
posted @ 2020-05-09 23:28
梦醒潇湘
阅读(123)
评论(0)
推荐(0)
摘要:
题目: 解答: 通过中序遍历进行比较。 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : v 阅读全文
posted @ 2020-05-09 23:21
梦醒潇湘
阅读(178)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : 阅读全文
posted @ 2020-05-09 23:17
梦醒潇湘
阅读(167)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : 阅读全文
posted @ 2020-05-09 23:14
梦醒潇湘
阅读(271)
评论(0)
推荐(0)
摘要:
题目: 解答: 利用两个栈s1,s2,其中s1存放排序数据,当执行push()时,若s1.top()<val,将所有小于val的元素放入s2,再将val压入s1中,最后将s2中的元素放入s1中,实现s1的排序push。 1 class SortedStack { 2 public: 3 stack< 阅读全文
posted @ 2020-05-09 23:09
梦醒潇湘
阅读(170)
评论(0)
推荐(0)
摘要:
题目: 解答: 第一个栈保存正常push,另一个保存逆序栈,也就是队列顺序。第二个优先于第一个栈。 1 class MyQueue { 2 public: 3 stack<int> que; 4 stack<int> temp; 5 /** Initialize your data structur 阅读全文
posted @ 2020-05-09 23:06
梦醒潇湘
阅读(164)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) { 阅读全文
posted @ 2020-05-09 22:58
梦醒潇湘
阅读(252)
评论(0)
推荐(0)
摘要:
题目: 解答: 常量空间的话,第一可以考虑是不是固定数量的几个变量能搞定;否则可以考虑是不是问题本身已经提供了足够的空间。 这道题目属于后者,就是利用矩阵的第一行和第一列来作为辅助空间使用。不用开辟新的存储空间。方法就是: A. 先确定第一行和第一列是否需要清零; B. 扫描剩下的矩阵元素,如果遇到 阅读全文
posted @ 2020-05-09 22:51
梦醒潇湘
阅读(184)
评论(0)
推荐(0)
摘要:
题目: 解答: 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 阅读全文
posted @ 2020-05-09 22:45
梦醒潇湘
阅读(178)
评论(0)
推荐(0)
摘要:
题目: 解答: 思路:滑动窗口。 我们可以使用哈希表记录每个字符的下一个索引,然后尽量向右移动尾指针来拓展窗口,并更新窗口的最大长度。如果尾指针指向的元素重复,则将头指针直接移动到窗口中重复元素的右侧。 1 class Solution { 2 public: 3 int lengthOfLonge 阅读全文
posted @ 2020-05-09 22:40
梦醒潇湘
阅读(171)
评论(0)
推荐(0)
摘要:
题目: 解法: /* * 位运算 * * 因为不能使用加减乘除四则运算,所以只能想到使用二进制的位运算实现相加操作。 * 二进制位运算中,异或操作: 1^1=0 0^0=0 1^0=1 0^1=1,可以模拟无进位的加操作; * 与操作:1&1=1 0&1=0 1&0=0 0&0=0,可以模拟进位的情 阅读全文
posted @ 2020-05-09 22:33
梦醒潇湘
阅读(207)
评论(0)
推荐(0)
摘要:
题目: 解答: 思路描述: 计算除 A[i] 以外所有元素的乘积,如果我们使用两层遍历就会重复计算很多次乘法, 其实就相当于求 A[i] 左边所有元素的乘积之和和右边所有元素的乘积 如果我们每次只考虑一边的乘积的话,比如left[i]代表i左侧的乘积,那么left[i + 1] = A[i] * l 阅读全文
posted @ 2020-05-09 22:24
梦醒潇湘
阅读(152)
评论(0)
推荐(0)
摘要:
题目: 解答: 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 阅读全文
posted @ 2020-05-09 20:14
梦醒潇湘
阅读(134)
评论(0)
推荐(0)
摘要:
题目: 解答: 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 }; 阅读全文
posted @ 2020-05-09 20:11
梦醒潇湘
阅读(151)
评论(0)
推荐(0)
摘要:
题目: 解答: 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 阅读全文
posted @ 2020-05-09 20:00
梦醒潇湘
阅读(119)
评论(0)
推荐(0)
摘要:
题目: 解答: 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 阅读全文
posted @ 2020-05-09 19:56
梦醒潇湘
阅读(156)
评论(0)
推荐(0)
摘要:
题目: 解答: 思路描述: 这个就可以先排一下序,然后计算出来 0 的个数 num1,以及需要多少张牌才能够连续,比如: 1 2 4 需要一张牌才能连续,如果数目不大于 num1,那么就是顺子。其中注意出现相同的牌也不是顺子! 1 class Solution { 2 public: 3 bool 阅读全文
posted @ 2020-05-09 19:53
梦醒潇湘
阅读(190)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class Solution { 2 public: 3 string reverseLeftWords(string s, int n) 4 { 5 reversestr(s, 0, n); 6 reversestr(s, n, s.size()); 7 reversestr( 阅读全文
posted @ 2020-05-09 19:49
梦醒潇湘
阅读(127)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class MaxQueue { 2 queue<int> q; 3 deque<int> d; 4 public: 5 MaxQueue() { 6 } 7 8 int max_value() 9 { 10 if (d.empty()) 11 return -1; 12 ret 阅读全文
posted @ 2020-05-09 19:45
梦醒潇湘
阅读(142)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class Solution { 2 public: 3 vector<int> maxSlidingWindow(vector<int>& nums, int k) 4 { 5 if(nums.size() == 0 || k == 1) 6 { 7 return nums; 阅读全文
posted @ 2020-05-09 19:42
梦醒潇湘
阅读(150)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class Solution { 2 public: 3 int reverse_string(string& s, int start, int end) 4 { 5 for (int i = start; i <= (start + end) / 2; i++) 6 { 7 阅读全文
posted @ 2020-05-09 19:36
梦醒潇湘
阅读(149)
评论(0)
推荐(0)
摘要:
题目: 解答: 方法一: 两次反转,先反转每个单词,再反转每个句子。 方法二: 1 class Solution { 2 public: 3 string reverseWords(string s) 4 { 5 if(s.empty()) 6 { 7 return s; 8 } 9 10 int 阅读全文
posted @ 2020-05-09 19:35
梦醒潇湘
阅读(174)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class Solution { 2 public: 3 vector<vector<int>> findContinuousSequence(int target) 4 { 5 int i = 1; // 滑动窗口的左边界 6 int j = 1; // 滑动窗口的右边界 7 阅读全文
posted @ 2020-05-09 19:31
梦醒潇湘
阅读(149)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class Solution { 2 public: 3 vector<int> twoSum(vector<int>& nums, int target) 4 { 5 // vector<int> res; 6 if (nums.size() <= 1) 7 { 8 retur 阅读全文
posted @ 2020-05-09 19:28
梦醒潇湘
阅读(135)
评论(0)
推荐(0)
摘要:
题目: 解答: 方法一:哈希 class Solution { public: int singleNumber(std::vector<int> &nums) { if (nums.empty()) { return 0; } // 哈希表存储数和其出现次数 std::unordered_map< 阅读全文
posted @ 2020-05-09 19:24
梦醒潇湘
阅读(205)
评论(0)
推荐(0)
摘要:
题目: 解答: 方法一:哈希 1 class Solution { 2 public: 3 vector<int> singleNumber(vector<int>& nums) { 4 map<int, int> count; 5 for (int n : nums) count[n] ++; 6 阅读全文
posted @ 2020-05-09 19:19
梦醒潇湘
阅读(167)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : 阅读全文
posted @ 2020-05-09 19:08
梦醒潇湘
阅读(172)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : 阅读全文
posted @ 2020-05-09 19:05
梦醒潇湘
阅读(123)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : 阅读全文
posted @ 2020-05-09 19:02
梦醒潇湘
阅读(153)
评论(0)
推荐(0)
摘要:
题目: 解答: 令0~n的数与nums中的数异或,运算中除了缺失值只出现一次外,其他数都出现两次等同于与自身异或。 1 class Solution { 2 public: 3 int missingNumber(vector<int>& nums) 4 { 5 int n = nums.size( 阅读全文
posted @ 2020-05-09 18:59
梦醒潇湘
阅读(119)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class Solution { 2 public: 3 int search(vector<int>& nums, int target) 4 { 5 int left = left_bound(nums, target); 6 int right = right_bound( 阅读全文
posted @ 2020-05-09 18:56
梦醒潇湘
阅读(204)
评论(0)
推荐(0)
摘要:
题目: 解答: 方法一:双指针法 (1)创建两个指针 pA 和 pB,分别初始化为链表 A 和 B 的头结点。然后让它们向后逐结点遍历。 (2)当 pA到达链表的尾部时,将它重定位到链表 B 的头结点 (你没看错,就是链表 B); 类似的,当 pB 到达链表的尾部时,将它重定位到链表 A 的头结点。 阅读全文
posted @ 2020-05-09 18:53
梦醒潇湘
阅读(789)
评论(0)
推荐(0)
摘要:
题目: 解答: 方法一:优先级队列 1 class Solution { 2 public int nthUglyNumber(int n) { 3 PriorityQueue<Long> pq = new PriorityQueue<>(); 4 Set<Long> s = new HashSet 阅读全文
posted @ 2020-05-09 18:45
梦醒潇湘
阅读(180)
评论(0)
推荐(0)
摘要:
题目: 解答: 方法一:哈希,遍历。 1 class Solution { 2 public: 3 char firstUniqChar(string s) 4 { 5 if (s == "") 6 { 7 return ' '; 8 } 9 map<char, int> str; 10 11 fo 阅读全文
posted @ 2020-05-09 16:50
梦醒潇湘
阅读(148)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class Solution { 2 public: 3 int maxSubArray(vector<int>& nums) 4 { 5 int sum = nums[0]; 6 int b = 0; 7 8 for (int i = 0; i < nums.size(); i 阅读全文
posted @ 2020-05-09 16:39
梦醒潇湘
阅读(102)
评论(0)
推荐(0)
摘要:
题目: 解答: 方法一:C++超时 1 class MedianFinder { 2 vector<double> store; 3 4 public: 5 // Adds a number into the data structure. 6 void addNum(int num) 7 { 8 阅读全文
posted @ 2020-05-09 16:35
梦醒潇湘
阅读(182)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class Solution { 2 public: 3 vector<int> getLeastNumbers(vector<int>& arr, int k) 4 { 5 vector<int> res; 6 priority_queue<int> q; 7 for (int 阅读全文
posted @ 2020-05-09 16:26
梦醒潇湘
阅读(154)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class Solution { 2 public: 3 int majorityElement(vector<int>& nums) 4 { 5 int x = 0; 6 int votes = 0; 7 for(int num : nums) 8 { 9 if(votes = 阅读全文
posted @ 2020-05-09 16:23
梦醒潇湘
阅读(129)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class Solution { 2 public: 3 vector<string> permutation(string str) 4 { 5 vector<string> result; 6 if(str.empty()) 7 { 8 return result; 9 } 阅读全文
posted @ 2020-05-09 16:18
梦醒潇湘
阅读(177)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : 阅读全文
posted @ 2020-05-09 16:11
梦醒潇湘
阅读(101)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 // 中序遍历即可。只需要记录一个pre指针即可。 2 3 4 class Solution { 5 public: 6 TreeNode* Convert(TreeNode* pRootOfTree) 7 { 8 if(pRootOfTree == nullptr) 9 { 1 阅读全文
posted @ 2020-05-09 16:05
梦醒潇湘
阅读(137)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : 阅读全文
posted @ 2020-05-09 16:01
梦醒潇湘
阅读(224)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : 阅读全文
posted @ 2020-05-09 15:59
梦醒潇湘
阅读(148)
评论(0)
推荐(0)
摘要:
题目: 解答: BST的后序序列的合法序列是,对于一个序列S,最后一个元素是x (也就是根),如果去掉最后一个元素的序列为T,那么T满足:T可以分成两段,前一段(左子树)小于x,后一段(右子树)大于x,且这两段(子树)都是合法的后序序列。 1 class Solution { 2 public: 3 阅读全文
posted @ 2020-05-09 15:54
梦醒潇湘
阅读(170)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : 阅读全文
posted @ 2020-05-09 15:41
梦醒潇湘
阅读(138)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : 阅读全文
posted @ 2020-05-09 15:39
梦醒潇湘
阅读(113)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : 阅读全文
posted @ 2020-05-09 15:36
梦醒潇湘
阅读(150)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class Solution { 2 public: 3 bool validateStackSequences(vector<int>& pushed, vector<int>& popped) 4 { 5 stack<int> st; 6 int n = popped.siz 阅读全文
posted @ 2020-05-09 15:30
梦醒潇湘
阅读(148)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class MinStack { 2 stack<int> s; 3 stack<int> s2; 4 public: 5 /** initialize your data structure here. */ 6 MinStack() { 7 8 } 9 10 void pus 阅读全文
posted @ 2020-05-09 15:13
梦醒潇湘
阅读(130)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class Solution { 2 public: 3 vector<int> spiralOrder(vector<vector<int>>& matrix) 4 { 5 vector<int> result; 6 if (matrix.empty()) 7 { 8 retu 阅读全文
posted @ 2020-05-09 15:02
梦醒潇湘
阅读(139)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : 阅读全文
posted @ 2020-05-09 14:56
梦醒潇湘
阅读(181)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : 阅读全文
posted @ 2020-05-09 14:52
梦醒潇湘
阅读(163)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 1 class Solution { 2 2 bool isSubtree(TreeNode* pRootA, TreeNode* pRootB) 3 3 { 4 4 if (pRootB == NULL) 5 5 { 6 6 return true; 7 7 } 8 8 if 阅读全文
posted @ 2020-05-09 14:49
梦醒潇湘
阅读(137)
评论(0)
推荐(0)
摘要:
题目: 解答: 方法一:递归。 1 /* 2 struct ListNode { 3 int val; 4 struct ListNode *next; 5 ListNode(int x) : 6 val(x), next(NULL) { 7 } 8 };*/ 9 class Solution { 阅读全文
posted @ 2020-05-09 14:32
梦醒潇湘
阅读(120)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) { 阅读全文
posted @ 2020-05-09 14:25
梦醒潇湘
阅读(105)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) { 阅读全文
posted @ 2020-05-09 14:21
梦醒潇湘
阅读(208)
评论(0)
推荐(0)
摘要:
题目: 解答: 方法一: 1 class Solution { 2 public: 3 vector<int> exchange(vector<int>& nums) 4 { 5 int left = 0; 6 int right = nums.size() - 1; 7 while (left < 阅读全文
posted @ 2020-05-09 14:18
梦醒潇湘
阅读(130)
评论(0)
推荐(0)
摘要:
题目: 解答: 1)先去除字符串首尾的空格2)然后根据e划分指数和底数3)判断指数和底数是否合法即可 1 class Solution { 2 public: 3 bool isNumber(string s) { 4 //1、从首尾寻找s中不为空格首尾位置,也就是去除首尾空格 5 int i=s. 阅读全文
posted @ 2020-05-09 14:09
梦醒潇湘
阅读(136)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class Solution { 2 public: 3 bool isMatch(string s, string p) 4 { 5 s=" "+s;//防止该案例:""\n"c*" 6 p=" "+p; 7 8 int m = s.size(); 9 int n = p.si 阅读全文
posted @ 2020-05-09 14:06
梦醒潇湘
阅读(203)
评论(0)
推荐(0)
摘要:
题目: 解答: 快速幂。 1 class Solution { 2 public: 3 double myPow(double x, int n) 4 { 5 double res=1.0; 6 int i=n; 7 while(i) 8 { 9 if(i&1) 10 { 11 res*=x; // 阅读全文
posted @ 2020-05-09 14:00
梦醒潇湘
阅读(96)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class Solution { 2 public: 3 int hammingWeight(uint32_t n) 4 { 5 int res = 0; 6 while(n != 0) 7 { 8 res += n & 1; 9 n >>= 1; 10 } 11 return 阅读全文
posted @ 2020-05-09 13:56
梦醒潇湘
阅读(105)
评论(0)
推荐(0)
摘要:
题目: 解答: 阅读全文
posted @ 2020-05-09 13:54
梦醒潇湘
阅读(111)
评论(0)
推荐(0)
摘要:
题目: 解答: 回溯法。 (1)在board中找到一个位置,使得board[i][j] == word[0],可以作为搜索的入口; (2)由于一个格子不能重复进入,因此需要定义一个visit数组,保证每个格子只进入一次; (3)找到一个可行解即返回true。若该次搜索返回false,那么进行步骤1. 阅读全文
posted @ 2020-05-09 13:46
梦醒潇湘
阅读(165)
评论(0)
推荐(0)
摘要:
题目: 解答: (1)如下图所示,寻找旋转数组的最小元素即为寻找 右排序数组 的首个元素 numbers[x] ,称 x 为 旋转点 。 (2)排序数组的查找问题首先考虑使用 二分法 解决,其可将遍历法的 线性级别 时间复杂度降低至 对数级别 。 算法流程: (1)循环二分:设置 i, j 指针分别 阅读全文
posted @ 2020-05-09 13:41
梦醒潇湘
阅读(134)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class Solution { 2 public: 3 int numWays(int n) 4 { 5 vector<int> v(n + 1, 1); 6 for(int i = 2; i <= n; i++) 7 { 8 v[i] = (v[i - 1] + v[i - 阅读全文
posted @ 2020-05-09 13:17
梦醒潇湘
阅读(179)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class Solution { 2 public: 3 int fib(int n) 4 { 5 vector<int> v(100 + 1); 6 v.at(0) = 0; 7 v.at(1) = 1; 8 9 for (int i = 2; i <= n; ++i) 10 阅读全文
posted @ 2020-05-09 13:05
梦醒潇湘
阅读(130)
评论(0)
推荐(0)
摘要:
题目: 解答: 只使用一个栈 stack1 当作队列,另一个栈 stack2 用来辅助操作。 要想将新加入的元素出现栈底,需要先将 stack1 的元素转移到 stack2,将元素入栈 stack1,最后将 stack2 的元素全部回到 stack1。 1 class CQueue { 2 publ 阅读全文
posted @ 2020-05-09 12:58
梦醒潇湘
阅读(92)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int 阅读全文
posted @ 2020-05-09 12:50
梦醒潇湘
阅读(394)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : 阅读全文
posted @ 2020-05-09 12:49
梦醒潇湘
阅读(194)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : 阅读全文
posted @ 2020-05-09 12:47
梦醒潇湘
阅读(206)
评论(0)
推荐(0)
摘要:
题目: 解答: 方法一:栈保存,先进后出。 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x) 阅读全文
posted @ 2020-05-09 12:28
梦醒潇湘
阅读(159)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class Solution { 2 public: 3 string replaceSpace(string s) 4 { 5 string res; 6 for(int i = 0; i < s.size(); ++i) 7 { 8 if(s[i] == ' ') 9 { 1 阅读全文
posted @ 2020-05-09 11:57
梦醒潇湘
阅读(124)
评论(0)
推荐(0)
摘要:
题目: 解答: 1 class Solution { 2 public: 3 bool findNumberIn2DArray(vector<vector<int>>& matrix, int target) 4 { 5 int i=matrix.size()-1; 6 int j=0; 7 // 阅读全文
posted @ 2020-05-09 11:53
梦醒潇湘
阅读(154)
评论(0)
推荐(0)
摘要:
题目: 解答: 方法一:哈希 使用哈希来进行处理,当发现哈希中包含相应的元素时,则表示出现了重复的元素,则返回即可。 1 class Solution { 2 public: 3 int findRepeatNumber(vector<int>& nums) 4 { 5 std::set<int> 阅读全文
posted @ 2020-05-09 11:44
梦醒潇湘
阅读(141)
评论(0)
推荐(0)

浙公网安备 33010602011771号