#20 Valid Parentheses
#21 Merge Two Sorted Lists
#26 Remove Duplicates from Sorted Array
#27 Remove Element
#28 Implement strStr()
#20 Valid Parentheses
要求:给定括号字符串,判断是否有效括号串
思路1:遍历字符串,栈结构,压栈时,判断与栈顶的关系;匹配则弹出。不匹配继续入栈。最后判断栈是否为空。一次通过。
class Solution {
public:
bool isValid(string s) {
if(s.size()==0) return false;
stack<char> stk;
for(int i = 0; i<s.size();i++)
{
if(stk.empty())
stk.push(s[i]);
else
{
switch(s[i])
{
case '(':
case '{':
case '[':
stk.push(s[i]);break;
case ')':
if(stk.top() == '(')
stk.pop();
else
stk.push(s[i]);
break;
case '}':
if(stk.top() == '{')
stk.pop();
else
stk.push(s[i]);
break;
case ']':
if(stk.top() == '[')
stk.pop();
else
stk.push(s[i]);
break;
}
}
}
return stk.empty() ? true : false;
}
};
参考大神博客园:Grandyang
思路2:总体想法一样,但是大神的解答效率稍微高一点。
class Solution {
public:
bool isValid(string s) {
stack<char>stk;
for(int i = 0; i<s.size();i++)
{
if(stk.empty() || s[i]== '('||s[i]=='{'||s[i]=='[') stk.push(s[i]);
else if(s[i]==')'&&stk.top()!='(') return false;
else if(s[i]==']'&&stk.top()!='[') return false;
else if(s[i]=='}'&&stk.top()!='{') return false;
else stk.pop();
}
return stk.empty();
}
};
#21 Merge Two Sorted Lists
要求:合并2个排序列表
思路1:递归法,浅显易懂。不做解释
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(!l1) return l2;
if(!l2) return l1;
ListNode * pMergeList = NULL;
if(l1->val < l2->val)
{
pMergeList = l1;
pMergeList->next = mergeTwoLists(l1->next,l2);
}
else
{
pMergeList = l2;
pMergeList->next = mergeTwoLists(l1,l2->next);
}
return pMergeList;
}
};
思路2:循环做法,代码较多。
首先找到链表头,然后逐个对比那个链表的值小,就先进入合并链表中。最后再将剩下的链表链接到合并链表上。
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(!l1) return l2;
if(!l2) return l1;
ListNode * pMergeList = nullptr;
if(l1->val < l2->val)
{
pMergeList = l1;
l1 = l1->next;
pMergeList -> next = nullptr;
}
else
{
pMergeList = l2;
l2 = l2->next;
pMergeList -> next = nullptr;
}
ListNode * pTemp = pMergeList;
while(l1!=nullptr && l2!=nullptr)
{
if(l1->val < l2->val)
{
pTemp->next = l1;
l1 = l1->next;
}
else
{
pTemp->next = l2;
l2 = l2->next;
}
pTemp = pTemp->next;
pTemp->next = nullptr;
}
if(l1==nullptr) pTemp->next = l2;
else if(l2==nullptr) pTemp->next = l1;
return pMergeList;
}
};
参考大神博客园:Grandyang
思路3:这种方法,打死我都想不出来。缺点是,会改变输入,优点是:只需要3行,佩服。
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (!l1 || (l2 && l1->val > l2->val)) swap(l1, l2);
if (l1) l1->next = mergeTwoLists(l1->next, l2);
return l1;
}
};
#26 Remove Duplicates from Sorted Array
要求:给定非递减数组,去重,返回去重后的数字个数
思路1:明显不是最优解,但聊胜于无,变量使用太多,考虑优化。该做法不符合题目要求(开辟空间了)
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.size() == 0) return 0;
int result = 1;
int currentValue = nums[0];
vector<int> vec;
vec.push_back(nums[0]);
for(int i = 1; i<nums.size(); i++)
{
if(currentValue != nums[i])
{
result++;
currentValue = nums[i];
vec.push_back(currentValue);
}
}
nums=vec;
return result;
}
};
参考大神博客园:Grandyang
思路2:快慢指针法;起始:指向同一个值,如果相同,快指针向前。如果不同,赋值,两个指针都向前
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
if(nums.empty()) return 0;
int slow = 0, fast = 0;
while(fast < nums.size())
{
if(nums[slow] == nums[fast])
fast++;
else
nums[++slow] = nums[fast++];
}
return slow+1;
}
};
思路3:很难想到;只要当前数字比上一个大,那就没有重复了。只能说很秀,我服
class Solution {
public:
int removeDuplicates(vector<int>& nums) {
int i = 0;
for(int num:nums)
{
if(i<1 || num > nums[i-1])
nums[i++]=num;
}
return i;
}
};
#27 Remove Element
要求:给定数组,某数字;删除数组中的该数字,返回剩余个数,不可多开辟空间
思路1:排序一下,再删除,但是这个想法不太合适,就不写代码了,其他没思路
参考大神博客园:Grandyang
一个变量计数,如果不同,就将值赋进来
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int res = 0;
for(int i = 0; i<nums.size(); i++)
{
if(val != nums[i]) nums[res++] = nums[i];
}
return res;
}
};
#28 Implement strStr()
要求:给定2个字符串,返回串2在串1的索引,否则根据情况返回0,或者-1
思路1:没做出来。标记一下,下次重做。
参考大神博客园:Grandyang
外层遍历父串,内层每次都从头开始遍历子串。逐个判断字符是否相同,最后判断子串是否走到最后。否则返回-1.
class Solution {
public:
int strStr(string haystack, string needle) {
if(needle.size() == 0) return 0;
if(needle.size() > haystack.size()) return -1;
for(int i = 0; i < (haystack.size()-needle.size()+1); i++)
{
int j = 0;
for(j = 0; j<needle.size(); j++)
{
if(needle[j] != haystack[j+i])
break;
}
if(j == needle.size())
return i;
}
return -1;
}
};
思路2:依然是2层循环,不写终止条件,循环体中判断终止条件。效率没有思路1好。
class Solution {
public:
int strStr(string haystack, string needle) {
for(int i = 0; ;i++)
for(int j = 0; ; j++)
{
if(j == needle.size()) return i;
if(i+j == haystack.size()) return -1;
if(needle[j]!=haystack[i+j]) break;
}
return -1;
}
};
菜鸡一枚,欢迎大家批评指正,互相交流啊~