
leetcode
文章平均质量分 57
胡阿优
啦啦啦
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
leetcode two sum
先用快排,时间复杂度为nlogn,再在每个循环里用二分进行查找,时间复杂度为nlogn,总体时间复杂度为nlognclass Solution {public: vector<int> twoSum(vector<int>& nums, int target) { vector<int> result, copy_nums = nums; sort(nums原创 2016-05-29 14:07:44 · 243 阅读 · 0 评论 -
218. The Skyline Problem [leetcode]
这道题的原理就是,寻找有效的拐点。该点满足不被其他任何building覆盖,或者为一个building右部的拐点。一个building具有的覆盖力从左边起持续到右边结束。当右边结束的时候,该building对有部分的所有building都拾取了覆盖力。因此,我们先将所有的building的左边线段和右边线段分开,将位置和高度信息以pair的形式存入vector中。再将所有线段按照位置的先后排序。然后遍历这些线段。当遍历到开始(building的左边线段)线段时,将其加入multiset容器m中,当遍历原创 2017-08-11 16:39:59 · 318 阅读 · 0 评论 -
187. Repeated DNA Sequences [leetcode]
题目:All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DN原创 2017-08-11 15:18:34 · 234 阅读 · 0 评论 -
347. Top K Frequent Elements [leetcode]
法 一:hashmap + vector时间:O(n)class Solution {public: vector topKFrequent(vector& nums, int k) { vector res; if (k < 1) return res; if (k >= nums.size()) return nums;原创 2017-08-11 11:35:33 · 230 阅读 · 0 评论 -
220. Contains Duplicate III [leetcode]
题目:Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute原创 2017-08-10 15:57:09 · 217 阅读 · 0 评论 -
260. Single Number III [leetcode]
每次遇得这种位操作的新题,都不会做。很无奈。看了大神的讲解才恍然大悟。哎。原理就是对数组进行分组,使得两个单独的数a、b被分到不同的组。再在不同的组里面通过异或的操作分别得出两个数。怎么样分组呢? 寻找a与b的不同点。此时,不用考虑其他数,因为其他数都是成双的,相同的数一定会被分到同一组。通过异或操作我们可以得到a与b不同的位。我们选取a^b的最低不为0的位。通过判断每个数原创 2017-08-10 00:33:18 · 236 阅读 · 0 评论 -
110. Balanced Binary Tree [leetcode]
用递归来解题。/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };原创 2017-08-09 20:26:59 · 215 阅读 · 0 评论 -
164. Maximum Gap [leetcode]
这道题可以有两种刚解法:解法一:基于bucket sort的方法:有n个未排序的数字。1、先求出数组内最大数max和最小数min。2、求出每个桶内的数字范围:gap = (max - min) / (n - 1)。3、算出桶的数量: N = (max - min)/ gap + 1 (可知 N >= n)每个桶对应的数字范围为: 第一个:[min, min原创 2017-08-09 19:54:17 · 267 阅读 · 0 评论 -
89. Gray Code [leetcode]
89. Gray CodeThe gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, pri原创 2017-08-08 12:15:11 · 292 阅读 · 0 评论 -
Battleships in a Board [Leetcode]
从上到下,从左到右遍历,遇到X,判断左边和上边是否有X,没的话则为新出现的battleship。class Solution {public: int countBattleships(vector<vector<char>>& board) { int res = 0; for (int i = 0; i < board.size(); i++) {原创 2017-08-01 18:17:27 · 221 阅读 · 0 评论 -
142. Linked List Cycle II [leecode]
利用 floyd cycle detection原理来解题分析详见 博客287. Find the Duplicate Number [Leetcode]代码:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * L原创 2017-08-08 10:50:09 · 264 阅读 · 0 评论 -
189. Rotate Array [Leetcode]
题目:--------------------------------------------------------------------------------------------------------------------------------------Rotate an array of n elements to the right by k steps原创 2017-08-07 17:23:07 · 301 阅读 · 0 评论 -
287. Find the Duplicate Number [Leetcode]
这题首先想到的最简单的方法是用快排然后再遍历。然而题目要求不能改变原来的数组顺序。所以快排的方法被排除。后来想到了用binary search来做。方法一:Binary Search由于数字的范围是确定的[1,n]取数字mid,如果【1,mid)没有重复的数字的话,这区间的数字个数应该 mid - 1, 如果在(mid,n] 之间没有重复数字的话,该区间的数字个数应该 (m原创 2017-08-07 14:58:53 · 355 阅读 · 0 评论 -
150. Evaluate Reverse Polish Notation [Leetcode]
用stack来保存还未操作的数字。 一旦遇见操作符,则pop出stack中的头两个数字,进行计算,再把结果push进stack中。class Solution {public: int evalRPN(vector<string>& tokens) { stack<int> nums; for (auto i : tokens) {原创 2017-07-31 15:36:26 · 209 阅读 · 0 评论 -
384. Shuffle an Array [Leetcode]
利用swap+随机数生成一个随机的序列。class Solution {public: Solution(vector<int> nums) { srand(time(NULL)); this->originalNums = nums; size = nums.size(); } /** Resets the array to原创 2017-07-31 14:55:45 · 246 阅读 · 0 评论 -
153. Find Minimum in Rotated Sorted Array [Leetcode]
用二分查找法来解决:class Solution {public: int findMin(vector<int>& nums) { int left = 0, right = nums.size() - 1; while (right > left) { int mid = left + (right - left) / 2;原创 2017-07-31 14:24:14 · 210 阅读 · 0 评论 -
49. Group Anagrams [Leetcode]
核心思想: 利用mashmap来存储同构词对应的字符表。其中每一个key对应着结果vector中字符集所在的index。运行耗时:26msclass Solution {public: vector<vector<string>> groupAnagrams(vector<string>& strs) { vector<vector<string>> words;原创 2017-07-31 11:35:02 · 195 阅读 · 0 评论 -
420. Strong Password Checker [leetcode]
思路: 有三个问题需要解决: 1、缺失字符种类问题(大写,小写、数字) 可用操作:replace,insert 2、长度问题 可用该操作:insert(< 6),delete(> 20) 3、重复问题 可用操作: replace,insert,delete,其中replace的解决方法最优。可以看到各种问题之间是有重复性的,也就是说可以通过解决某个问题,同时解决另一个问题。原创 2017-08-06 11:59:59 · 865 阅读 · 0 评论