![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
狠人王
怀着一颗热情的心,追随自己的梦想!
展开
-
【leetcode记录帖】【7】对称二叉树
https://leetcode-cn.com/problems/symmetric-tree/submissions/分析:要使得整个二叉树是镜像对称的,则必须要求每个子结构:左节点的左孩子等于右节点的右孩子,右节点的左孩子等于左节点的右孩子,符合递归寻找最小子结构的性质。所以使用DFS。递归终止条件是:1、两节点为空2、两节点有一个为空3、两节点的值不相等/** * Definition for a binary tree node. * struct Tree..原创 2020-06-06 12:35:07 · 248 阅读 · 0 评论 -
【leetcode记录帖】【6】Merge Sorted Array
https://leetcode-cn.com/problems/merge-sorted-array/1、利用arraycopy函数合并后排序,官方的解答太秀了。我确实没想起来这样做。时间复杂度比较大。class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { System.arraycopy(nums2,0,nums1,m,n);原创 2020-06-05 13:49:54 · 176 阅读 · 0 评论 -
【leetcode记录帖】【5】Maxmum Subarray
最大子序和问题。要求连续数组和最大。1、纯暴力,枚举区间端点,三重循环O(n^3)超出时间限制class Solution { public int maxSubArray(int[] nums) { int maxn=Integer.MIN_VALUE; if(nums.length==1) return nums[0]; int sum=0; for(int i=0;i<nums.length;i++)原创 2020-06-04 11:21:24 · 176 阅读 · 2 评论 -
【LeetCode记录帖】【4】Remove Duplicates from Sorted Array
题目链接https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/1、反正是vector嘛,直接erase删除就完了,当然这可能不是本题最好的解法,但绝对是最无脑的。erase函数需要注意有返回值,返回的是删除元素的下一个位置。class Solution {public: int removeDuplicates(vector<int>& nums) { for(auto原创 2020-06-03 10:42:50 · 720 阅读 · 1 评论 -
【LeetCode记录帖】【3】Merge Two Sorted Lists
题目链接:https://leetcode-cn.com/problems/merge-two-sorted-lists/1、递归解法。思路:每次mergeTwoLists出来的子结构都是当前最小的。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullpt...原创 2020-06-03 09:53:46 · 119 阅读 · 1 评论 -
【LeetCode记录帖】【2】Valid Parentheses
题目连接https://leetcode-cn.com/problems/valid-parentheses/解法:利用压栈的方法,将栈的头部和相对于字符串的后一个字符进行比较,若匹配就头部出栈,不匹配就入栈,最终如果栈为空即达到有效串的要求。特判一下空串。class Solution {public: bool isValid(string s) { if(s=="") return true; stack<int> sta;原创 2020-06-03 09:11:13 · 117 阅读 · 1 评论 -
【leetcode记录帖】【1】TwoSum
题目链接:https://leetcode-cn.com/problems/two-sum/1、暴力法:双重循环。复杂度O(n)。class Solution { public int[] twoSum(int[] nums, int target) { int a[]=new int[2]; for(int i=0;i<nums.length;i++) { for(int j=i+1;j<nums.原创 2020-06-02 23:08:01 · 110 阅读 · 0 评论 -
135. candy(贪心法)
There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least one cand...原创 2019-08-03 17:26:41 · 103 阅读 · 0 评论