Skip to content

Commit 59e7261

Browse files
committed
🚀 04-Aug-2020
1 parent 905a1e2 commit 59e7261

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
2+
3+
Note: For the purpose of this problem, we define empty string as valid palindrome.
4+
5+
Example 1:
6+
7+
Input: "A man, a plan, a canal: Panama"
8+
Output: true
9+
Example 2:
10+
11+
Input: "race a car"
12+
Output: false
13+
14+
15+
Constraints:
16+
17+
s consists only of printable ASCII characters.
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
class Solution {
32+
public:
33+
34+
bool isPalindrome(string s) {
35+
int n=s.length();
36+
int i=0, j=n-1;
37+
while(i<j){
38+
if(!isalnum(s[i])){ i++; continue; }
39+
if(!isalnum(s[j])){ j--; continue; }
40+
if(tolower(s[i])!=tolower(s[j])) return false;
41+
i++;
42+
j--;
43+
}
44+
return true;
45+
}
46+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
2+
3+
Note: For the purpose of this problem, we define empty string as valid palindrome.
4+
5+
Example 1:
6+
7+
Input: "A man, a plan, a canal: Panama"
8+
Output: true
9+
Example 2:
10+
11+
Input: "race a car"
12+
Output: false
13+
14+
15+
Constraints:
16+
17+
s consists only of printable ASCII characters.
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
class Solution {
32+
public:
33+
34+
bool isPalindrome(string s) {
35+
int n=s.length();
36+
int i=0, j=n-1;
37+
while(i<j){
38+
if(!isalnum(s[i])){ i++; continue; }
39+
if(!isalnum(s[j])){ j--; continue; }
40+
if(tolower(s[i])!=tolower(s[j])) return false;
41+
i++;
42+
j--;
43+
}
44+
return true;
45+
}
46+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.
2+
3+
Example 1:
4+
5+
Input: [3,0,1]
6+
Output: 2
7+
Example 2:
8+
9+
Input: [9,6,4,2,3,5,7,0,1]
10+
Output: 8
11+
Note:
12+
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
13+
14+
15+
16+
17+
18+
19+
20+
21+
class Solution {
22+
public:
23+
int missingNumber(vector<int>& nums) {
24+
int res=0, n=nums.size();
25+
for(int i=1;i<=n;i++) res^=i;
26+
for(int x: nums)
27+
res^=x;
28+
return res;
29+
}
30+
};

competitive programming/leetcode/287. Find the Duplicate Number.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,39 @@ class Solution {
6262
};
6363

6464

65+
66+
67+
68+
69+
70+
71+
72+
// Slow and Fast pointer technique (will also work for negatives)
73+
// D = distance from start till starting of loop
74+
// K = distance start of loop till both pointers meet
75+
// C = length of loop
76+
// i, j distance covered in loops by fast and slow pointer
77+
// distance covered by slow, N = D + K + C * i
78+
// distance covered by fast, 2N = D + K + C * j
79+
// subtracting we get D = C(j - 2*i ) - K
80+
// Place slow to start again, it covers D distance to reach start of loop
81+
// Lets say fast makes x loops and still remains on the colliding position and now
82+
// if he has to reach the start of the loop he hast to go K steps backward
83+
84+
class Solution {
85+
public:
86+
int findDuplicate(vector<int>& nums) {
87+
if(nums.size()<=0) return -1;
88+
int slow=nums[0], fast=nums[0];
89+
do{
90+
slow=nums[slow];
91+
fast=nums[nums[fast]];
92+
}while(slow!=fast);
93+
fast=nums[0];
94+
while(slow!=fast){
95+
slow=nums[slow];
96+
fast=nums[fast];
97+
}
98+
return slow;
99+
}
100+
};

competitive programming/leetcode/75. Sort Colors.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,27 @@ class Solution {
3535
for(int i=zeros+ones;i<zeros+ones+twos;i++) nums[i]=2;
3636
}
3737
};
38+
39+
40+
41+
42+
43+
// Dutch national Flag Algorithm
44+
45+
class Solution {
46+
public:
47+
void sortColors(vector<int>& nums) {
48+
int n=nums.size();
49+
int low=0, mid=0, high=n-1;
50+
while(mid<=high){
51+
if(nums[mid]==0){ // Step 1
52+
swap(nums[low], nums[mid]);
53+
low++; mid++;
54+
} else if(nums[mid]==1) mid++; // Step 2
55+
else {
56+
swap(nums[mid], nums[high]); // Step 3
57+
high--;
58+
}
59+
}
60+
}
61+
};

0 commit comments

Comments
 (0)