Skip to content

Commit 35743e7

Browse files
committed
7218
1 parent 1b19d34 commit 35743e7

5 files changed

+149
-0
lines changed

C++/142. Linked List Cycle II.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//142. Linked List Cycle II
2+
3+
/**
4+
* Definition for singly-linked list.
5+
* struct ListNode {
6+
* int val;
7+
* ListNode *next;
8+
* ListNode(int x) : val(x), next(NULL) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode *detectCycle(ListNode *head) {
14+
ListNode* fast = head;
15+
ListNode* slow = head;
16+
ListNode* entry = head;
17+
while(slow && fast && fast->next){
18+
slow = slow->next;
19+
fast = fast->next->next;
20+
if(slow == fast){
21+
while(slow!=entry){
22+
slow = slow->next;
23+
entry = entry->next;
24+
}
25+
return entry;
26+
}
27+
}
28+
return NULL;
29+
30+
}
31+
};

C++/234. Palindrome Linked List.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
//234. Palindrome Linked List
3+
4+
/**
5+
* Definition for singly-linked list.
6+
* struct ListNode {
7+
* int val;
8+
* ListNode *next;
9+
* ListNode(int x) : val(x), next(NULL) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
bool isPalindrome(ListNode* head) {
15+
if(head == NULL || head->next == NULL) return true;
16+
return find(head,head->next)!=NULL?true:false;
17+
}
18+
ListNode* find(ListNode* head,ListNode* nex) {
19+
20+
//If only one element
21+
if(nex==NULL) return head;
22+
23+
//If you reach the last element
24+
if(nex->next==NULL)
25+
return head->val==nex->val ? head->next : NULL;
26+
27+
//Recursively call till you reach last element.
28+
//As soon as you reach the last element, Just return the next of the head
29+
//So that previous calls can use that refernce and compare with the second last and so on
30+
31+
ListNode* checkhead = find(head,nex->next);
32+
33+
if(checkhead==NULL) return NULL;
34+
35+
return checkhead->val==nex->val?checkhead->next:NULL;
36+
37+
}
38+
39+
};

C++/261. Graph Valid Tree.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//261. Graph Valid Tree
2+
3+
class Solution {
4+
public:
5+
bool validTree(int n, vector<pair<int, int>>& edges) {
6+
vector<int> nodes(n,0);
7+
for(int i=0;i<nodes.size();++i){
8+
nodes[i] = i;
9+
}
10+
for(int i=0;i<edges.size();++i){
11+
int root1 = edges[i].first;
12+
int root2 = edges[i].second;
13+
while(nodes[root1]!=root1)
14+
root1 = nodes[root1];
15+
while(nodes[root2]!=root2)
16+
root2 = nodes[root2];
17+
if(root1==root2)
18+
return false;
19+
nodes[root2] = root1;
20+
}
21+
return edges.size() == n-1;
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
674. Longest Continuous Increasing Subsequence
3+
4+
if len(nums) < 1:
5+
return 0
6+
cur_len = 1
7+
max_len = 1
8+
for i in range(1,len(nums)):
9+
if nums[i] > nums[i-1]:
10+
cur_len = cur_len + 1
11+
else:
12+
cur_len = 1
13+
14+
if cur_len > max_len:
15+
max_len = cur_len
16+
return max_len
17+
18+
*/
19+
static int x = []() { std::ios::sync_with_stdio(false); cin.tie(NULL); return 0; }();
20+
21+
class Solution {
22+
public:
23+
int findLengthOfLCIS(vector<int>& nums) {
24+
if(nums.size()<1){
25+
return 0;
26+
}
27+
int current = 1;
28+
int max = 1;
29+
for(int i=1;i<nums.size();++i){
30+
current = (nums[i]>nums[i-1])?current+1:1;
31+
max = (current>max)?current:max;
32+
}
33+
return max;
34+
}
35+
};
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#209. Minimum Size Subarray Sum
2+
# same two pointer idea as mentioned in solution
3+
# main idea is to precompute sums and then change
4+
# it on the go as per required.
5+
class Solution:
6+
def minSubArrayLen(self, s, nums):
7+
n = len(nums)
8+
ans = float('inf')
9+
left = 0
10+
presum = 0
11+
for i in range(n):
12+
presum += nums[i]
13+
while (presum>=s):
14+
ans = min(ans,i-left+1)
15+
presum -= nums[left]
16+
left += 1
17+
if ans == float('inf'):
18+
return 0
19+
else:
20+
return ans
21+

0 commit comments

Comments
 (0)