LeetCode Remove Element
摘要:链接:https://oj.leetcode.com/problems/remove-element/跟上一道题很像,设置两个指针根据情况交替前进就可以。代码很简单class Solution{ public: int removeElement(int A[],int n,int elem){ ...
阅读全文
LeetCode Remove Duplicates from Sorted Array
摘要:链接:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/把有序数组中重复的元素删除,并返回删除后的数组长度。考虑直接插入排序的过程,如果遇见重复的元素,直接抛弃,判断下一个元素。由于数组本身有序,所以,时间复杂的...
阅读全文
LeetCode Generate Parentheses
摘要:链接:https://oj.leetcode.com/problems/generate-parentheses/好几天没做题了,一直在看java ee。。看来我在回溯,递归方面还是相当的弱啊。。。最后还是参考的别人的思路class Solution{ public: vector generat...
阅读全文
LeetCode Valid Parentheses
摘要:链接: https://oj.leetcode.com/problems/valid-parentheses/括号匹配。用栈class Solution{ public: bool isValid(string s) { stack sta; int n=s.size(); for(...
阅读全文
LeetCode Remove Nth Node From End of List
摘要:链接: https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/给链表添加哨兵,使用差速指针找到待删除节点的上一个节点,删除即可 。只需遍历一次链表/** * Definition for singly-linked lis...
阅读全文
LeetCode 4sum
摘要:链接:https://oj.leetcode.com/problems/4sum/一种方法是跟3sum一样,使用头尾指针交替移动,时间复杂度为O(n3),空间复杂度为O(1),注意去重class Solution{ public: vector >fourSum(vector &num,int t...
阅读全文
LeetCode Letter Combinations of a Phone Number
摘要:链接:https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/递归算法:class Solution{ public: vector letterCombinations(str...
阅读全文
LeetCode 3Sum Closest
摘要:链接:https://oj.leetcode.com/problems/3sum-closest/跟3-sum的思路是一样的。根据sum与target的大小关系,分别移动数组两端的“指针”,同时记录sum与target最小的距离class Solution {public: int three...
阅读全文
LeetCode Integer to Roman
摘要:链接: https://oj.leetcode.com/problems/integer-to-roman/题目上已经说最大出现的整型值为3999,这样就很简单了。class Solution{ public: string intToRoman(int num) { string ans;...
阅读全文
Container With Most Water
摘要:链接: https://oj.leetcode.com/problems/container-with-most-water/尺取法,从两端向中间缩进class Solution{ public: int maxArea(vector &height) //尺取法 { int L=0; ...
阅读全文
LeetCode ZigZag Conversion
摘要:链接: https://oj.leetcode.com/problems/zigzag-conversion/题意为:把字符串按如下规则排列AGMBFHLNCEIK:DJ:输出按行顺序:AGMBFHLNCEIKDJ剩下的就是通过字符的坐标计算在字符串中的位置了class Solution{ publ...
阅读全文
LeetCode 5 最长回文子串 Manacher线性算法
摘要:题目链接:https://oj.leetcode.com/problems/longest-palindromic-substring/回文串即正向反向序列都一样的连续序列 如abba,abcba...为了统一回文串的偶数情况和奇数情况,可以向串中插入不相关的字符,例如abba->#a#b#b#a#...
阅读全文
LeetCode Longest Substring Without Repeating Characters
摘要:链接: https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/字符串中最长连续不重复子序列,用JAVA中的哈希表比较方便public class Solution { public int...
阅读全文
Add Two Numbers
摘要:链接:https://oj.leetcode.com/problems/add-two-numbers/链表的大数加法/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode ...
阅读全文
LeetCode Swap Nodes in Pairs
摘要:链接:https://oj.leetcode.com/problems/swap-nodes-in-pairs/交换链表相邻的节点的位置/** * Definition for singly-linked list. * struct ListNode { * int val; * ...
阅读全文