【LeetCode】2020-03 每日一题

121. 买卖股票的最佳时机(简单)

【分类】:模拟、思维

【题解】:可以用O(n)的复杂度完成,只需要在遍历的时候记录到当前位置为止买入股票的最小价格minn,再维护一个当前卖出股票价(a-minn)的最大值即可。

【代码】:

C++:

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int minn = 0x3f3f3f3f;
        int ans = 0;
        for(int i = 0; i < prices.size(); i++)
        {
            minn = min(prices[i], minn);
            ans = max(ans, prices[i] - minn);
        }
        return ans;
    }
};
View Code

Python3:

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        ans = 0
        minn = float(inf)
        for a in prices:
            minn = min(minn, a)
            ans = max(ans, a - minn)
        return ans
View Code

169. 多数元素(简单)

【分类】:模拟、思维

【题解】:

解法1:找到一个出现次数大于n/2向下取整,其实稍微想一下就知道只要排序一下取中位数即可。

解法2:当然也可以直接做,用map存键值对,键为每个数字,值为每个数字出现的次数,然后遍历map找到出现次数大于n/2的键即可。

【代码】:

C++(解法1):

class Solution {
public:
    int majorityElement(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int n = nums.size();
        return nums[n / 2];
    }
};
View Code

Python3(解法2):

class Solution:
    def majorityElement(self, nums: List[int]) -> int:
        dict1 = {}
        for a in nums:
            if a in dict1:
                dict1[a] = dict1[a] + 1
            else:
                dict1[a] = 1
        for k, v in dict1.items():
            if v > (len(nums) / 2):
                return k
View Code

206. 反转链表(简单)

【分类】:链表、递归

【题解】:

解法1:遍历的思想。其实就是把1->2->3->4变为1<-2<-3<-4。然后就很好理解了,假如说当前head在3,那么需要将head后面的链表保存下来,由于newList已经是1<-2,所以只需要把head指向这个newList就变成1<-2<-3了。当然这种解法也可以携程递归形式。

解法2:递归地思想,还蛮难理解的。假设原理表为1->2->3->4,3和4已经反转那么链表变为1->2->3<-4,此时需要将2->3进行反转,head指向2,那么只需head-->next->nextt = head即可变成2<-3,但是此时head->next也指向3,为了不形成环需要将head->next = NULL,可以看看这个博主的图解,看了就懂的那种。

【代码】

C++(解法1递归形式):

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* pre = NULL;
    ListNode* tmp = NULL;
    ListNode* reverseList(ListNode* head) {
        if(head == NULL)return pre;
        tmp = head->next;
        head->next = pre;
        pre = head;
        head = tmp;
        return reverseList(head);
    }
};
View Code

C++(解法2):

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        if(head == NULL || head->next == NULL)return head;
        ListNode* now = reverseList(head->next);
        head->next->next = head;
        head->next = NULL;
        return now;
    }
};
View Code

Python3(解法1遍历形式):

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        newList = None
        while(head != None):
            tmp = head.next
            head.next = newList
            newList = head
            head = tmp
        return newList
View Code

猜你喜欢

转载自www.cnblogs.com/z1014601153/p/12654019.html
今日推荐