摘要: 题目链接 方法一: class Solution { public String reverseWords(String s) { //先将字符串用空格划分成字符串数组 String[] strs = s.split(" "); StringBuffer buffer = new StringBuf 阅读全文
posted @ 2022-02-09 19:11 蹇爱黄 阅读(35) 评论(0) 推荐(0)
摘要: 题目链接 class Solution { public void reverseString(char[] s) { int l = 0; int r = s.length - 1; while(l<r){ s[l] ^= s[r];//s[l] = a^b s[r] ^= s[l];//s[r] 阅读全文
posted @ 2022-02-09 18:21 蹇爱黄 阅读(28) 评论(0) 推荐(0)
摘要: 题目链接 class Solution { public boolean canWinNim(int n) { return n % 4 != 0; } } 阅读全文
posted @ 2022-02-09 15:55 蹇爱黄 阅读(82) 评论(0) 推荐(0)
摘要: 题目链接 class Solution { public int[] productExceptSelf(int[] nums) { int n = nums.length; int[] ans = new int[n]; int left = 1; //让每个数等于自己左边所有数的乘积 for(i 阅读全文
posted @ 2022-02-09 15:41 蹇爱黄 阅读(34) 评论(0) 推荐(0)
摘要: 题目链接 阅读全文
posted @ 2022-02-09 15:22 蹇爱黄 阅读(34) 评论(0) 推荐(0)
摘要: 题目链接 注意p,q必然存在树内, 且所有节点的值唯一!!! 递归思想, 对以root为根的(子)树进行查找p和q, 如果root == null || p || q 直接返回root 表示对于当前树的查找已经完毕, 否则对左右子树进行查找, 根据左右子树的返回值判断: 1. 左右子树的返回值都不为 阅读全文
posted @ 2022-02-09 15:10 蹇爱黄 阅读(41) 评论(0) 推荐(0)
摘要: 题目链接 //二叉搜索树的特性 class Solution { TreeNode res = null; public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { lca(root,p,q); ret 阅读全文
posted @ 2022-02-09 15:00 蹇爱黄 阅读(33) 评论(0) 推荐(0)
摘要: 题目链接 class Solution { public boolean isPowerOfTwo(int n) { if(n<=0) return false; //8的二进制1000 7的二进制0111 做个与操作结果为零 if((n&n-1)==0) return true; else ret 阅读全文
posted @ 2022-02-09 14:51 蹇爱黄 阅读(28) 评论(0) 推荐(0)
摘要: 题目链接 二叉树特点左子树小于根节点根节点小于右子树 class Solution { public int kthSmallest(TreeNode root, int k) { //左子树节点个数 int leftN = findChild(root.left); //如果k等于左子树节点加一, 阅读全文
posted @ 2022-02-09 14:43 蹇爱黄 阅读(30) 评论(0) 推荐(0)
摘要: 题目链接 Set实现 Map实现 class Solution { public boolean containsDuplicate(int[] nums) { int len = nums.length; Map<Integer,Integer> map = new HashMap<>(len); 阅读全文
posted @ 2022-02-09 03:40 蹇爱黄 阅读(25) 评论(0) 推荐(0)
摘要: 题目链接 先用库函数试一下 快排:从两边往中间走,找个参照值,左边的大于参照值,右边的等于参照值时就交换这两个数。 class Solution { public int findKthLargest(int[] nums, int k) { qsort(nums, 0, nums.length - 阅读全文
posted @ 2022-02-09 02:57 蹇爱黄 阅读(36) 评论(0) 推荐(0)
摘要: 题目链接 真希望所有题都这么easy class Solution { public ListNode reverseList(ListNode head) { ListNode pre = null; ListNode cur = head; while(cur != null){ //temp保 阅读全文
posted @ 2022-02-09 02:07 蹇爱黄 阅读(34) 评论(0) 推荐(0)
摘要: 题目链接 既然个数大于一般那就先sort,再取中间吧 但是面试官可能会想要其答案 class Solution { public int majorityElement(int[] nums) { int count = 1,maj = nums[0]; for(int i = 1;i < nums 阅读全文
posted @ 2022-02-09 02:00 蹇爱黄 阅读(47) 评论(0) 推荐(0)
摘要: 题目链接 public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if(headA == null || headB == null) return null; Lis 阅读全文
posted @ 2022-02-09 01:32 蹇爱黄 阅读(34) 评论(0) 推荐(0)
摘要: 这道题评论区的大佬真是各抒己见啊,我也不知道用栈来实现对不对,但是看到一个非常棒的用栈来实现的方法 class MinStack { private int min = Integer.MAX_VALUE; private Stack<Integer> stack; public MinStack( 阅读全文
posted @ 2022-02-09 01:13 蹇爱黄 阅读(33) 评论(0) 推荐(0)
摘要: 题目链接 递归排序三部曲:①快慢指针找中点;②递归调用mergeSort; ③合并两个链表 归并 class Solution { public ListNode sortList(ListNode head){ return mergeSort(head); } //归并排序 private Li 阅读全文
posted @ 2022-02-09 00:34 蹇爱黄 阅读(35) 评论(0) 推荐(0)