![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
M阳光
java
展开
-
LeetCode Word Break
链接: https://oj.leetcode.com/problems/word-break/dp[i] 表示s[原创 2014-11-11 22:49:05 · 376 阅读 · 0 评论 -
LeetCode Merge Two Sorted Lists
链接: https://oj.leetcode.com/problems/merge-two-sorted-lists/题目要去把两个有序的链表合并,并且合并后的链表依然有序./** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; *原创 2014-12-24 22:31:29 · 378 阅读 · 0 评论 -
LeetCode Best Time to Buy and Sell Stock II
链接: https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/class Solution{ public: int maxProfit(vector &prices) { if(prices.size()==0) return 0; int ans=0; for(i原创 2014-12-10 14:07:12 · 353 阅读 · 0 评论 -
LeetCode Maximum Product Subarray
链接: https://oj.leetcode.com/problems/maximum-product-subarray/求最大连续序列的乘积因为偶数个负数相乘为正数,且任何数乘0等于0.所以在计算的过程中需要维护两个数组,一个记录到当前位的最大乘积,一个记录到当前位的最小乘积..并初始化为A[i]class Solution{ public: int maxPr原创 2014-12-27 14:03:35 · 471 阅读 · 0 评论 -
LeetCode 3sum
链接: https://oj.leetcode.com/problems/3sum/2sum:尺取法 通过a+b于tar3sum:即对0~n个数分别做2sum...原创 2014-12-12 17:13:32 · 412 阅读 · 0 评论 -
LeetCode Majority Element
链接: https://oj.leetcode.com/problems/majority-element/LeetCode给的这道题的解答真的不错:Runtime: O(n2) — Brute force solution: Check each element if it is the majority element.Runtime: O(n), Space: O(n) —原创 2015-01-16 20:02:16 · 442 阅读 · 0 评论 -
LeetCode Minimum Path Sum
链接: https://oj.leetcode.com/problems/minimum-path-sum/简单的动态规划.[i][j]表示到达位置(i,j)时的最小sumpublic class Solution{ public int minPathSum(int[][] grid) { int n=grid.length; int m=grid[0原创 2014-12-13 12:32:41 · 408 阅读 · 0 评论 -
LeetCode Letter Combinations of a Phone Number
链接:https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/递归算法:class Solution{ public: vector<string> letterCombinations(string digits) { vector<string>原创 2015-02-06 01:37:29 · 671 阅读 · 0 评论 -
LeetCode Swap Nodes in Pairs
链接:https://oj.leetcode.com/problems/swap-nodes-in-pairs/交换链表相邻的节点的位置/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int原创 2015-01-18 14:24:10 · 390 阅读 · 0 评论 -
LeetCode Find Peak Element
链接: https://oj.leetcode.com/problems/find-peak-element/题目要求复杂度在对数级以内对数组下标进行二分搜索,public class Solution{ public int findPeakElement(int [] num) { int l=0,r=num.length-1; int mid=(l+r)/2原创 2014-12-13 20:02:28 · 517 阅读 · 0 评论 -
Add Two Numbers
链接:https://oj.leetcode.com/problems/add-two-numbers/链表的大数加法/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) :原创 2015-01-20 20:08:30 · 431 阅读 · 0 评论 -
LeetCode Valid Parentheses
链接: https://oj.leetcode.com/problems/valid-parentheses/括号匹配。用栈class Solution{ public: bool isValid(string s) { stack sta; int n=s.size(); for(int i=0;i<n;i++) { char t=s[原创 2015-02-14 17:01:36 · 591 阅读 · 0 评论 -
LeetCode 4sum
链接:https://oj.leetcode.com/problems/4sum/一种方法是跟3sum一样,使用头尾指针交替移动,时间复杂度为O(n3),空间复杂度为O(1),注意去重class Solution{ public: vector >fourSum(vector &num,int target) { vector > ans; sort(n原创 2015-02-10 00:19:42 · 521 阅读 · 0 评论 -
LeetCode Longest Substring Without Repeating Characters
链接: https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/字符串中最长连续不重复子序列,用JAVA中的哈希表比较方便public class Solution { public int lengthOfLongestSubstring(String s) {原创 2015-01-22 23:50:34 · 475 阅读 · 0 评论 -
LeetCode Generate Parentheses
链接:https://oj.leetcode.com/problems/generate-parentheses/好几天没做题了,一直在看java ee。。看来我在回溯,递归方面还是相当的弱啊。。。最后还是参考的别人的思路class Solution{ public: vector generateParenthesis(int n) { vect原创 2015-02-20 19:43:37 · 514 阅读 · 0 评论 -
LeetCode Remove Duplicates from Sorted Array
链接:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/把有序数组中重复的元素删除,并返回删除后的数组长度。考虑直接插入排序的过程,如果遇见重复的元素,直接抛弃,判断下一个元素。由于数组本身有序,所以,时间复杂的为O(n).class Solution{ public:原创 2015-02-23 23:42:37 · 485 阅读 · 0 评论 -
LeetCode Remove Nth Node From End of List
链接: https://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/给链表添加哨兵,使用差速指针找到待删除节点的上一个节点,删除即可 。只需遍历一次链表/** * Definition for singly-linked list. * struct ListNode { * int va原创 2015-02-12 00:58:46 · 501 阅读 · 0 评论 -
LeetCode Remove Element
链接:https://oj.leetcode.com/problems/remove-element/跟上一道题很像,设置两个指针根据情况交替前进就可以。代码很简单class Solution{ public: int removeElement(int A[],int n,int elem){ int p1=-1,p2=0; while(p2<n){原创 2015-02-26 14:00:50 · 511 阅读 · 0 评论 -
LeetCode Permutations
链接: https://oj.leetcode.com/problems/permutations/每次从num中选择一个数添加到结果中,并用used标记用过的数.public class Solution { private boolean used[]; private List > ans; private List tans ; public List > p原创 2014-12-08 22:08:58 · 454 阅读 · 0 评论 -
LeetCode 3Sum Closest
链接:https://oj.leetcode.com/problems/3sum-closest/跟3-sum的思路是一样的。根据sum与target的大小关系,分别移动数组两端的“指针”,同时记录sum与target最小的距离class Solution {public: int threeSumClosest(vector &num,int target)原创 2015-02-02 22:33:20 · 479 阅读 · 0 评论 -
LeetCode Gray Code
链接: https://oj.leetcode.com/problems/gray-code/格雷码 见维基百科 http://zh.wikipedia.org/wiki/%E6%A0%BC%E9%9B%B7%E7%A0%81原创 2014-11-15 21:05:59 · 604 阅读 · 0 评论 -
LeetCode Palindrome Partitioning
链接: https://oj.leetcode.com/problems/palindrome-partitioning/原创 2014-11-02 11:47:42 · 360 阅读 · 0 评论 -
LeetCode Gas Station
链接: https://oj.leetcode.com/problems/gas-station/wangs原创 2014-11-02 21:46:44 · 479 阅读 · 0 评论 -
LeetCode Find Minimum in Rotated Sorted Array
链接: https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/旋转shuzh原创 2014-11-20 20:16:08 · 400 阅读 · 0 评论 -
LeetCode Two Sum
链接: https://oj.leetcode.com/problems/two-sum/在一个数组中找出两个原创 2014-11-22 13:15:29 · 434 阅读 · 0 评论 -
LeetCode Partition List
链接: https://oj.leetcode.com/problems/partition-list/双重指针....../** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : v原创 2014-11-27 17:26:53 · 366 阅读 · 0 评论 -
LeetCode Maximum Subarray
链接: https://oj.leetcode.com/problems/maximum-subarray/最大li原创 2014-11-23 14:41:24 · 451 阅读 · 0 评论 -
LeetCode Best Time to Buy and Sell Stock
链接: https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/刚开始用dp写的...时间复杂度是O(n^2) 果断超时了.下面是TLE的代码class Solution{ public: int maxProfit(vector &prices) { int dp[prices.size(原创 2014-11-29 12:20:13 · 350 阅读 · 0 评论 -
LeetCode Unique Binary Search Trees
链接: https://oj.leetcode.com/problems/unique-binary-search-trees/dp[i]表示当n为i时的BST数量..递推关系如下图这样,就把n=4分割为n=3,n=1,n=2.用dp数组记录结果代码:class Solution{ public: int numTrees(int n)原创 2014-11-30 13:14:45 · 393 阅读 · 0 评论 -
LeetCode Path Sum
链接: https://oj.leetcode.com/problems/path-sum/shen原创 2014-11-24 22:25:40 · 469 阅读 · 0 评论 -
LeetCode Single Number II
链接: https://oj.leetcode.com/problems/single-number-ii/虽然知道这个题肯定跟位运算,异或有关...但实在没想出来怎么解决class Solution {public: int singleNumber(int A[], int n) { int l=0,r=1; sort(A,A+n);原创 2014-12-14 16:41:47 · 390 阅读 · 0 评论 -
LeetCode Sum Root to Leaf Numbers
链接: https://oj.leetcode.com/problems/sum-root-to-leaf-numbers/dfs..sum保存中间结果.每计算完一条路径,就更新ans/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; *原创 2014-12-02 19:37:15 · 376 阅读 · 0 评论 -
LeetCode ZigZag Conversion
链接: https://oj.leetcode.com/problems/zigzag-conversion/题意为:把字符串按如下规则排列A G MB FH LNCE IK :D J :输出按行顺序原创 2015-01-28 01:34:23 · 476 阅读 · 0 评论 -
LeetCode Reorder List
链接: https://oj.leetcode.com/problems/reorder-list/空间复杂度为O(n),时间复杂度为 O(n)的代码:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * Lis原创 2014-12-04 13:55:13 · 339 阅读 · 0 评论 -
Container With Most Water
链接: https://oj.leetcode.com/problems/container-with-most-water/尺取法,从两端向中间缩进class Solution{ public: int maxArea(vector &height) //尺取法 { int L=0; int R=height.size()-1; int a原创 2015-01-29 15:02:53 · 409 阅读 · 0 评论 -
LeetCode Rotate List
链接: https://oj.leetcode.com/problems/rotate-list//** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * va原创 2014-12-05 21:39:24 · 406 阅读 · 0 评论 -
LeetCode Integer to Roman
链接: https://oj.leetcode.com/problems/integer-to-roman/题目上已经说最大出现的整型值为3999,这样就很简单了。class Solution{ public: string intToRoman(int num) { string ans; char str_num[8]; sprintf(str_n原创 2015-01-31 20:42:25 · 399 阅读 · 0 评论 -
LeetCode 5 最长回文子串 Manacher线性算法
题目链接:https://oj.leetcode.com/problems/longest-palindromic-substring/回文串即正向反向序列都一样的连续序列 如abba,abcba...为了统一回文串的偶数情况和奇数情况,可以向串中插入不相关的字符,例如abba->#a#b#b#a#, abcba->#a#b#c#b#a#建立数组arr[]记录主串中以第原创 2015-01-26 12:19:47 · 664 阅读 · 0 评论