
Leetcode
OOC_ZC
OOC
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Leetcode之Two Sum 题解
class Solution {public:/* O(n*n)解法 vector<int> twoSum(vector<int>& nums, int target) { vector<int> ans; for(int i = 0; i < nums.size(); ++i){ for(int j = i + 1; j < n原创 2018-01-15 23:20:25 · 341 阅读 · 0 评论 -
Top K 问题
基于堆排序(内存足够存放所有数据)堆排序的初始建堆操作时间复杂度为O(N)(可证,通过级数),之后每加入新元素或者取走堆顶元素的时间复杂度为O(logN),所以堆排序的时间复杂度为O (N*logN) 。 因为每次取堆顶的元素都是最大/最小的,这里建大顶堆的话,取到第K大的元素时间复杂度为O(N+K*logN) 。class Solution{public: void Ad...原创 2018-09-11 21:44:58 · 337 阅读 · 0 评论 -
Leetcode之4sum 解法
类似3sum问题,在外面增加一个循环。 注意重复的问题。class Solution {public: vector<vector<int>> fourSum(vector<int> &nums, int target) { vector<vector<int>> ans; if...原创 2018-09-11 14:10:43 · 601 阅读 · 0 评论 -
二进制中1的个数
如果输入为负数,用补码表示此数。(即不需要多余的处理)常见写法:int NumberOf1 (int n) { int flag = 1; int ans = 0; while (flag) { // 左移最后变为0 if(flag & n) ans ++; flag = flag <&l...原创 2018-09-01 16:13:05 · 183 阅读 · 0 评论 -
Kth Largest Element in an Array - 找出数组中的第K大的数
这个问题比较经典,有很多解法,这里介绍几个经典解法。原创 2018-07-29 17:27:46 · 412 阅读 · 0 评论 -
Leetcode之Longest Palindromic Substring
寻找字符串中的最长回文子串。// dp[i][j] == 1 表示字符串i-j为回文。/* dp[i][j] = true, i == j dp[i][j] = true, i == j - 1 && str[i] == str[j] dp[i][j] = true, i < j - 1 && str[i] == str[j]...原创 2018-05-19 17:37:09 · 214 阅读 · 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 (i, 0). Find two li...原创 2018-05-19 17:34:50 · 227 阅读 · 0 评论 -
Leetcode之Valid Phone Numbers解法
用Shell脚本判断file.txt文件中的电话号是否符合要求的格式。 两种格式: (xxx) xxx-xxxxxxx-xxx-xxxxawk '/^(\([0-9]{3}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$/' file.txt用awk命令+RE解决。 开始及结尾处的 / 是正则表达式的定界符(在一些语言里要求使用)。 ^ 表示无其他前缀。 $ ...原创 2018-05-18 16:29:51 · 457 阅读 · 0 评论 -
Leetcode之Median_of_Two_Sorted_Arrarys解法
class Solution {public: double findMedianSortedArrays(vectorint>& nums1, vectorint>& nums2) { int a[100000], a_num = 0; int i = 0, j = 0, x, y; while(i < nums1.size() || j原创 2018-01-16 23:10:38 · 350 阅读 · 0 评论 -
Leetcode之Longest Substring Without Repeating Characters解法
class Solution {public: int lengthOfLongestSubstring(string s) { int ans = 0, i = 0, j = 0; unordered_map<char, int> map; for( j = 0; j < s.length(); ++j){ if(m原创 2018-01-16 13:59:52 · 244 阅读 · 0 评论 -
Leetcode之Add Two Numbers解法
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* ad原创 2018-01-15 23:24:54 · 426 阅读 · 0 评论 -
LeetCode Database 题目
175.SELECT Person.FirstName, Person.LastName, Address.City, Address.StateFROM Person LEFT JOIN AddressON Person.PersonId = Address.PersonId;176.SELECT MAX(Salary) AS SecondHighestSalaryFROM Emp...原创 2019-06-20 10:40:44 · 311 阅读 · 0 评论