Skip to content

Commit 57588fb

Browse files
Search Insert Position
1 parent 33eb15a commit 57588fb

File tree

4 files changed

+122
-1
lines changed

4 files changed

+122
-1
lines changed

10.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
3+
Search Insert Position
4+
----------------------
5+
6+
Given a sorted array 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.
7+
8+
You may assume no duplicates in the array.
9+
10+
Example 1:
11+
12+
Input: [1,3,5,6], 5
13+
Output: 2
14+
Example 2:
15+
16+
Input: [1,3,5,6], 2
17+
Output: 1
18+
Example 3:
19+
20+
Input: [1,3,5,6], 7
21+
Output: 4
22+
Example 4:
23+
24+
Input: [1,3,5,6], 0
25+
Output: 0
26+
*/
27+
28+
class Solution {
29+
public:
30+
int searchInsert(vector<int>& nums, int target) {
31+
if(binary_search(nums.begin(), nums.end(), target)) return lower_bound(nums.begin(), nums.end(), target) - nums.begin();
32+
else return upper_bound(nums.begin(), nums.end(), target - 1) - nums.begin();
33+
}
34+
};

8.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
3+
Power of Two
4+
------------
5+
6+
Given an integer, write a function to determine if it is a power of two.
7+
8+
Example 1:
9+
10+
Input: 1
11+
Output: true
12+
Explanation: 20 = 1
13+
Example 2:
14+
15+
Input: 16
16+
Output: true
17+
Explanation: 24 = 16
18+
Example 3:
19+
20+
Input: 218
21+
Output: false
22+
*/
23+
24+
class Solution {
25+
public:
26+
bool isPowerOfTwo(int n) {
27+
long long m = n;
28+
return m && (!(m&(m-1)));
29+
}
30+
};

9.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
3+
Is Subsequence
4+
--------------
5+
6+
Given a string s and a string t, check if s is subsequence of t.
7+
8+
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).
9+
10+
Follow up:
11+
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?
12+
13+
Credits:
14+
Special thanks to @pbrother for adding this problem and creating all test cases.
15+
16+
17+
18+
Example 1:
19+
20+
Input: s = "abc", t = "ahbgdc"
21+
Output: true
22+
Example 2:
23+
24+
Input: s = "axc", t = "ahbgdc"
25+
Output: false
26+
27+
28+
Constraints:
29+
30+
0 <= s.length <= 100
31+
0 <= t.length <= 10^4
32+
Both strings consists only of lowercase characters.
33+
*/
34+
35+
class Solution {
36+
public:
37+
bool isSubsequence(string s, string t) {
38+
int j=0;
39+
for(int i=0; i<t.size(); i++) {
40+
if(j < s.size() && t[i] == s[j]) j++;
41+
}
42+
return j == s.size();
43+
}
44+
};

README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
# leetcode-june-challenge
1+
# LeetCode June Challenge
2+
3+
| S No | Problem | URL |
4+
|:----:|:------------------------------:|:------------------------------------------------------------------------------------:|
5+
| 1 | Invert Binary Tree | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/1.cpp) |
6+
| 2 | Delete Node in a Linked List | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/2.cpp) |
7+
| 3 | Two City Scheduling | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/3.cpp) |
8+
| 4 | Reverse String | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/4.cpp) |
9+
| 5 | Random Pick with Weight | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/5.cpp) |
10+
| 6 | Queue Reconstruction by Height | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/6.cpp) |
11+
| 7 | Coin Change 2 | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/7.cpp) |
12+
| 8 | Power of Two | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/8.cpp) |
13+
| 9 | Is Subsequence | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/9.cpp) |
14+
| 10 | Search Insert Position | [Link](https://github.com/ishpreet-singh/leetcode-june-challenge/blob/master/10.cpp) |

0 commit comments

Comments
 (0)