Arrays-LinkedList-hashtable-strings Flashcards

1
Q

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]

Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]

Constraints:

2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
Only one valid answer exists.

Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity?

A
class Solution {
public:
    vector twoSum(vector& nums, int target) {
        unordered_map hash;
        vector result;
        for(int i =0;i
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/

A

https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

https://leetcode.com/problems/concatenation-of-array/

A

https://leetcode.com/problems/concatenation-of-array/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

https://leetcode.com/problems/running-sum-of-1d-array/

A

https://leetcode.com/problems/running-sum-of-1d-array/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

https://leetcode.com/problems/shuffle-the-array/

A

https://leetcode.com/problems/shuffle-the-array/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/

A

https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

https://leetcode.com/problems/number-of-good-pairs/

A

https://leetcode.com/problems/number-of-good-pairs/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/

A

https://leetcode.com/problems/how-many-numbers-are-smaller-than-the-current-number/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

https://leetcode.com/problems/shuffle-string/

A

https://leetcode.com/problems/shuffle-string/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/

A

https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

https://leetcode.com/problems/truncate-sentence/

A

https://leetcode.com/problems/truncate-sentence/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/

A

https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

https://leetcode.com/problems/sort-array-by-parity/

A

https://leetcode.com/problems/sort-array-by-parity/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

https://leetcode.com/problems/n-repeated-element-in-size-2n-array/

A

https://leetcode.com/problems/n-repeated-element-in-size-2n-array/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/

A

https://leetcode.com/problems/replace-elements-with-greatest-element-on-right-side/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

https://leetcode.com/problems/unique-number-of-occurrences/

A

https://leetcode.com/problems/unique-number-of-occurrences/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

https://leetcode.com/problems/squares-of-a-sorted-array

A

https://leetcode.com/problems/squares-of-a-sorted-array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

https://leetcode.com/problems/intersection-of-two-arrays/

A

https://leetcode.com/problems/intersection-of-two-arrays/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

https://leetcode.com/problems/three-consecutive-odds/

A

https://leetcode.com/problems/three-consecutive-odds/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away/

A

https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

https://leetcode.com/problems/contains-duplicate/

A

https://leetcode.com/problems/contains-duplicate/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/

A

https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

https://leetcode.com/problems/binary-search/

A

https://leetcode.com/problems/binary-search/

24
Q

https://leetcode.com/problems/max-consecutive-ones

A

https://leetcode.com/problems/max-consecutive-ones

25
Q

https://leetcode.com/problems/intersection-of-two-arrays-ii/

A

https://leetcode.com/problems/intersection-of-two-arrays-ii/

26
Q

https://leetcode.com/problems/remove-duplicates-from-sorted-array/

A

https://leetcode.com/problems/remove-duplicates-from-sorted-array/

27
Q

https://leetcode.com/problems/reverse-string/

A

https://leetcode.com/problems/reverse-string/

28
Q

https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/

A

https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/

29
Q

https://leetcode.com/problems/valid-parentheses/

A

https://leetcode.com/problems/valid-parentheses/

30
Q

https://leetcode.com/problems/remove-outermost-parentheses/

A

https://leetcode.com/problems/remove-outermost-parentheses/

31
Q

https://leetcode.com/problems/delete-node-in-a-linked-list/

A

https://leetcode.com/problems/delete-node-in-a-linked-list/

32
Q

https://leetcode.com/problems/reverse-linked-list/

A

https://leetcode.com/problems/reverse-linked-list/

33
Q

https://leetcode.com/problems/merge-two-sorted-lists/

A

https://leetcode.com/problems/merge-two-sorted-lists/

34
Q

https://leetcode.com/problems/remove-duplicates-from-sorted-list/

A

https://leetcode.com/problems/remove-duplicates-from-sorted-list/

35
Q

https://leetcode.com/problems/intersection-of-two-linked-lists/

A

https://leetcode.com/problems/intersection-of-two-linked-lists/

36
Q

https://leetcode.com/problems/linked-list-cycle/

A

https://leetcode.com/problems/linked-list-cycle/

37
Q

https://leetcode.com/problems/palindrome-linked-list/

A

https://leetcode.com/problems/palindrome-linked-list/

38
Q

https://leetcode.com/problems/remove-linked-list-elements/

A

https://leetcode.com/problems/remove-linked-list-elements/

39
Q

https://leetcode.com/problems/middle-of-the-linked-list/

A

https://leetcode.com/problems/middle-of-the-linked-list/

40
Q

https://leetcode.com/problems/merge-in-between-linked-lists/

A

https://leetcode.com/problems/merge-in-between-linked-lists/

41
Q

https://leetcode.com/problems/swapping-nodes-in-a-linked-list/

A

https://leetcode.com/problems/swapping-nodes-in-a-linked-list/

42
Q

https://leetcode.com/problems/odd-even-linked-list/

A

https://leetcode.com/problems/odd-even-linked-list/

43
Q

https://leetcode.com/problems/swap-nodes-in-pairs/

A

https://leetcode.com/problems/swap-nodes-in-pairs/

44
Q

https://leetcode.com/problems/partition-list/

A

https://leetcode.com/problems/partition-list/

45
Q

https://leetcode.com/problems/insertion-sort-list/

A

https://leetcode.com/problems/insertion-sort-list/

46
Q

https://leetcode.com/problems/reverse-linked-list-ii/

A

https://leetcode.com/problems/reverse-linked-list-ii/

47
Q

https://leetcode.com/problems/linked-list-cycle-ii/

A

https://leetcode.com/problems/linked-list-cycle-ii/

48
Q

https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/

A

https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/

49
Q

https://leetcode.com/problems/add-two-numbers/

A

https://leetcode.com/problems/add-two-numbers/

50
Q

https://leetcode.com/problems/remove-nth-node-from-end-of-list/

A

https://leetcode.com/problems/remove-nth-node-from-end-of-list/

51
Q

https://leetcode.com/problems/rotate-list/

A

https://leetcode.com/problems/rotate-list/

52
Q

You’re given strings jewels representing the types of stones that are jewels, and stones representing the stones you have. Each character in stones is a type of stone you have. You want to know how many of the stones you have are also jewels.

Letters are case sensitive, so “a” is considered a different type of stone from “A”.

Example 1:

Input: jewels = “aA”, stones = “aAAbbbb”
Output: 3

Example 2:

Input: jewels = “z”, stones = “ZZ”
Output: 0

Constraints:

1 <= jewels.length, stones.length <= 50
jewels and stones consist of only English letters.
All the characters of jewels are unique.
A
class Solution {
public:
    int numJewelsInStones(string jewels, string stones) {
        unordered_set hash(jewels.begin(),jewels.end());
        int count = 0;
        for(int i:stones){
            if(hash.count(i)){
                count++;
            }
        }
        return count;
} };
53
Q
  1. Check if the Sentence Is Pangram

A pangram is a sentence where every letter of the English alphabet appears at least once.

Given a string sentence containing only lowercase English letters, return true if sentence is a pangram, or false otherwise.

Example 1:

Input: sentence = “thequickbrownfoxjumpsoverthelazydog”
Output: true
Explanation: sentence contains at least one of every letter of the English alphabet.

Example 2:

Input: sentence = “leetcode”
Output: false

Constraints:

1 <= sentence.length <= 1000
sentence consists of lowercase English letters.
A
class Solution {
public:
    bool checkIfPangram(string sentence) {
        unordered_set hash(sentence.begin(),sentence.end());
        if(hash.size()==26) return true;
        return false;
    }
};
54
Q

Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number.

Return the decimal value of the number in the linked list.

Example 1:

Input: head = [1,0,1]
Output: 5
Explanation: (101) in base 2 = (5) in base 10

Example 2:

Input: head = [0]
Output: 0

Example 3:

Input: head = [1]
Output: 1

Example 4:

Input: head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0]
Output: 18880

Example 5:

Input: head = [0,0]
Output: 0

Constraints:

The Linked List is not empty.
Number of nodes will not exceed 30.
Each node's value is either 0 or 1.
A
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    int getDecimalValue(ListNode* head) {
        int sum = 0;
        while(head){
            sum =sum<<1; 
            sum |= head->val;
            head=head->next;
        }
        return sum;
} };
55
Q
  1. First Bad Version
    Easy

You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.

Suppose you have n versions [1, 2, …, n] and you want to find out the first bad one, which causes all the following ones to be bad.

You are given an API bool isBadVersion(version) which returns whether version is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.

Example 1:

Input: n = 5, bad = 4
Output: 4
Explanation:
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
Then 4 is the first bad version.

Example 2:

Input: n = 1, bad = 1
Output: 1

A
// The API isBadVersion is defined for you.
// bool isBadVersion(int version);
class Solution {
public:
    int firstBadVersion(int n) {
        int low=1,high=n,mid;
        while(low<=high){
            mid=low + (high - low) / 2;;
            if(isBadVersion(mid)){
                high=mid-1;
            }
            else{
                low=mid+1;
            }
        }
        return low;
    }
};
56
Q
  1. Search Insert Position
    Easy

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.

You must write an algorithm with O(log n) runtime complexity.

Example 1:

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

Example 2:

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

Example 3:

Input: nums = [1,3,5,6], target = 7
Output: 4

Example 4:

Input: nums = [1,3,5,6], target = 0
Output: 0

Example 5:

Input: nums = [1], target = 0
Output: 0

Constraints:

1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums contains distinct values sorted in ascending order.
-104 <= target <= 104
A
class Solution {
public:
    int searchInsert(vector& nums, int target) {
       int low=0,high=nums.size()-1,mid;
        while(low<=high){
            mid=((low+high)/2);
            if(nums[mid]==target) return mid;
            else if(target