350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.
Example:
1 | Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. |
Note:
Each element in the result should appear as many times as it shows in both arrays.
The result can be in any order.
Follow up:
What if the given array is already sorted? How would you optimize your algorithm?
What if nums1’s size is small compared to nums2’s size? Which algorithm is better?
What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
问题
求出两个数组中全部重复的元素
思路
使用unordered_map 进行统计++,随后再比较–
C++
1 | vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { |
268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.
For example,
Given nums = [0, 1, 3] return 2.
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
问题
给一个顺序数组,找出遗失的数字
思路
通过^找出遗失的数字,多出的数字一定等于数组长度
C++
1 | int missingNumber(vector<int>& nums) { |
447. Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a “boomerang” is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters).
Find the number of boomerangs. You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive).
Example:
Input:
[[0,0],[1,0],[2,0]]
Output:
2
Explanation:
The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]
问题
给出一系列坐标,通过这些坐标问你能找出几组回旋镖数组?回旋镖数组的意思是中点到两点距离相同。
思路
使用欧氏距离计算两点距离(不开方)
使用unordered_map存储 距离:相同距离的边数,
再使用排列组合将边数的组合数全部加起来
C++
1 | int numberOfBoomerangs(vector<pair<int, int>>& points) { |
541. Reverse String II
Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.
Example:
1 | Input: s = "abcdefg", k = 2 |
Restrictions:
The string consists of lower English letters only.
Length of the given string and k will in the range [1, 10000]
问题
给一个数组s,每隔k位翻转字符
C++
1 | string reverseStr(string s, int k) { |
收获
min(s.begin()+i+k,s.end())
551. Student Attendance Record I
You are given a string representing an attendance record for a student. The record only contains the following three characters:
1 | 'A' : Absent. |
问题
给一组数组,超过1个A或者连续2个L则输出false
思路
使用map进行统计,遇到L则检查前两位是否是L
C++
1 | bool checkRecord(string s) { |
别人写法
1 | bool checkRecord(string s) { |
收获
regex_search(s, regex(“A.*A|LLL”)) 正则表达式
543. Diameter of Binary Tree
Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
Example:
1 | Given a binary tree |
Note: The length of path between two nodes is represented by the number of edges between them.
问题
找出二叉树中任意两点的最长距离
思路
使用DFS遍历左右子树,将两棵树的深度相加
C++
1 | int maxDepth = 0; |
108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
问题
将有序数组转化为二叉搜索树
思路
去数组的中位数作为root,递归构建左右子树
C++
1 | TreeNode* sortedArrayToBST(vector<int>& nums) { |
415. Add Strings
Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.
Note:
1 | The length of both num1 and num2 is < 5100. |
问题
给2个用string表示的数字,将其相加并输出,
思路
进位(carry)累加到下一位计算,将最终结果逆序输出
C++
1 | string addStrings(string num1, string num2) { |
405. Convert a Number to Hexadecimal
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.
Note:
1 | All letters in hexadecimal (a-f) must be in lowercase. |
问题
将数转化为16进制数
思路
十进制&15 输出的结果为对应16进制数的最后4位对应的十进制数
C++
1 | string toHex(int num) { |
收获
num & 15 转化16进制
121. Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
1 | Example 1: |
max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
1 | Example 2: |
In this case, no transaction is done, i.e. max profit = 0.
问题
给一组数组,a[i]代表第i天的股票价格,问只能进行一次买卖的话怎么样才能获得最大收益。
思路
每次取一个日期,遍历求出他能取得的最大收益,直到所有日期取完
C++
1 | int maxProfit(vector<int>& prices) { |
202. Happy Number
Write an algorithm to determine if a number is “happy”.
A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
Example: 19 is a happy number
1 | 1^2 + 9^2 = 82 |
问题
找出happy number
思路
非happy number会出现循环解(听过insert进set的成功与否 来探查)
C++
1 | bool isHappy(int n) { |
收获
从数理角度考虑,数学归纳法?
326. Power of Three
Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
问题
求一个数是否是三的倍数
思路
log10(n)/log10(3)是否为整数
C++
1 | bool isPowerOfThree(int n) { |
231. Power of Two
同上
83. Remove Duplicates from Sorted List
Given a sorted linked list, delete all duplicates such that each element appear only once.
1 | For example, |
问题
删除重复的结点
思路
C++
1 | ListNode* deleteDuplicates(ListNode* head) { |
35. Search Insert Position
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.
You may assume no duplicates in the array.
1 | Here are few examples. |
问题
插入数字,返回位置
思路
常规解法不提,提一提二分查找
C++
1 | int searchInsert(vector<int>& nums, int target) { |
437. Path Sum III
You are given a binary tree in which each node contains an integer value.
Find the number of paths that sum to a given value.
The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).
The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.
1 | Example: |
问题
树种结点组合成指定数字的组合数,只能由父到子
思路
两次递归
C++
1 | public: |
70. Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
Note: Given n will be a positive integer.
问题
爬楼梯,一次只能爬1或者2,求有多少种爬法
思路
斐波那契数列
C++
1 | int climbStairs(int n) { |
收获
###