Skip to content

Commit 4bada00

Browse files
committed
Day 70 - 3 leet code problems
1 parent 0fd8062 commit 4bada00

File tree

4 files changed

+178
-3
lines changed

4 files changed

+178
-3
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 98 |
10-
| Current Streak | 69 days |
11-
| Longest Streak | 69 ( August 17, 2015 - October 24, 2015 ) |
9+
| Total Problems | 101 |
10+
| Current Streak | 70 days |
11+
| Longest Streak | 70 ( August 17, 2015 - October 25, 2015 ) |
1212

1313
</center>
1414

@@ -175,3 +175,6 @@ Include contains single header implementation of data structures and some algori
175175
|Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively.| [mergeArrays.cpp](leet_code_problems/mergeArrays.cpp)
176176
|Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example: <ul><li>A = [2,3,1,1,4], return true.</li></ul><ul><li>A = [3,2,1,0,4], return false.</li></ul>| [jumpGame.cpp](leet_code_problems/jumpGame.cpp)|
177177
|Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example 1 -> A, 2 -> B,...26 -> Z, 27 -> AA, 28 -> AB, ...705 -> AAC | [excelColSheetTitle.cpp](leet_code_problems/excelColSheetTitle.cpp)|
178+
|Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].| [moveZeroes.cpp](leet_code_problems/moveZeroes.cpp)|
179+
|Given an array of integers, find if the array contains any duplicates. Function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.| [containsDuplicate.cpp](leet_code_problems/containsDuplicate.cpp)|
180+
| Given a list, rotate the list to the right by k places, where k is non-negative. For example: <ul><li>Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL</li></ul>| [rotateList.cpp](leet_code_problems/rotateList.cpp)|
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Given an array of integers, find if the array contains any duplicates.
3+
* Your function should return true if any value appears at least twice in the array,
4+
* and it should return false if every element is distinct.
5+
*
6+
* Approach : sort and check for adjacent elements for duplicates.
7+
*
8+
*/
9+
10+
11+
#include <iostream>
12+
#include <vector>
13+
#include <algorithm>
14+
15+
16+
bool containsDuplicate(std::vector<int>& nums) {
17+
std::sort( nums.begin(), nums.end());
18+
for ( size_t i = 1; i < nums.size(); ++i ) {
19+
if ( nums[i-1] == nums[i] ) {
20+
return true;
21+
}
22+
}
23+
return false;
24+
}
25+
26+
void printVect( std::vector<int> & vec ) {
27+
std::cout << "VEC:";
28+
for ( auto & i : vec ) {
29+
std::cout << i << " ";
30+
}
31+
std::cout << std::endl;
32+
}
33+
34+
int main() {
35+
std::vector<int> vec1{ 1, 99, 99, 4, 8, 98, 96, 3, 5, 19, 23, 17, 84, 23 };
36+
printVect(vec1);
37+
if ( containsDuplicate(vec1) ) {
38+
std::cout << "Above vector contains duplicates\n";
39+
} else {
40+
std::cout << "Above vector does not contain duplicates\n";
41+
}
42+
return 0;
43+
}

leet_code_problems/moveZeroes.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.
3+
* For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
4+
*
5+
* Approach: start putting all non zero elements from at the start, We will fill in all non zero elements in order.
6+
*/
7+
8+
#include <iostream>
9+
#include <vector>
10+
#include <algorithm>
11+
12+
void moveZeroes(std::vector<int>& nums) {
13+
size_t j = 0;
14+
for ( size_t i = 0; i < nums.size(); ++i ) {
15+
if ( nums[i] != 0 ) {
16+
std::swap(nums[i], nums[j]);
17+
++j;
18+
}
19+
}
20+
}
21+
22+
void printVec(std::vector<int> & vec) {
23+
std::cout << "Vec:";
24+
for( auto & v : vec ) {
25+
std::cout << v << " ";
26+
}
27+
std::cout << std::endl;
28+
}
29+
30+
int main() {
31+
std::vector<int> nums{ 0, 1, 0, 3, 12 };
32+
std::cout << "Before ";
33+
printVec(nums);
34+
moveZeroes(nums);
35+
std::cout << "After ";
36+
printVec(nums);
37+
return 0;
38+
}

leet_code_problems/rotateList.cpp

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Given a list, rotate the list to the right by k places, where k is non-negative.
3+
* For example:
4+
* Given 1->2->3->4->5->NULL and k = 2,
5+
* return 4->5->1->2->3->NULL
6+
*/
7+
8+
#include <iostream>
9+
10+
struct ListNode {
11+
int val;
12+
ListNode * next;
13+
ListNode(int x) : val(x), next(NULL) {}
14+
};
15+
16+
17+
void printList(ListNode * node) {
18+
std::cout << "List: ";
19+
while( node ) {
20+
std::cout << node->val << " ";
21+
node = node->next;
22+
}
23+
std::cout << std::endl;
24+
}
25+
26+
void insert(ListNode* & head, int data ) {
27+
28+
ListNode * newNode = new ListNode(data);
29+
if ( head == nullptr ) {
30+
head = newNode;
31+
} else {
32+
ListNode * curr = head;
33+
while ( curr->next != nullptr ) {
34+
curr = curr->next;
35+
}
36+
curr->next = newNode;
37+
}
38+
}
39+
40+
int length(ListNode * head) {
41+
int count = 0;
42+
while( head ) {
43+
head = head->next;
44+
++count;
45+
}
46+
return count;
47+
}
48+
49+
50+
ListNode* rotateRight(ListNode* head, int k) {
51+
if ( head == nullptr || head->next == nullptr) {
52+
return head;
53+
}
54+
int len = length(head);
55+
k = k % len;
56+
if ( k == 0) {
57+
return head;
58+
}
59+
ListNode * ptr1 = head;
60+
ListNode * ptr2 = head;
61+
int c = 0;
62+
while( ptr1 && c < k ) {
63+
ptr1 = ptr1->next;
64+
c++;
65+
}
66+
if (!ptr1) {
67+
return nullptr;
68+
}
69+
while( ptr1 && ptr1->next != nullptr ) {
70+
ptr1 = ptr1->next;
71+
ptr2 = ptr2->next;
72+
}
73+
ListNode * newHead = ptr2->next;
74+
ptr2->next = nullptr;
75+
ptr1->next = head;
76+
return newHead;
77+
}
78+
79+
80+
int main() {
81+
ListNode * head = nullptr;
82+
insert( head, 1);
83+
insert( head, 2);
84+
insert( head, 3);
85+
insert( head, 4);
86+
insert( head, 5);
87+
printList(head);
88+
head = rotateRight(head, 2);
89+
printList(head);
90+
return 0;
91+
}

0 commit comments

Comments
 (0)