牛客题霸
我会jvav啊
此人很懒,已经转行摸鱼了
展开
-
牛客 NC1 大数加法(模拟)
将两个字符串逆置 然后维护s为最长串进行遍历相加 注意是否有进位 public class Solution { int carry = 0; public void add(StringBuilder sb ,char x ,char y){ int sum = x -'0' + y - '0' + carry;//相加 Integer tmp = sum%10; String c = tmp.toString(); sb.原创 2021-03-19 22:55:40 · 195 阅读 · 0 评论 -
牛客 NC3 链表中环的入口节点(快慢指针)
快慢指针找第一次相遇 相遇后慢指针回到起点 再每次快慢指针都走一步 再次相遇就是入口 public ListNode detectCycle(ListNode head) { if(head == null || head.next == null) return null; ListNode p = head ,q = head; while(q != null && q.next != null){ p =.原创 2021-03-19 20:53:24 · 86 阅读 · 0 评论 -
牛客 NC41 找到字符串的最长无重复字符子串(滑动窗口)
滑动窗口维护左边界 记录最大值 public int maxLength (int[] arr) { int left = 0; int max = -1; HashMap<Integer ,Integer> map = new HashMap<>(); for(int i = 0;i < arr.length;i++){ if(map.containsKey(arr[i])){ .原创 2021-03-18 14:20:38 · 256 阅读 · 0 评论 -
牛客 NC19 子数组的最大累加和问题(dp)
用变量dp维护子数组的左边界的同时维护一个最大值 public int maxsumofSubarray (int[] arr) { int dp = 0; int max = Integer.MIN_VALUE; for(int i = 0 ;i < arr.length ;i++){ dp += arr[i]; max = Math.max(max,dp); if(dp.原创 2021-03-18 13:01:42 · 101 阅读 · 0 评论 -
牛客NC68 跳台阶(dp)
入门级dp public int JumpFloor(int target) { if(target < 2) return 1; int[] dp = new int[target + 1]; dp[0] = 1; dp[1] = 1; for(int i = 2; i < target + 1 ;i++) dp[i] = dp[i-1] + dp[i-2]; re.原创 2021-03-18 12:55:33 · 127 阅读 · 0 评论 -
牛客 NC33 合并有序链表(合并链表)
public ListNode mergeTwoLists (ListNode l1, ListNode l2) { ListNode dummyHead = new ListNode(0); ListNode node = dummyHead; while(l1 != null && l2 != null){ if(l1.val < l2.val){ node.next ...原创 2021-03-18 12:48:38 · 131 阅读 · 0 评论 -
牛客 NC61 两数之和(哈希表)
public int[] twoSum (int[] numbers, int target) { HashMap<Integer ,Integer> map = new HashMap<>(); for(int i = 0 ;i < numbers.length ;i++){ if (map.containsKey(target - numbers[i])) return new...原创 2021-03-18 12:08:02 · 143 阅读 · 0 评论 -
牛客NC88 寻找第K大(优先队列小根堆)
题目 维护一个长度为K的升序优先队列 大于队首就入队 最后堆顶就是第K大的数 import java.util.*; public class Solution { public int findKth(int[] a, int n, int K) { PriorityQueue<Integer> queue = new PriorityQueue<Integer>(); for(int i = 0 ;i < n ;i++){原创 2021-03-18 11:57:00 · 125 阅读 · 0 评论 -
牛客NC15 求二叉树的层序遍历(队列)
题目 层序遍历需要借助队列的先进先出的性质每一层遍历 只遍历当前层的结点,所以遍历的当前结点要把左右儿子全入队 作为下层结点。 public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) { ArrayList<ArrayList<Integer>> list = new ArrayList<>(); if (root == null) r原创 2021-03-18 11:38:18 · 140 阅读 · 0 评论 -
牛客NC119 最小的K个数(优先队列或者排序)
题目 找前K小的数 维护一个长度为K的降序优先队列 排序这里就不写了 小于K直接入队,等于K时 如果当前元素小于队首元素,队首元素出队,当前元素入队 import java.util.ArrayList; import java.util.PriorityQueue; import java.util.*; public class Solution { public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, i原创 2021-03-18 11:06:11 · 105 阅读 · 1 评论 -
牛客NC45 实现二叉树先序,中序和后序遍历(递归)
public int[][] threeOrders (TreeNode root) { ArrayList<Integer> list = new ArrayList(); ArrayList<Integer> list2 = new ArrayList(); ArrayList<Integer> list3 = new ArrayList(); preOrder(root,list); ...原创 2021-03-16 22:11:26 · 148 阅读 · 0 评论 -
牛客NC4 判断链表中是否有环(快慢指针)
题目 快慢指针判断是否相遇 public boolean hasCycle(ListNode head) { if(head == null || head.next == null) return false; ListNode p = head; ListNode q = head; while(q != null && q.next != null){ q = q.next.next;原创 2021-03-16 21:27:50 · 97 阅读 · 0 评论 -
牛客NC140 排序(快排和归并排序)
题目 复习下快排和归并 果然忘了hhhh 快排 import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * 将给定数组排序 * @param arr int整型一维数组 待排序的数组 * @return int整型一维数组 */ public int[] MySort (int[] arr) { quic原创 2021-03-16 21:22:44 · 157 阅读 · 0 评论 -
牛客NC78反转链表 (头插法插入)
题目链接 把头结点下一个指向null 头结点后面的所有结点依次插入到头结点前 public ListNode ReverseList(ListNode head) { if(head == null || head.next == null) return head; ListNode dummyNode = new ListNode(0); ListNode second = head.next; head.next = null;原创 2021-03-16 19:43:40 · 82 阅读 · 0 评论