自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(21)
  • 收藏
  • 关注

原创 2021-02-01

Day 2.1二叉树中的最大路径和题目思路定义方法lmax找到从root向左走所能得到的最大和,方法rmax找到从root向右走所能得到的最大和,再遍历每个节点作为root计算maxclass Solution { int max = -1000; public int maxPathSum(TreeNode root) { getAns(root); return max; } //遍历每个节点,计算节点值与该节点的lmax

2021-02-01 10:36:06 78 1

原创 2021-01-31

Day 1.31二叉树的最近公共祖先题目思路(1)class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { while(right(root,p)&&right(root,q))root=root.right; while(left(root,p)&&left(root,q))root=root.

2021-01-31 10:51:40 70

原创 2021-01-30

Day 1.30二叉搜索树的最近公共祖先题目思路从root开始遍历二叉树,只有当root的值在p和q的值之间时,root即为p和q的最近公共祖先代码class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q){ if(p.val>=q.val){ while(!(root.val>=q.val&&

2021-01-30 10:43:22 67

原创 2021-01-29

Day 1.29二叉搜索树中第K小的元素题目思路对二叉树遍历,找到第k小的元素递归法遍历二叉树:public List<Integer> inorderTraversal(TreeNode root) { List<Integer> ans = new ArrayList<>(); getAns(root, ans); return ans;}private void getAns(TreeNode node, List<

2021-01-29 08:25:06 63

原创 2021-01-28

Day 1.28二叉树的最大深度题目代码class Solution { public int maxDepth(TreeNode root) { if(root==null)return 0; int l = maxDepth(root.left); int r = maxDepth(root.right); if(l>=r)return l+1; else return r+1; }}结

2021-01-28 07:47:09 60

原创 2021-01-26

Day 1.26正则表达式匹配题目代码class Solution { public boolean isMatch(String s, String p) { if (p.length() == 0) { return s.length() == 0; } if (p.length() > 1 && p.charAt(1) == '*') { // p的第二个字符是 '*' //字符"*"把前面

2021-01-26 11:34:26 71

原创 2021-01-25

Day 1.25实现strStr()题目代码class Solution { public int strStr(String haystack, String needle) { int lenh = haystack.length(); int lenn = needle.length(); if(lenn==0) return 0; if(lenn>lenh) return -1

2021-01-25 08:43:29 95

原创 2021-01-24

Day 1.24翻转字符串里的单词题目代码class Solution { public String reverseWords(String s) { String[] words = s.trim().split(" "); //去除首尾空格并按空格将s拆分成字符串数组 int length = words.length; StringBuilder sb = new StringBuilder(); sb

2021-01-24 07:48:08 55

原创 2021-01-23

Day 1.23最长公共前缀题目思路先令最长公共前缀为数组中的第一个元素a,再循环遍历每个元素,使a不断减小为:a与遍历到的元素的公共前缀代码class Solution { public String longestCommonPrefix(String[] strs) { String a; if(strs.length==0)return ""; a = strs[0]; for (String str : strs

2021-01-23 08:00:28 48

原创 2021-01-22

Day 1.22字符串相乘题目思路通过模拟竖式进行运算,注意到num1[i] 和 num2[j] 的乘积对应的就是 res[i+j] 和 res[i+j+1] 这两个位置。代码class Solution { public String multiply(String num1, String num2) { if(num1.equals("0") || num2.equals("0"))return "0"; int m=num1.length();

2021-01-22 08:09:55 98 2

原创 2021-01-21

Day 1.21只出现一次的数字(Ⅱ)题目思路(1)同(Ⅰ)遍历后查找,代码完全不变思路(2)(位运算答案)引入运算符:& 按位与运算符:参与运算的两个值,如果两个相应位都为1,则该位的结果为1,否则为0 ;~ 按位取反运算符:将参与运算的数每位取反,1变0,0变1;定义变量:seen_once: 保存出现一次的元素,确保所有三个的元素作用的结果对只出现一次的元素无影响seen_twice:保存出现两次的元素定义运算规则:seen_once = ~seen_twice &

2021-01-21 07:29:16 69

原创 2021-01-20

Day 1.20只出现一次的数字题目思路(1)class Solution { public int singleNumber(int[] nums) { Arrays.sort(nums);//排序数组,之后无脑遍历 if(nums.length == 1)return nums[0]; if(nums[0] != nums [1]) return nums[0]; if(nums[nums.length-2] != num

2021-01-20 07:29:24 55

原创 2021-01-19

Day 1.19排序链表题目思路(1)把head节点值依次取出,放入数组a[]中,再对a[]排序,最后依次取出a[]的元素拼接成新链表class Solution { public ListNode sortList(ListNode head) { ListNode n = new ListNode(1),h = n; if(head == null) return null; int a[] = new int[

2021-01-19 08:01:13 78

原创 2021-01-17

Day 1.17(一)环形链表Ⅱ题目代码public class Solution { public ListNode detectCycle(ListNode head) { ListNode[] listNode = new ListNode[10000]; int count = 0; while (head != null) { for(int i = 0;i<=count;i++){

2021-01-17 07:37:35 94

原创 2021-01-16

Day 1.16环形链表题目代码public class Solution { public boolean hasCycle(ListNode head) { Set<ListNode> set = new HashSet<>(); while (head != null) { if (set.contains(head)) return true; set.add(head); head = head.next;

2021-01-16 08:27:53 69

原创 2021-01-15

Day 1.15合并两个有序链表题目思路(1)class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode listNode = new ListNode(0); ListNode head = listNode; while(l1 != null && l2 != null) { if(l1.val < l2.val) { head.n

2021-01-15 11:16:34 69

原创 2021-01-13

Day 1.13最长回文子串题目思路(1)class Solution {public: string longestPalindrome(string s) { int c,m = 0,l = 0,j = 0,k = 0,g = 0; string u; for(int i = 0;s[i]!='\0';i++){ if(s[i]==s[i+1]){ c = 0; for(j = i,k = i+1;j>

2021-01-13 08:50:44 67

原创 2021-01-12

Day 12最小栈题目思路(1)class MinStack {public: /** initialize your data structure here. */ int a[10000];int i=0,j=0; MinStack() { } void push(int x) { a[i] = x; i++; } void pop() { a[i-1] = 0;

2021-01-12 10:56:40 71

原创 2021-01-11

Day 1.11有效的括号题目思路(1)class Solution {public: bool isValid(string s) { int a = s.size(); int b = 0; if(a % 2 == 1)return false; else if(a == 0)return true; else{ for(int i = 0; i < a-1 ; ++i){

2021-01-11 17:08:03 100

原创 2021-01-10

Day 1.10回文数题目代码class Solution {public: bool isPalindrome(int x) { double sum=0,a=0,b=x; if(x<0) return false; while(x){ sum=sum*10+x%10; x=x/10; } if(sum==b) return true; else return false; }};结果笔记结合整数反转可发现只需判断整数反转后是否

2021-01-10 09:46:28 73

原创 2021-01-09

Day 1.9整数反转题目代码class Solution {public: int reverse(int x) { long long sum=0,a[100],k=0,p=1; if(x==0)return 0; for(int i = 0;x!=0;i++){ a[i] = x % 10; x = x / 10; k=i; } for(int i = k;i>=0;i--){ sum += a[i]*p; p*=10;

2021-01-09 21:45:06 109

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除