
leetcode
Stillsings
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
LeetCode-34. 在排序数组中查找元素的第一个和最后一个位置(Find First and Last Position of Element in Sorted Array)
二分class Solution {public: vector<int> searchRange(vector<int>& nums, int target) { int l = left_bound(nums, target); int r = right_bound(nums, target); re...原创 2019-11-25 14:32:56 · 253 阅读 · 0 评论 -
LeetCode-33. 搜索旋转排序数组(Search in Rotated Sorted Array)
二分(待优化)class Solution {public: int search(vector<int>& nums, int target) { if(nums.size() == 0){ return -1; } if(nums.size() == 1){ return nums[0] == target ...原创 2019-11-24 10:53:53 · 210 阅读 · 0 评论 -
LeetCode-32. 最长有效括号(Longest Valid Parentheses)
栈class Solution {public: int longestValidParentheses(string s) { stack<int> st; int ans = 0; if(s.size() <= 1) return 0; st.push(-1); for (int i ...原创 2019-11-22 10:34:46 · 231 阅读 · 0 评论 -
LeetCode-31. 下一个排列(Next Permutation)
STL or ManualSTLclass Solution {public: void nextPermutation(vector<int>& nums) { next_permutation(nums.begin(), nums.end()); }};Manualclass Solution {public: ...原创 2019-11-21 17:40:25 · 241 阅读 · 0 评论 -
LeetCode-30. 串联所有单词的子串(Substring with Concatenation of All Words)
哈希(待优化)class Solution {public: vector<int> findSubstring(string s, vector<string>& words) { vector<int> res; if(s.size() == 0 || words.size() == 0) return res; ...原创 2019-11-20 14:50:15 · 202 阅读 · 0 评论 -
LeetCode-35. 搜索插入位置(Search Insert Position)
二分class Solution {public: int searchInsert(vector<int>& nums, int target) { int i; if(target > nums[nums.size() - 1]){ return nums.size(); } ...原创 2019-11-19 19:55:16 · 231 阅读 · 0 评论 -
LeetCode-29. 两数相除(Divide Two Integers)
各种特判class Solution {public: int divide(int dividend, int divisor) { if(dividend == INT_MIN && divisor == -1) return INT_MAX; int flag = (dividend < 0) ^ (divisor < 0) ...原创 2019-11-18 19:01:16 · 171 阅读 · 0 评论 -
LeetCode-28. 实现 strStr()(Implement strStr())——KMP算法
KMP算法class Solution {private: vector<vector<int>> dp;public: int strStr(string haystack, string needle) { if(needle.size() == 0) { return 0; } int j = 0; ...原创 2019-11-18 10:42:03 · 202 阅读 · 0 评论 -
LeetCode-27. 移除元素(Remove Element)
双指针(击败100%!)class Solution {public: int removeElement(vector<int>& nums, int val) { int i = -1; if(nums.size() == 0){ return 0; } for (int j =...原创 2019-11-17 15:15:08 · 195 阅读 · 0 评论 -
LeetCode-26. 删除排序数组中的重复项(Remove Duplicates from Sorted Array)
双指针class Solution {public: int removeDuplicates(vector<int>& nums) { int i = 0; if(nums.size() == 0) { return 0; } for (int j = 1; j < nums....原创 2019-11-17 15:01:29 · 303 阅读 · 0 评论 -
LeetCode-25. K 个一组翻转链表(Reverse Nodes in k-Group)
和24题差不多,用一个指针记录前一个节点,需要特判,若count结束后并不等于k则翻转回来class Solution {public: ListNode* reverseKGroup(ListNode* head, int k) { ListNode *newHead = new ListNode(0); ListNode *relativeHead = newH...原创 2019-11-16 15:46:06 · 292 阅读 · 0 评论 -
LeetCode-24. 两两交换链表中的节点(Swap Nodes in Pairs)
用一个指针记录前一个节点(和下一题一起做了)class Solution {public: ListNode* swapPairs(ListNode* head) { int k = 2; ListNode *newHead = new ListNode(0); ListNode *relativeHead = newHead; ...原创 2019-11-16 15:44:35 · 216 阅读 · 0 评论 -
LeetCode-23. 合并K个排序链表(Merge k Sorted Lists)
排序class Solution {public: ListNode* mergeKLists(vector<ListNode*>& lists) { ListNode* ans = new ListNode(0); ListNode* now = ans; vector<int> nums; int ...原创 2019-11-15 10:52:57 · 205 阅读 · 0 评论 -
LeetCode-22. 括号生成(Generate Parentheses)
回溯(递归)class Solution { vector<string> ans;public: vector<string> generateParenthesis(int n) { string res; getParenthesis(res, n, n); return ans; } vo...原创 2019-11-14 19:30:24 · 209 阅读 · 0 评论 -
LeetCode-21. 合并两个有序链表(Merge Two Sorted Lists)
迭代class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode ans(0); ListNode *p = &ans; while(l1 && l2) { if(l1->val ...原创 2019-11-13 21:17:57 · 265 阅读 · 0 评论 -
LeetCode-20. 有效的括号(Valid Parentheses)
栈(待优化)class Solution {public: bool isValid(string s) { stack<char> st; for(int i = 0; i < s.size(); i++) { if(s[i] == '(' || s[i] == '[' || s[i] == '{'){ ...原创 2019-11-13 21:01:38 · 184 阅读 · 0 评论 -
LeetCode-19. 删除链表的倒数第N个节点(Remove Nth Node From End of List)
双指针/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode...原创 2019-11-13 20:43:22 · 181 阅读 · 0 评论 -
LeetCode-18. 四数之和(4Sum)
双指针+排序class Solution {public: vector<vector<int>> fourSum(vector<int>& nums, int target) { vector<vector<int>> ans; sort(nums.begin(), nums.end()); ...原创 2019-11-13 20:24:18 · 181 阅读 · 0 评论 -
LeetCode-17. 电话号码的字母组合(Letter Combinations of a Phone Number)
回溯(递归)class Solution { vector<string> ans; map<int, string> m;public: vector<string> letterCombinations(string digits) { m[2] = "abc"; m[3] = "def"; m...原创 2019-11-13 19:31:46 · 289 阅读 · 0 评论 -
LeetCode-16. 最接近的三数之和(3Sum Closest)
双指针加排序class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> res; sort(nums.begin(), nums.end()); ...原创 2019-11-12 21:23:24 · 216 阅读 · 0 评论 -
LeetCode-15. 三数之和(3Sum)
2Sum思想类似(此题做优化)class Solution {public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> res; sort(nums.begin(), nums.end())...原创 2019-11-12 16:22:51 · 236 阅读 · 0 评论 -
LeetCode-14. 最长公共前缀(Longest Common Prefix)
暴力出奇迹??????(击败100%)class Solution {public: string longestCommonPrefix(vector<string>& strs) { int size = strs.size(); if(size == 0){ return ""; }else ...原创 2019-11-06 21:51:11 · 316 阅读 · 0 评论 -
LeetCode-13. 罗马数字转整数(Roman to Integer)
哈希表//哈希表class Solution {public: int romanToInt(string s) { unordered_map<string, int> nums = {{"I", 1}, {"IV",4}, {"V", 5}, {"IX", 9},{"X", 10},{"XL", 40}, {"L", 50}, {"XC", 90}, ...原创 2019-11-04 16:49:48 · 346 阅读 · 0 评论 -
LeetCode-12. 整数转罗马数字(Integer to Roman)
贪心class Solution {public: string intToRoman(int num) { int nums[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; string romans[] = {"M","CM","D","CD","C","XC","L","XL"...原创 2019-11-04 14:39:25 · 192 阅读 · 0 评论 -
LeetCode-11. 盛最多水的容器(Container With Most Water)
class Solution {public: int maxArea(vector<int>& height) { int l = 0, r = height.size() - 1; int MAX = 0; while(l < r) { MAX = max(MAX, (r - l) * (mi...原创 2019-11-04 14:34:55 · 202 阅读 · 0 评论 -
LeetCode-2.两数相加(Add Two Number)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* ad...原创 2019-09-17 17:42:28 · 170 阅读 · 0 评论 -
LeetCode-3. 无重复字符的最长子串(Longest Substring Without Repeating Characters)
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。示例 1:输入: “abcabcbb”输出: 3解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。示例 2:输入: “bbbbb”输出: 1解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。示例 3:输入: “pwwkew”输出: 3解释: 因为无重复字符的最长子串是 “wke”,所...原创 2019-09-19 18:25:42 · 173 阅读 · 0 评论 -
LeetCode-4. 寻找两个有序数组的中位数(Median of Two Sorted Arrays)
寻找两个有序数组的中位数示例 1:nums1 = [1, 3]nums2 = [2]则中位数是 2.0示例 2:nums1 = [1, 2]nums2 = [3, 4]则中位数是 (2 + 3)/2 = 2.5int n1 = nums1.size(), n2 = nums2.size(); int mid = (n1 + n2 - 1) / 2; ...原创 2019-09-19 19:34:55 · 191 阅读 · 0 评论 -
LeetCode-5.最长回文子串(Longest Palindromic Substring)
最长回文子串(动态规划)给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。示例 1:输入: “babad”输出: “bab”注意: “aba” 也是一个有效答案。示例 2:输入: “cbbd”输出: “bb”class Solution {public: string longestPalindrome(string s) ...原创 2019-09-23 10:19:26 · 160 阅读 · 0 评论 -
LeetCode-6.Z 字形变换(ZigZag Conversion)
6.Z 字形变换(ZigZag Conversion)将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。示例 1:输入: s = "LEETCODEISHIRING", numRows = 3输出: "LCIRETOESIIGEDHN"解释: L C I RE T O E S I I GE D H N示例 2:输入: s = "...原创 2019-09-24 11:29:29 · 246 阅读 · 0 评论 -
LeetCode7. 整数反转(Reverse Integer)
6.Z 字形变换(ZigZag Conversion)给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。示例 1::输入: 123输出: 321示例 2:输入: -123输出: -321示例 3:输入: 120输出: 21源代码:class Solution {public: int reverse(int x) { ...原创 2019-09-24 13:48:25 · 372 阅读 · 0 评论 -
LeetCode-8. 字符串转换整数 (atoi)(String to Integer (atoi))
8. 字符串转换整数 (atoi)(String to Integer (atoi)请你来实现一个 atoi 函数,使其能将字符串转换成整数。首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起...原创 2019-09-28 15:08:42 · 234 阅读 · 0 评论 -
LeetCode-9. 回文数(Palindrome Number)
9. 回文数(Palindrome Number)判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。示例 1:输入: 121输出: true示例 2:输入: -121输出: false解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。示例 3:输入: 10输出: false解释: 从...原创 2019-09-28 21:38:24 · 440 阅读 · 0 评论 -
LeetCode-10. 正则表达式匹配(Regular Expression Matching)
10. 正则表达式匹配(Regular Expression Matching)给你一个字符串 s 和一个字符规律 p,请你来实现一个支持 ‘.’ 和 ‘’ 的正则表达式匹配。 ‘.’ 匹配任意单个字符 '’ 匹配零个或多个前面的那一个元素所谓匹配,是要涵盖 整个 字符串 s的,而不是部分字符串。说明:s 可能为空,且只包含从 a-z 的小写字母。p 可能为空,且只包含从 a-z ...原创 2019-10-05 14:51:43 · 332 阅读 · 0 评论 -
LeetCode-1.两数之和(Two Sum )
/** 使用哈希表**/#include <iostream>#include <vector>#include <unordered_map>using namespace std;class Solution{ public: vector<int> twosum(vector<int> &nums, i...原创 2019-09-16 17:43:10 · 150 阅读 · 0 评论