欢迎使用皮肤 Geek|

蹇爱黄

园龄:4年1个月粉丝:3关注:1

02 2022 档案

剑指offer 6~9
摘要:##6.剑指 Offer 11. 旋转数组的最小数字 二分查找 class Solution { public int minArray(int[] numbers) { int l = 0, r = numbers.length - 1; while (l < r) { int mid = ((r
31
0
0
剑指offer 1~5
摘要:##1.用两个栈实现队列 用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 ) class CQueue { //全局声明两个栈
25
0
0
腾讯五十题 反转字符串中的单词III
摘要:题目链接 方法一: class Solution { public String reverseWords(String s) { //先将字符串用空格划分成字符串数组 String[] strs = s.split(" "); StringBuffer buffer = new StringBuf
22
0
0
腾讯五十题 No.46 反转字符串
摘要:题目链接 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]
16
0
0
腾讯五十题 No.45 Nim游戏
摘要:题目链接 class Solution { public boolean canWinNim(int n) { return n % 4 != 0; } }
18
0
0
腾讯五十题 No.44 除自身以外数组的乘积
摘要:题目链接 class Solution { public int[] productExceptSelf(int[] nums) { int n = nums.length; int[] ans = new int[n]; int left = 1; //让每个数等于自己左边所有数的乘积 for(i
24
0
0
腾讯五十题 No.42 二叉树的最近公共祖先
摘要:题目链接 注意p,q必然存在树内, 且所有节点的值唯一!!! 递归思想, 对以root为根的(子)树进行查找p和q, 如果root == null || p || q 直接返回root 表示对于当前树的查找已经完毕, 否则对左右子树进行查找, 根据左右子树的返回值判断: 1. 左右子树的返回值都不为
24
0
0
腾讯五十题No.42 二叉搜索树的最近公共祖先
摘要:题目链接 //二叉搜索树的特性 class Solution { TreeNode res = null; public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { lca(root,p,q); ret
20
0
0
腾讯五十题 No.41 2的幂
摘要:题目链接 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
19
0
0
腾讯五十题No.40二叉树搜索中第k小的元素
摘要:题目链接 二叉树特点左子树小于根节点根节点小于右子树 class Solution { public int kthSmallest(TreeNode root, int k) { //左子树节点个数 int leftN = findChild(root.left); //如果k等于左子树节点加一,
19
0
0
腾讯五十题 No.39 存在重复元素
摘要:题目链接 Set实现 Map实现 class Solution { public boolean containsDuplicate(int[] nums) { int len = nums.length; Map<Integer,Integer> map = new HashMap<>(len);
17
0
0
腾讯五十题 No.38 数组中第k个最大元素
摘要:题目链接 先用库函数试一下 快排:从两边往中间走,找个参照值,左边的大于参照值,右边的等于参照值时就交换这两个数。 class Solution { public int findKthLargest(int[] nums, int k) { qsort(nums, 0, nums.length -
27
0
0
腾讯五十题 No.37 反转链表
摘要:题目链接 真希望所有题都这么easy class Solution { public ListNode reverseList(ListNode head) { ListNode pre = null; ListNode cur = head; while(cur != null){ //temp保
19
0
0
腾讯五十题 No.36 多数元素
摘要:题目链接 既然个数大于一般那就先sort,再取中间吧 但是面试官可能会想要其答案 class Solution { public int majorityElement(int[] nums) { int count = 1,maj = nums[0]; for(int i = 1;i < nums
31
0
0
腾讯五十题 No.35 相交链表
摘要:题目链接 public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { if(headA == null || headB == null) return null; Lis
17
0
0
腾讯五十题 No.34
摘要:这道题评论区的大佬真是各抒己见啊,我也不知道用栈来实现对不对,但是看到一个非常棒的用栈来实现的方法 class MinStack { private int min = Integer.MAX_VALUE; private Stack<Integer> stack; public MinStack(
20
0
0
腾讯五十题 No.33 排序链表
摘要:题目链接 递归排序三部曲:①快慢指针找中点;②递归调用mergeSort; ③合并两个链表 归并 class Solution { public ListNode sortList(ListNode head){ return mergeSort(head); } //归并排序 private Li
24
0
0
腾讯五十题 No.32 LRU缓存
摘要:感觉贼难啊 在Java里边比较简单 class LRUCache extends LinkedHashMap<Integer, Integer>{ private int capacity; public LRUCache(int capacity){ super(capacity,0.75F,tr
38
0
0
腾讯五十题No.31 环形链表II
摘要:题目链接 判断环的入口,需要先判断有没有环,在寻找环的入口,找入口时需要重新定义两个”指针“,一个指向头节点,一个指向当前fast,让他们前进速度相同,他们想入的点即为入口。 public class Solution { public ListNode detectCycle(ListNode h
37
0
0
腾讯五十题 No.30
摘要:题目链接 因为需要用到fast.next.next所以必须确保fast!=null && fast.next!=null,才能走到fast = fast.next.next; public class Solution { public boolean hasCycle(ListNode head)
24
0
0
腾讯五十题 No.29只出现一次的数字
摘要:不开辟新的空间,一般暗示用位运算或者异或解题 class Solution { public int singleNumber(int[] nums) { int single = 0; for(int num:nums){ single ^= num; } return single; } } 大
18
0
0
腾讯五十题 No.28 二叉树中的最大路径和
摘要:题目链接 class Solution { private int res = Integer.MIN_VALUE; public int maxPathSum(TreeNode root) { getMax(root); return res; } private int getMax(TreeN
26
0
0
腾讯五十题 No.27 买卖股票的最佳时机II
摘要:题目链接 动态规划 考虑到「不能同时参与多笔交易」,因此每天交易结束后只可能存在手里有一支股票或者没有股票的状态。 定义状态 dp[i][0] 表示第 i 天交易完后手里没有股票的最大利润,dp[i][1] 表示第 i 天交易完后手里持有一支股票的最大利润(i 从 0 开始)。 考虑 dp[i][0
18
0
0
腾讯五十题 No.26 买卖股票的最佳时机
摘要:题目链接 class Solution { public int maxProfit(int[] prices) { if(prices.length <= 1) return 0; int low = prices[0],res = 0; for(int i=1;i<prices.length;i
19
0
0
腾讯五十题 No.25 二叉树的最大深度
摘要:题目链接 递归 class Solution { public int maxDepth(TreeNode root) { if(root==null) return 0; //左子树递归 int left = maxDepth(root.left); //右子树递归 int right = max
19
0
0
腾讯五十题 No.24 格雷编码
摘要:题目链接 我直接跪 我直接背
14
0
0
腾讯五十题 No.23 合并两个有序数组
摘要:题目链接 思路 标签:从后向前数组遍历 因为 nums1 的空间都集中在后面,所以从后向前处理排序的数据会更好,节省空间,一边遍历一边将值填充进去 设置指针 r1 和 r2 分别指向 nums1 和 nums2 的有数字尾部,从尾部值开始比较遍历,同时设置指针 len 指向 nums1 的最末尾,每
23
0
0
腾讯五十题No.22 子集
摘要:题目链接 看来还得读写几个题才会有感觉 力扣的大佬是真的多 class Solution { List<List<Integer>> res; List<Integer> path; public List<List<Integer>> subsets(int[] nums) { res = new
25
0
0
腾讯五十题 No.21 爬楼梯
摘要:题目链接 动态规划找到递推公式,确定好开始两项的值 class Solution { public int climbStairs(int n) { if(n<=2) return n; int[] dp = new int[n+1]; //数组从第二个元素开始,第一个位置dp[0]不用,这样可以跟
23
0
0
腾讯五十题 No.20 不同路径
摘要:题目链接 递归 class Solution { public int uniquePaths(int m, int n) { return dfs(new HashMap<Pair,Integer>(),0,0,m,n); } private int dfs(Map<Pair,Integer> c
28
0
0
腾讯五十题 No.19旋转链表
摘要:题目链接 class Solution { public ListNode rotateRight(ListNode head, int k) { if(head == null) return null; int len = 1; ListNode tail = head; while(tail.
20
0
0
腾讯五十题 No.18 螺旋矩阵II
摘要:题目链接 这个是往里边填,并且是个规整的正方形矩阵 class Solution { public int[][] generateMatrix(int n) { int l = 0, r = n - 1, t = 0, b = n - 1; int[][] mat = new int[n][n];
24
0
0
腾讯五十题No.17 螺旋矩阵
摘要:题目链接 二刷发现已经忘光光了。。。。 class Solution { public List<Integer> spiralOrder(int[][] matrix) { List<Integer> ans = new ArrayList<>(); //二维数组长和宽 int n = matri
26
0
0
腾讯五十题No.16 最大子数组和
摘要:题目链接 class Solution { public int maxSubArray(int[] nums) { //sum每轮暂时存值, int sum=0,res=nums[0]; for(int num:nums){ //sum为sum+下一个数组元素,与下一个数组元素之间的最大值 sum
18
0
0
腾讯五十题 No.16 全排列
摘要:题目链接 回溯+递归 0ms class Solution { public List<List<Integer>> permute(int[] nums) { //1.初始化一个动态数组存储可能的数组 List<List<Integer>> res = new ArrayList<>(); //2
22
0
0
腾讯五十题 No.15 字符串相乘
摘要:题目链接 解题必看:0-9里边任意两数相乘,乘积最大也只有两位 num1的第i位(高位从0开始)和num2的第j位相乘的结果在乘积中的位置是[i+j, i+j+1] 例: 123 * 45, 123的第1位 2 和45的第0位 4 乘积 08 存放在结果的第[1, 2]位中 index: 0 1 2
32
0
0
腾讯五十题 No.14 搜索旋转排序数组
摘要:题目链接 class Solution { public int search(int[] nums, int target) { int len = nums.length; int l = 0,r = len - 1; while(l<=r){ //二分法 int mid = (l + r)/2
22
0
0
腾讯五十题 No.13 删除有序数组中的重复项
摘要:题目链接 class Solution { public int removeDuplicates(int[] nums) { int fast=1,slow=0; while(fast<nums.length){ //如果快慢指针上的元素不相等就将该元素 if(nums[fast] != nums
21
0
0
腾讯五十题 No.12 合并两个有序链表
摘要:题目链接 0ms class Solution { public ListNode mergeTwoLists(ListNode list1, ListNode list2) { //如果某个list为空就直接返回零一list if(list1==null) return list2; if(lis
20
0
0
腾讯五十题 No.11 有效的括号
摘要:题目链接 class Solution { public boolean isValid(String s) { //初始化一个栈 Stack<Character> stack = new Stack<Character>(); //利用栈的先进后出的特性去检验括号能不能配对 for(char c
27
0
0
腾讯五十题No.10 最接近的三数之和
摘要:题目链接 class Solution { public int threeSumClosest(int[] nums, int target) { int res = Integer.MAX_VALUE; Arrays.sort(nums); int sum = 0; for(int i = 0;
31
0
0
腾讯五十题 No.9
摘要:题目链接 刚开始感觉哇这代码好长,还总写漏,但是写了几遍后感觉好了很多 class Solution { public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> result = new ArrayList<>();
24
0
0
腾讯五十题 No.8 最长公共子串
摘要:题目链接 string.startsWith(s):一个字符串是不是以s开始 0ms class Solution { public String longestCommonPrefix(String[] strs) { if(strs == null || strs.length == 0) re
18
0
0
腾讯五十题 No.7 盛水最多的容器
摘要:题目链接 左右指针,不知道为什么挺慢的4ms public class Solution { public int maxArea(int[] height) { //定义左右指针 int l = 0,r = height.length-1; int ans = 0; while(l<r){ //找
18
0
0
腾讯五十题 No.6 回文数
摘要:题目链接 easy题,没有奇奇怪怪的条件限制 class Solution{ public boolean isPalindrome(int x){ if(x<0) return false; //if(x<10) return true; int num = x; int ans = 0; whi
19
0
0
腾讯五十题No.5 字符串转换整数
摘要:题目链接 1.去除前导空格 2.第一个字符是数字吗 3.第一个字符是'+'吗 4.第一个字符是'-'吗 5.后边的每个字符是数字吗 class Solution { public int myAtoi(String s) { char[] chars = s.toCharArray(); int n
21
0
0
腾讯五十题No.4整数反转(溢出!溢出!溢出!)
摘要:题目链接 符合题意的解 class Solution { public int reverse(int x) { int ans = 0; while (x != 0) { int pop = x % 10; if (ans > Integer.MAX_VALUE / 10 || (ans == I
23
0
0
腾讯五十题No.3(二刷再看)
摘要:题目链接 大佬的解题思路(身为菜狗的我压根没想这么多) class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int m = nums1.length; int n = nums2.leng
19
0
0
腾讯五十题No.2
摘要:题目链接 2ms 中心扩散:每遍历一个元素就左右扩展 class Solution { public String longestPalindrome(String s) { if(s==null || s.length()==0) return ""; //保存起止位置 int[] range =
16
0
0
腾讯五十题No.1
摘要:力扣题目链接 class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode root = new ListNode(0); ListNode cursor = root; //保存进位值 int
23
0
0
剑指offer 乘积小于K的子数组
摘要:力扣题目链接 class Solution { public int numSubarrayProductLessThanK(int[] nums, int k) { if(k < 2) return 0; int i = 0,j = 0; int ans = 0; int x = 1; while
23
0
0
剑指offer 和大于等于target的最短子数组
摘要:力扣题目 解题思路: 双指针(有点滑动窗口的思想) 1.初始话最短数组的长度 min = Integer.MAX_VALUE 2.初始化一个数组的和 sum = 0; 3.定义变量(窗口的前后指针)i=0 j=0 j为快指针 4.开始遍历,当sum的值大于等于target时更新min 5.缩小窗口范
38
0
0
剑指offer 最接近的三数之和
摘要:力扣题目链接 6ms有点长也不知道咋优化 class Solution { public int threeSumClosest(int[] nums, int target) { int n = nums.length; //先排个序 Arrays.sort(nums); //记录第一个值 int
24
0
0
剑指offer 最长公共前缀Java
摘要:力扣题目链接 第一种1ms class Solution { public String longestCommonPrefix(String[] strs) { //求出字符数组长度,方便后边遍历 int n = strs.length; if(n==0) return ""; //初始化返回值
20
0
0
剑指offer 数组中和为零的三个数Java
摘要:力扣题目链接 class Solution { public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> res = new ArrayList<>(); int n = nums.length; //剪枝,当数组小于
27
0
0
剑指offer 排序数组中的两数之和
摘要:力扣题目链接 跟两数之和好像啊 于是写了一个HashMap class Solution { public int[] twoSum(int[] numbers, int target) { int[] a = new int[2]; Map<Integer,Integer> map = new H
28
0
0
剑指offer 单词长度的最大乘积Java--二进制状态压缩
摘要:力扣题目链接 感觉自己是真的菜。。。每次都看大佬题解 public class Solution { public int maxProduct(String[] words) { //字符数组的长度 int len = words.length; //定义一个长度为字符数组长度的新数组 int[]
29
0
0
剑指offer 只出现一次的数字Java
摘要:力扣题目链接 不知道别的方法怎么样,只能想出HashMap class Solution { public int singleNumber(int[] nums) { Map<Integer,Integer> map = new HashMap<Integer,Integer>(); for(in
24
0
0
剑指offer 前n个数二进制中1的个数
摘要:力扣题目链接 位运算 class Solution { public int[] countBits(int n) { int[] nums = new int[n+1]; for(int i=0;i<=n;++i){ for(int j=0;j<32;++j){ nums[i] += (i>>j)
21
0
0
剑指offer 二进制加法Java
摘要:题目链接 在代码中需要注意的有: 本题给出的二进制数字是字符串形式,不可以转化成 int 型,因为可能溢出; 两个「加数」的字符串长度可能不同; 在最后,如果进位 carry 不为 0,那么最后需要计算进位; 向结果字符串 res 拼接的顺序是向后拼接,返回时需要把 res 反转 。 代码中的巧妙之
28
0
0
剑指offer 整数的除法Java
摘要:力扣题目链接 遇到代替乘除或者优化乘除,首先考虑位运算 这道题需要注意Integer.MIN_VALUE 直接用减法来替代会超出时间限制 class Solution { public int divide(int a, int b) { if (b == 0) return 0; if (a ==
44
0
0
剑指offer 替换空格Java
摘要:力扣题目链接 希望面试给我这道题 肯定不是利用String类中的replaceAll方法 class Solution { public String replaceSpace(String s) { return s.replaceAll(" ", "%20"); } } 方法一:字符数组 由于每
26
0
0
剑指offer 复杂链表赋值Java
摘要:题目链接 1.普通链表的定义: class Node{ int val; Node next; public Node(int val){ this.val = val; this.next = null; } 2.本题的复杂链表节点定义: class Node{ int val; Node nex
112
0
0
剑指offer Java 反转链表
摘要:题目链接 迭代 class Solution { public ListNode reverseList(ListNode head) { ListNode pre = null,cur = head,next = null; while(cur!=null){ //将cur.next存起来 nex
20
0
0
剑指offfer 从尾到头打印链表
摘要:题目链接 #1.for循环 class Solution { public int[] reversePrint(ListNode head) { ListNode temp = head; int n = 0; while(temp!=null){ temp = temp.next; n++; }
27
0
0
剑指offer 包含min函数的栈
摘要:力扣题目链接 class MinStack { //声明两个栈 A为数据栈,B为辅助栈 Stack<Integer> A,B; public MinStack() { //初始化栈 A = new Stack<>(); B = new Stack<>(); } public void push(in
26
0
0
剑指offer 双栈实现队列(java)
摘要:力扣题目链接 class CQueue { //全局声明两个栈 LinkedList<Integer> A,B; public CQueue() { //new两个栈 A = new LinkedList<Integer>(); B = new LinkedList<Integer>(); } pu
24
0
0
java基础(一)
摘要:#1.java基础 个人学习笔记有错请指正 ##1.多态 父抽象类被子类继承,并且子类可以重写父类方法。 ##2.静态变量 静态变量指的是被static修饰的类的变量;静态变量被所有类实例对象所共享,在内存中只有一个副本,当且仅当在类初次加载时会被初始化。 ##3.try catch finally
64
0
0
反转字符串II Java
摘要:力扣题目链接 基础知识 #1.解法一 class Solution { public String reverseStr(String s, int k) { StringBuffer res = new StringBuffer(); int length = s.length(); int st
26
0
0
反转字符串 Java
摘要:力扣题目链接 位运算 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];
16
0
0
四数之和 Java
摘要:力扣题目链接 双指针 class Solution { public List<List<Integer>> fourSum(int[] nums, int target) { List<List<Integer>> result = new ArrayList<>(); Arrays.sort(n
46
0
0
三数之和 java
摘要:1.HashSet class Solution { public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> millionYuanList = new ArrayList<>(); if(nums.length <
98
0
0
赎金信 Java
摘要:与有效字母异位词类似 class Solution { public boolean canConstruct(String ransomNote, String magazine) { //记录杂志字符串出现的次数 int[] arr = new int[26]; int temp; char[]
41
0
0
四数相加II Java
摘要:1.使用HashMap class Solution { public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) { //统计前两个数组中的元素之和(key),以及出现次数(valuevalue) Map
49
0
0
两数之和 java
摘要:#1.使用HashMap class Solution { public int[] twoSum(int[] nums, int target) { if(nums==null || nums.length==0){ return new int[0]; } //存储结果的数组 int[] res
58
0
0
点击右上角即可分享
微信分享提示
深色
回顶
收起