![](https://img-blog.csdnimg.cn/20201014180756724.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
牛客-剑指offer
amalloc
这个作者很懒,什么都没留下…
展开
-
NC52 括号序列
解法:使用stack,左进右匹配,利用后进先出的特性import java.util.*;public class Solution { /** * * @param s string字符串 * @return bool布尔型 */ public boolean isValid (String s) { // write code here int len = s.length(); if(l原创 2021-08-19 15:13:35 · 263 阅读 · 0 评论 -
NC22 合并两个有序的数组
解题思路:双指针倒序合并public class Solution { public void merge(int A[], int m, int B[], int n) { int p = m-1; int q = n-1; int i = m+n-1; while(p>=0&&q>=0){ if(A[p]>B[q]){ A[i--] = A.原创 2021-08-19 14:50:07 · 97 阅读 · 0 评论 -
NC50 链表中的节点每k个一组翻转
解法一,借用栈的后进先出完成每个的翻转,时间On 空间Ok,k为栈的借用容量 public ListNode reverseKGroup (ListNode head, int k) { // write code here if(k<=1||head==null) return head; Stack<ListNode> stack = new Stack<>(); ListNod原创 2021-08-18 21:57:35 · 104 阅读 · 0 评论 -
NC41 最长无重复子数组
解法:用set来判断重复,用双指针来确定长度,右指针扩展,左指针缩减至不重复。import java.util.*;public class Solution { /** * * @param arr int整型一维数组 the array * @return int整型 */ public int maxLength (int[] arr) { // write code here if(arr.lengt.原创 2021-08-17 23:34:07 · 77 阅读 · 0 评论 -
NC19-子数组最大和
思路:一维动态规划数组,每个下标值与加前和比较,去最大值import java.util.*;public class Solution { /** * max sum of the subarray * @param arr int整型一维数组 the array * @return int整型 */ public int maxsumofSubarray (int[] arr) { // write code here .原创 2021-08-17 22:52:08 · 58 阅读 · 0 评论 -
NC76 两个栈实现队列
入队,正常入栈出队,借助第二个栈,若第二个栈为空,将第一个栈全部移动到第二个栈;不为空,正常出栈import java.util.Stack;public class Solution { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int nod原创 2021-08-17 22:40:06 · 94 阅读 · 0 评论 -
NC33 合并有序链表
解法:选择其中一个链表加入虚表头,在该原列表里插入另一个列表时间负责度O(n)空间复杂度O(1)/*public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; }}*/public class Solution { public ListNode Merge(ListNode list1,ListNode list2.原创 2021-08-17 22:35:55 · 170 阅读 · 0 评论 -
NC15 求二叉树的层序遍历
import java.util.*;/* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * } */public class Solution { /** * * @param root TreeNode类 * @return int整型ArrayList<ArrayList<>&.原创 2021-08-15 23:29:29 · 78 阅读 · 0 评论 -
NC88 寻找第K大
import java.util.*;public class Solution { private int K; public int findKth(int[] a, int n, int K) { // write code here this.K = K; quickSort(a,0,a.length-1); return a[K-1]; } public void quickSort(int[] .原创 2021-08-15 23:28:13 · 78 阅读 · 0 评论 -
NC45 实现二叉树先序,中序和后序遍历
import java.util.*;/* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * } */public class Solution { /** * * @param root TreeNode类 the root of binary tree * @return int整型二维数组原创 2021-08-15 22:13:17 · 74 阅读 · 0 评论 -
NC93 设计LRU缓存结构
NC93 设计LRU缓存结构感谢牛客题解博主-代码:import java.util.*;public class Solution { /** * lru design * @param operators int整型二维数组 the ops * @param k int整型 the k * @return int整型一维数组 *思路在于,使用map来判断是否存在key,value; * 使用链表来判断顺序,使用原创 2021-08-15 17:05:25 · 80 阅读 · 0 评论 -
NC140-排序
解法1-快速排序时间 O(nlogn) 空间O(1)public void quickSort(int[] arr, int low, int high){ if(low>=high) return; int p = arr[low]; int i = low; int j = high; while(i<j){ while(j>i&&arr.原创 2021-08-15 15:47:06 · 104 阅读 · 0 评论 -
NC-反转链表
解法1. 哑节点前插public class Solution { public ListNode ReverseList(ListNode head) { if(head==null || head.next==null) return head; ListNode dummy = new ListNode(-1); while(head!=null){ ListNode p = head.next.原创 2021-08-04 22:20:50 · 69 阅读 · 0 评论 -
NC-大数相加
题目:解决:现将二者长度补充0至长度相等,模拟数据加法从尾部开始相加,并添加进位标志进位为多少,初始为0.int 转 char,char转int代码: public String solve (String s, String t) { // write code here int len1 = s.length(); int len2 = t.length(); if(len1<len2) s =原创 2021-06-06 23:20:05 · 136 阅读 · 0 评论 -
NC-二叉树中两个节点的最近公共祖先
题目:解决:递归递归找两个节点,找到则返回该节点如果该节点是两个节点的以外节点,对于当前节点的左右继续找,如果左右均不为空,说明在该节点两边,应返回该节点,如果仅有一个不为空,说明两节点在同一边,返回不为空的节点如果都为空,说明这个节点的所在分支没有这两个节点,返回空。代码: public int lowestCommonAncestor (TreeNode root, int o1, int o2) { // write code here Tree原创 2021-06-06 21:15:49 · 91 阅读 · 0 评论 -
NC-删除链表的倒数第n个节点
题目:解决方法:快慢指针 public ListNode removeNthFromEnd (ListNode head, int n) { // write code here if(head==null) return head; ListNode dummy = new ListNode(-1); dummy.next = head; ListNode slow = dummy;原创 2021-06-06 20:49:35 · 66 阅读 · 0 评论 -
NC-子数组的最大累加和
题目:解决:动规 public int maxsumofSubarray (int[] arr) { // write code here if(arr.length<1) return -1; int max = arr[0]; for(int i =1;i<arr.length;i++){ arr[i] = arr[i]>arr[i]+arr[i-1]?arr[i原创 2021-06-06 20:40:09 · 52 阅读 · 0 评论 -
NC-寻找第K大
题目:解决方法:快排、堆1.快排public class Solution { public int findKth(int[] a, int n, int K) { // write code here quickSort(a,0,a.length-1,K-1); return a[K-1]; } public static void quickSort(int[] a,int low,int high,int k){原创 2021-06-06 20:27:02 · 50 阅读 · 0 评论 -
NC-二叉树的之字形层序遍历
题目:解决方法:BFS队列、DFS递归1.BFS队列 public ArrayList<ArrayList<Integer>> zigzagLevelOrder (TreeNode root) { // write code here ArrayList<ArrayList<Integer>> list = new ArrayList<>(); if(root==null)原创 2021-05-30 19:40:38 · 58 阅读 · 0 评论 -
NC-求二叉树的层序遍历
题目:解决方法:BFS队列、DFS递归1.队列public class Solution { /** * * @param root TreeNode类 * @return int整型ArrayList<ArrayList<>> */ public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) { // wri原创 2021-05-30 19:18:40 · 51 阅读 · 0 评论 -
NC-合并有序链表
题目:解决方法: public ListNode mergeTwoLists (ListNode l1, ListNode l2) { // write code here ListNode dummy = new ListNode(-1); ListNode pre = dummy; while(l1!=null&&l2!=null){ if(l1.val<l2.val){原创 2021-05-30 11:06:14 · 37 阅读 · 0 评论 -
NC-链表中的节点每k个一组翻转
题目:解决方法:模拟、栈1.模拟[注]:(1) 按顺序每k个进行分组(2) 记录下一组的pre节点,方便组之间的连接(3) 注意节点操作的内存变化和节点状态变化,比如pre、子函数的翻转之后!!! public ListNode reverseKGroup (ListNode head, int k) { // write code here ListNode dummy = new ListNode(-1); dummy.next =原创 2021-05-30 10:52:11 · 67 阅读 · 0 评论 -
NC-链表中倒数第k个节点
题目:解决方法:双指针、栈、递归1.双指针 public ListNode FindKthToTail (ListNode pHead, int k) { // write code here ListNode slow = pHead; ListNode fast = pHead; while(k>0&&fast!=null){ fast = fast.next;原创 2021-05-29 18:31:04 · 53 阅读 · 0 评论 -
NC-合并两个有序的数组
题目:解决方法:数组+双指针,倒着存,数据的挪动会少一些public class Solution { public void merge(int A[], int m, int B[], int n) { int tag = n + m - 1; int i = m-1; int j = n-1; while(i>=0 || j>=0){ if(i<0)原创 2021-05-29 16:31:08 · 532 阅读 · 0 评论 -
NC-两个栈实现队列
题目:解决方法:push 正常pop 栈2是否为空,不空,正常pop,空,将正常push的栈中的元素pop到stack2中import java.util.Stack;public class Solution { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public v原创 2021-05-28 22:03:57 · 59 阅读 · 0 评论 -
NC-矩阵的最小路径和
题目:解决方法:动态规划import java.util.*;import java.lang.*;public class Solution { /** * * @param matrix int整型二维数组 the matrix * @return int整型 */ public int minPathSum (int[][] matrix) { // write code here int n = m原创 2021-05-28 21:54:35 · 65 阅读 · 0 评论 -
NC-链表中环的入口节点
题目:解决方法:快慢指针、哈希1.快慢指针存在假设,当快慢指针相遇时,快指针走的步数是慢指针的2倍,则有,2(a+b)=a+b+c+b,得出a=c,所以相遇时,一个从头节点向前走,一个从相遇节点继续走,再次相遇就是入口。感谢牛客题解public class Solution { public ListNode detectCycle(ListNode head) { ListNode slow = head; ListNode fast = head;原创 2021-05-28 21:31:23 · 618 阅读 · 0 评论 -
NC-数组中出现次数超过一半的数字
题目:解决方法:排序、哈希、候选法1.排序import java.util.*;public class Solution { public int MoreThanHalfNum_Solution(int [] array) { Arrays.sort(array); int len = array.length; int cnt =0; int res = array[len/2]; for(int i =原创 2021-05-28 20:12:57 · 55 阅读 · 0 评论