Search insert location (simple)
Given a sort array and a target value, find the target value in the array and return its index. If the target value does not exist in the array, returns where it will be inserted sequentially.
You can assume that there are no duplicate elements in the array.
Problem solving:
Simple binary search problem, because we know that when there is a target value in the sorting array, we can directly return the mid subscript. When there is no target value in the sorting array, we can analyze and conclude that in the first reciprocal of the while loop, it must be left=right, and the element to be located must be the first element larger than the target value, followed by right The value minus one exits the while loop. Therefore, we can know that the element corresponding to the left subscript is where the target target value should be inserted.
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) >> 1); if(nums[mid] == target) { return mid; } else if(nums[mid] < target) { left = mid + 1; } else { right = mid - 1; } } return left; } };
Appearance series (simple)
Appearance sequence is a sequence of integers. Starting from the number 1, each item in the sequence is a description of the previous item. The first five are as follows:
1 | 1 |
2 | 11 |
3 | 21 |
4 | 1211 |
5 | 111221 |
Among them:
- 1 is read as "one 1" ("one"), that is, 11.
- 11 is read as "two 1s" ("two ones"), i.e. 21.
- 21 is read as "one 2", "one 1" ("one two", "one one one"), i.e. 1211.
Given a positive integer n (1 ≤ n ≤ 30), output the nth term of appearance sequence.
Note: each entry in an integer sequence will be represented as a string.
Problem solving:
This question is not difficult in fact. The important thing is to understand the meaning of the question. It seems to me that this question was asked in the previous meituan offline interview. At that time, I gave the first five items of the appearance series in the way of an intelligence question, and asked me to write the sixth item. At that time, I didn't react. Now I turn around and find that it was the abstraction of the appearance series. Let's analyze the problem: the appearance sequence is actually a sequence that repeatedly describes the previous number. Let's give an example:
- First, the first number is 1, which is given in advance;
- Next, the second number describes the first number 1, one of which is 1, so the second number is 11;
- Next, the third number describes the second number 11. There are two ones in 11, so the third number is 21;
- Next, the fourth number describes the third number 21, including one 2 and one 1, so the fourth number is 1211;
- Next, the fifth number describes the fourth number 1211, including 1 1, 1 2 and 2 1, so the fifth number is 111221.
Now we finally understand the meaning of the question and start our code:
class Solution { public: string countAndSay(int n) { string str, res; res = "1"; if (n == 1) return res; for (int i = 1; i < n; ++i) { str = res; res = ""; for (int j = 0; j < str.size();) { // c is the counter, k is the corresponding subscript of the current description number int c=0,k=j; // Count if K is legal and str[k] is repeated while(k<str.size()&&str[k]==str[j]) { ++k; ++c; } res+=to_string(c)+str[j]; j=k; } } return res; } };
Length of last word (simple)
Given a string containing only uppercase and lowercase letters and spaces', returns the length of its last word.
If the last word does not exist, return 0.
Description: a word is a string of letters without any spaces.
Problem solving:
The focus of this question is on the consideration of special circumstances:
- In the first case, "Hello World", the length of the last word World is returned 5
- In the second case, "Hello World", you need to deal with the trailing space first, and then return the length of the last word World 5
After analyzing the special situation, start to write the code:
class Solution { public: int lengthOfLastWord(string s) { int last = s.size(); for(int i = s.size() - 1; i >= 0; --i) { if(s[i] == ' ') last = i; else break; } int flag = 0; for(int i = 0; i < last; ++i) { if(s[i] == ' ') { flag = i + 1; } } return last - flag; } };
Binary sum (simple)
Given two binary strings, return their sum (in binary).
The input is a non empty string and contains only the numbers 1 and 0.
Problem solving:
Reverse sum, null complement 0, mark carry number.
class Solution { public: string addBinary(string a, string b) { string ans = ""; int ca = 0; for(int i = a.size() - 1, j = b.size() - 1; i >= 0 || j >= 0; --i, --j) { int sum = ca; sum += i >= 0 ? a[i] - '0' : 0; sum += j >= 0 ? b[j] - '0' : 0; ans = to_string(sum % 2) + ans; ca = sum / 2; } if (ca == 1) ans = to_string(ca) + ans; return ans; } };
Square of x (simple)
Implement the int sqrt(int x) function.
Calculates and returns the square root of X, where x is a non negative integer.
Because the return type is an integer, the result only retains the integer part, and the decimal part will be rounded off.
Problem solving:
This problem is also called Newton method, in other words, the deformation problem of binary search. Before there was a blog about binary search, you can see that blog specifically.
class Solution { public: int mySqrt(int x) { long left = 0, right = x/2 + 1; while(left <= right) { long mid = left + ((right - left) >> 1); if(mid * mid > x) { right = mid - 1; } else { if((mid == x/2 + 1) || (mid+1) * (mid+1) > x) return mid; else left = mid + 1; } } return -1; } };
Stair climbing (simple)
Suppose you are climbing the stairs. You need n steps to get to the top of the building.
You can climb one or two steps at a time. How many different ways can you climb to the top of the building?
Note: given that n is a positive integer.
Problem solving:
A simple derivation problem, derived formula: A[n] = A[n-1] + A[n-2] and then solved:
class Solution { public: int climbStairs(int n) { if (n < 3) return n; const int len = n+1; int a[len]; a[1] = 1; a[2] = 2; for(int i = 3; i <= n; ++i) { a[i] = a[i-1] + a[i-2]; } return a[n]; } };
Delete duplicate elements in sort list (simple)
Given a sort list, delete all duplicate elements so that each element appears only once.
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* deleteDuplicates(ListNode* head) { ListNode* current = head; while (current != nullptr && current->next != nullptr) { if (current->next->val == current->val) { current->next = current->next->next; } else { current = current->next; } } return head; } };
Same tree (simple)
Given two binary trees, write a function to check whether they are the same.
If two trees are structurally identical and the nodes have the same values, they are considered to be identical.
Problem solving:
Recursively solve:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == nullptr && q == nullptr) return true; else if(p == nullptr || q == nullptr) return false; else if(p->val != q->val) return false; return isSameTree(p->left, q->left) && isSameTree(p->right, q->right); } };