Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions cpp/189-Rotate-Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Given an array, rotate the array to the right by k steps, where k is non-negative.
Ex.
Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]

1.- To avoid problems with the size of the vector we use the remainder of a division.
2.- Reverse the entire vector.
3.- Reverse the parts you want to obtain the result.

Time: O(1)
Space: O(1)
*/

class Solution {
public:
void rotate(vector<int>& nums, int k) {
k %= nums.size();
reverse(nums.begin(), nums.end());
reverse(nums.begin(), nums.begin() + k);
reverse(nums.begin() + k, nums.end());
}
};
39 changes: 39 additions & 0 deletions cpp/278-First-Bad-Version.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Suppose you have versions and you want to find out the first bad one, which causes all the following ones to be bad.n[1, 2, ..., n]
You are given an API which returns whether is bad. Implement a function to find the first bad version.

Ex.
Input: n = 5, bad = 4
Output: 4

Explanation:
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true

1.- Find the number in the middle of the vector.
2.- Takes a part (first or second), depending on whether or not the target is greater than the middel.
3.- Change the current left or right part.
3.- Do this process until the left reaches the right.

Time: O(log n)
Space: O(1)
*/

class Solution {
public:
int firstBadVersion(int n) {
int left = 1;
int right = n;

while (right > left) {
int mid = left + (right - left) / 2;

if (isBadVersion(mid))
right = mid;
else
left = mid + 1;
}
return left;
}
};
34 changes: 34 additions & 0 deletions cpp/35-Search-Insert-Position.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
Given a sorted array of distinct integers and a target value, return the index if the target is found.
If not, return the index where it would be if it were inserted in order.

Ex.
Input: nums = [1,3,5,6], target = 5
Output: 2

1.- Find the number in the middle of the vector.
2.- Takes a part (first or second), depending on whether or not the target is greater than the middel.
3.- Change the current left or right part.
3.- Do this process until the left exceeds the right.

Time: O(log n)
Space: O(1)
*/

class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int left = 0;
int right = nums.size() - 1;

while (left <= right) {
int mid = left + (right - left) / 2;

if (nums[mid] < target)
left = mid + 1;
else
right = mid - 1;
}
return left;
}
};
24 changes: 24 additions & 0 deletions cpp/977-Squares-of-a-Sorted-Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Given an integer array nums sorted in non-decreasing order, return
an array of the squares of each number sorted in non-decreasing order.

Ex.
Input: nums = [-4,-1,0,3,10]
Output: [0,1,9,16,100]

1.- Multiply each number of the nums by themselves.
2.- Use the sort function to sort the vector.

Time: O(NlogN)
Space: O(N)
*/

class Solution {
public:
vector<int> sortedSquares(vector<int>& nums) {
for (int& i : nums)
i *= i;
sort(nums.begin(), nums.end());
return nums;
}
};