LeetCode 练习 4月
https://leetcode.com/discuss/general-discussion/551411/30-day-leetcoding-challenge
4月
4.1 日 Single Number
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
这道题主要的要求是:程序具有线性复杂度,并且最好不使用额外的内存。
除去这两个要求的话,有如下方法:
第一种:先对序列进行排序,之后遍历一遍序列,如果存在一个数如 array[i] 的左边 array[i-1] 和右边 array[i+1] 均与 array[i] 不同,则这个数为要找的数(i=0以及i=n-1时进行特殊处理)
第二种:使用hashset,这是参考着https://www.cnblogs.com/grandyang/p/4130577.html 看的,我之前对于hashset了解并不多,这里正好也了解一下:https://blog.csdn.net/wds1181977/article/details/51424839 可以看到,其实在java中hashset是通过hashmap实现的,只是在键值对方面hashset 是不同的键对应同一个键值对。根据hashset的思想,可以解决该问题:遍历序列,如果当前值不在hashset中,则放入hashset,否则将同样的值移除hashset。注意:在java中有hashset,在C++中使用的是unordered_set,解决代码如下:
1
2
3
4
5
6
7
8
9
10
11class Solution {
public:
int singleNumber(vector<int>& nums) {
unordered_set<int> st;
for (int num : nums) {
if (st.count(num)) st.erase(num);
else st.insert(num);
}
return *st.begin();
}
};
但是由于上面条件的限制,不能使用上述两个方法。
开始我也没有想到,想了很长时间,想过通过数学的方法,加加减减得到结果,但是经过测试后不可行。
之后参考了网上的方法,使用*C++的异或操作 ^ * ,比如x=2,y=3 ,z=3.则x ^ y = 5,x^y^z = 2 , y ^ z = 0;
下面是解决方法:
1 | class Solution { |
4月2日 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:
Input: 19
Output: true
Explanation: 12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
解决代码:
1 | class Solution { |
代码提交后显示我的代码运行时间是较长的,我看了那些运行时间很短的代码,他们很多用了些技巧,比如:如果1<=n<10的话,如果n=1或者n=7,那么就符合,否则就不符合。但是这样我感觉不好!因为你需要先知道1-10中符合条件的数。
4月3日 Maximum Subarray
Given an integer array nums
, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.
Follow up:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
1 | class Solution { |
4月4日 Move Zeroes
Given an array nums
, write a function to move all 0
‘s to the end of it while maintaining the relative order of the non-zero elements.
Example:
Input: [0,1,0,3,12]
Output: [1,3,12,0,0]
Note:
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
1 | class Solution { |
4月5日 Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).
Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
Example 1:
Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Example 2:
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.
Example 3:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
1 | class Solution { |