![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
时空优化
peace in mind
这个作者很懒,什么都没留下…
展开
-
[LeetCode]Majority Element
数组求过半数的元素。做到这道题的时候,心里也是一阵痛,因为上学期算法期末考刚好有这样一道题,而且有空间的限制。当时想着用map来实现,时间上是快啊。但是不用想,那么大的数据,空间肯定会爆。LeetCode上的这个解法只用维护两个变量,而且时间也是O(n):源代码:int majorityElement(vector &num) { int count原创 2015-03-29 21:26:01 · 391 阅读 · 0 评论 -
[Leetcode] Wildcard Matching
题目链接在此。Implement wildcard pattern matching with support for '?' and '*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matchi原创 2017-03-13 16:02:30 · 436 阅读 · 0 评论 -
[Leetcode] 3Sum
题目链接在此。上古老题。Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: The sol原创 2017-03-13 04:26:22 · 423 阅读 · 0 评论 -
[Leetcode] Rotate Image
题目链接在此You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?把一个n x n的2D矩阵逆时针旋转90度,不用额外空间。原创 2017-03-13 04:00:29 · 293 阅读 · 0 评论 -
[Leetcode] Search in Rotated Sorted Array
原题链接在此Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to原创 2017-03-03 15:30:21 · 310 阅读 · 0 评论 -
[Leetcode] Container With Most Water
题目链接在此Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (转载 2017-03-01 19:49:55 · 293 阅读 · 0 评论 -
[LeetCode] Find All Numbers Disappeared in an Array
题目链接在此Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in thi原创 2017-02-16 21:33:07 · 264 阅读 · 0 评论 -
[LeetCode] 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.For example, given nums = [0, 1,转载 2015-10-08 14:14:13 · 330 阅读 · 0 评论 -
[Leetcode]Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘原创 2015-04-01 00:11:06 · 340 阅读 · 0 评论 -
[leetcode] Kth Smallest Element in a BST
题目链接在此Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow原创 2015-08-04 15:49:11 · 447 阅读 · 0 评论 -
[leetcode] Reverse Bits
题目链接在此Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as原创 2015-08-12 14:37:33 · 367 阅读 · 0 评论 -
[Leetcode]Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the转载 2015-03-31 11:19:29 · 313 阅读 · 0 评论 -
[LeetCode]Min Stack
实现栈的常规操作,其中getMin()要求用常数时间获取栈中当前最小元素。分析:用空间换时间:在栈A(原本的栈)外多用一个向栈顶递减的栈B。 插入值时如果值要小于或等于栈B顶元素,就将它同时压入栈A和B,否则只压入A;A的元素出栈时如果栈顶元素恰好等于B栈顶元素则同时B出栈。class MinStack {public: void push(int x原创 2015-03-29 22:44:03 · 322 阅读 · 0 评论 -
[Leetcode]Single Number I&II
第一题是说数组里恰好只有一个元素恰好出现了一次,其他元素都恰好出现了两次。Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. C原创 2015-03-31 10:17:55 · 310 阅读 · 0 评论