【LeetCode018-020】4Sum,单链表Nth去除,符号检测//指针与引用,stack用法

18. 4Sum

 
  • Total Accepted: 100564
  • Total Submissions: 391517
  • Difficulty: Medium
  • Contributors: Admin

Given an array S of n integers, are there elements abc, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note: The solution set must not contain duplicate quadruplets.

For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.

A solution set is:
[
  [-1,  0, 0, 1],
  [-2, -1, 1, 2],
  [-2,  0, 0, 2]
]





class Solution {
public:
	vector<vector<int>> fourSum(vector<int>& nums, int target) {
		vector<vector<int>> result;
		vector<int> temp;
		if(nums.size()<4)return result;
		sort(nums.begin(), nums.end());//#include<algorithm> 
		for (int i = 0; i<nums.size(); i++) {//四个的剔除掉
		temp.push_back(nums[i]);
		}
//		temp.push_back(nums[nums.size() - 3]);
//		temp.push_back(nums[nums.size() - 2]);
//		temp.push_back(nums[nums.size() - 1]);
		for (int i = 0; i < temp.size(); i++) {

			for (int j = temp.size() - 1; j > i + 2; j--) {
			    
				int begin = i + 1, end = j - 1;
				while (begin < end) {
					if(temp[i]+temp[j]+temp[begin]+temp[end]==target){
						vector<int>M;
						M.push_back(temp[i]); M.push_back(temp[begin]); 
						M.push_back(temp[end]); M.push_back(temp[j]);
						if(result.size()==0||result[result.size()-1]!=M)
						result.push_back(M);
					    begin++; end--;

					}
					if (temp[i] + temp[j] + temp[begin] + temp[end] > target)end--;
					if (temp[i] + temp[j] + temp[begin] + temp[end] < target)begin++;

				}
                          while(nums[j]==nums[j-1])j--;
			}
					    while(nums[i]==nums[i+1])i++;
		}
		return result;
	}
};






19. Remove Nth Node From End of List

  • Total Accepted: 153567
  • Total Submissions: 477873
  • Difficulty: Easy
  • Contributors: Admin

Given a linked list, remove the nth node from the end of list and return its head.

For example,

   Given linked list: 1->2->3->4->5, and n = 2.

   After removing the second node from the end, the linked list becomes 1->2->3->5.

Note:
Given n will always be valid.
Try to do this in one pass.

class Solution {
public:
	ListNode* removeNthFromEnd(ListNode* head, int n) {
	ListNode *test;
	ListNode *result;
	test = head;
	result = head;
	int nums = 0;
	if(head->next==NULL)return NULL;

	while (test != NULL) {
		test = test->next;
		nums++;
	}

	for (int i = 0; i < nums - n-1; i++) {
		head = head->next;
	}
	if (n == nums)result = result->next;
	if(head->next->next==NULL)head->next = NULL;
	else head->next = head->next->next;
		return result;
	}
};


20. Valid Parentheses
  • Total Accepted: 161385
  • Total Submissions: 502409
  • Difficulty: Easy
  • Contributors: Admin

Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

Subscribe to see which companies asked this question


//利用char相减为1或2……,虽然好像很慢…

class Solution {
public:
    bool isValid(string s) {
        bool result=true;
        if(s.size()%2==1)return false;
        else if(s.size()==0)return true;
        else {
            int add=1;
            for(int i=1;i<s.size();i++){
                if(s[i]==s[0])add++;
                if(s[i]==s[0]+1||s[i]==s[0]+2){
                    add--;
                    if(!add){
                    result=isValid(s.substr(1,i-1))*isValid(s.substr(i+1,s.size()-i-1));
                    break;
                    }
                }
                else{
                    result=false;
                }
            }
        }
        return result;
        
    }
};

结果喜人,击败了0.8%的人,哈哈哈哈,能这么慢也是蛮好玩的


附上优雅的2ms的方法://stack的用法

class Solution {
public:
    bool isValid(string s) {
        stack<char> paren;
        for (char& c : s) {
            switch (c) {
                case '(': 
                case '{': 
                case '[': paren.push(c); break;
                case ')': if (paren.empty() || paren.top()!='(') return false; else paren.pop(); break;
                case '}': if (paren.empty() || paren.top()!='{') return false; else paren.pop(); break;
                case ']': if (paren.empty() || paren.top()!='[') return false; else paren.pop(); break;
                default: ; // pass
            }
        }
        return paren.empty() ;
    }
};

去吃饭吃饭~






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朱铭德

五毛也是爱٩(●´৺`●)૭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值