Dear friends, it's another new day, how are you doing? Today is the second article of my leedcode brushing series, so without further ado, let's go directly to our topic today.
Article directory
valid brackets
Effective parentheses in leetcode
Topic requirements
Given a string s consisting only of '(', ')', '{', '}', '[', ']', determine whether the string is valid.
A valid string must satisfy:
An opening parenthesis must be closed with a closing parenthesis of the same type.
Opening parentheses must be closed in the correct order.
Each closing parenthesis has a corresponding opening parenthesis of the same type.
use case input
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
Question ideas
The requirement of this question is that we need to judge whether the given parentheses are legal, which means that when we encounter parentheses, we need to judge whether the nearest left parenthesis on the left matches the right parenthesis. We can use a data structure: stack to solve this problem, because the stack is one end in, one end out, this end is called the top of the stack, first in first out, last in first out. So we put all the left parentheses on the stack. When we encounter a right parenthesis, we take the left parenthesis from the top of the stack to see if it matches the right parenthesis. When the string is traversed, if the stack is not empty, it means that there is an unmatched left parenthesis, return false, otherwise return true.
Code
bool isValid(char * s){
int len = strlen(s);
//当字符串中没有或者只有一个字符时就直接返回false
if(len <= 1)
return false;
//字符个数为奇数就说明一定有一个未匹配所以就直接返回
if(len%2 == 1)
return false;
//tail记录栈顶的位置
int tail = 0;
char* arr = (char*)malloc(len*sizeof(char));
int i = 0;
for(int i = 0; i<len; i++)
{
if(s[i] == '(' || s[i] == '[' || s[i] == '{')
{
arr[tail++] = s[i];
}
else
{
if(tail == 0)
return false;
if(s[i] == ')')
{
if(arr[tail-1] != '(')
return false;
}
else if(s[i] == ']')
{
if(arr[tail-1] != '[')
{
return false;
}
}
else
{
if(arr[tail-1] != '{')
{
return false;
}
}
tail--;
}
}
if(tail == 0)
return true;
return false;
}
circular linked list
Topic requirements
Give you the head node head of a linked list, and judge whether there is a ring in the linked list.
If there is a node in the linked list that can be reached again by continuously tracking the next pointer, then there is a cycle in the linked list. In order to represent the ring in the given linked list, the evaluation system internally uses the integer pos to indicate the position where the end of the linked list is connected to the linked list (the index starts from 0). Note: pos is not passed as a parameter. Just to identify the actual situation of the linked list.
Returns true if there is a cycle in the linked list. Otherwise, returns false.
use case input
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a ring in the linked list whose tail is connected to the second node.
Example 2:
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a ring in the linked list whose tail is connected to the first node.
Example 3:
Input: head = [1], pos = -1
Output: false
Explanation: There is no ring in the linked list.
Question ideas
If the linked list has a ring, then we use two pointers: the fast pointer and the slow pointer. The slow pointer walks one node at a time, and the fast pointer walks two nodes. They will eventually meet.
Code
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
bool hasCycle(struct ListNode *head) {
struct ListNode* low = head;
struct ListNode* fast = head;
while(fast && fast->next)
{
low = low->next;
fast = fast->next->next;
if(low == fast)
return true;
}
return false;
}
Ring List II
leetcode circular linked list||
Topic requirements
Given the head node head of a linked list, return the first node of the linked list starting to enter the ring. Returns null if the linked list is acyclic.
If there is a node in the linked list that can be reached again by continuously tracking the next pointer, then there is a cycle in the linked list. In order to represent the ring in the given linked list, the evaluation system internally uses the integer pos to indicate the position where the end of the linked list is connected to the linked list (the index starts from 0). If pos is -1, there are no cycles in the list. Note: pos is not passed as a parameter, it is just to identify the actual situation of the linked list.
The linked list is not allowed to be modified.
use case input
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: Return the linked list node with index 1
Explanation: There is a ring in the linked list whose tail is connected to the second node.
Example 2:
Input: head = [1,2], pos = 0
Output: Return the linked list node with index 0
Explanation: There is a ring in the linked list, whose tail is connected to the first node.
Example 3:
Input: head = [1], pos = -1
Output: return null
Explanation: There is no ring in the linked list.
Question ideas
Our question still needs the knowledge of whether the above judgment is a circular linked list. There is a conclusion: when the fast and slow pointers meet, let the pointers start walking at the head node of the linked list and the node they meet, one node at a time , they will eventually meet at the entrance of the circular linked list.
Code
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode* slow = head;
struct ListNode* fast = head;
while(fast && fast->next)
{
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
{
struct ListNode* meet = slow;
while(head != meet)
{
meet = meet->next;
head = head->next;
}
return meet;
}
}
return NULL;
}