**

牛客网面试高频题top100(11~20 java实现)

**

11.跳台阶

一只青蛙一次可以跳上1级台阶,也可以跳上2级。求该青蛙跳上一个 n 级的台阶总共有多少种跳法(先后次序不同算不同的结果)。

public class Solution {public int jumpFloor(int target) {int c1 = 1,c2 = 2;if(target<3) return target;int c3 = 0;for(int i=3;i<=target;i++){c3 = c1+c2;c1 = c2;c2 = c3;}return c3;}
}

12.链表中的节点每K个一组翻转

将给出的链表中的节点每 k 个一组翻转,返回翻转后的链表
如果链表中的节点数不是 k 的倍数,将最后剩下的节点保持原样
你不能更改节点中的值,只能更改节点本身。

import java.util.*;/** public class ListNode {*   int val;*   ListNode next = null;* }*/public class Solution {public ListNode reverseKGroup (ListNode head, int k) {ListNode dummy = new ListNode(-1);dummy.next = head;ListNode pre = dummy;while(true){ListNode start = head;for(int i=1;i<k && head!=null;i++)  head = head.next;if(head==null) break;ListNode next = head.next;head.next = null;pre.next = reverse(start);start.next = next;pre = start;head = next;}return dummy.next;}public ListNode reverse(ListNode node){ListNode pre = null;ListNode cur = null;while(node!=null){cur = node.next;node.next = pre;pre = node;node = cur;}return pre;}
}

13.连续子数组最大和

输入一个长度为n的整型数组array,数组中的一个或连续多个整数组成一个子数组,子数组最小长度为1。求所有子数组的和的最大值。
数据范围:

public class Solution {public int FindGreatestSumOfSubArray(int[] array) {int[] dp = new int[array.length];dp[0] = array[0];int max = dp[0];for(int i=1;i<array.length;i++){dp[i] = Math.max(dp[i-1]+array[i], array[i]);max = Math.max(max,dp[i]);}return max;}
}

空间优化后:

public class Solution {public int FindGreatestSumOfSubArray(int[] array) {int temp = array[0];int max = temp;for(int i=1;i<array.length;i++){temp = Math.max(temp+array[i], array[i]);max = Math.max(max,temp);}return max;}
}

14.最长无重复子数组

给定一个长度为n的数组arr,返回arr的最长无重复元素子数组的长度,无重复指的是所有数字都不相同。
子数组是连续的,比如[1,3,5,7,9]的子数组有[1,3],[3,5,7]等等,但是[1,3,7]不是子数组

import java.util.*;
public class Solution {public int maxLength (int[] arr) {HashSet<Integer> set = new HashSet<>();int i=0,end = 0,max=0;while(end<arr.length){if(!set.contains(arr[end])){set.add(arr[end]);max = Math.max(max,end-i+1);end++;}else{set.remove(arr[i]);i++;}}return max;}
}

15.判断链表中是否有环

判断给定的链表中是否有环。如果有环则返回true,否则返回false。

/*** Definition for singly-linked list.* class ListNode {*     int val;*     ListNode next;*     ListNode(int x) {*         val = x;*         next = null;*     }* }*/
public class Solution {public boolean hasCycle(ListNode head) {if(head==null || head.next==null)  return false;ListNode low = head;ListNode fast = head;while(low!=null ){if(fast==null  || fast.next==null)  break;low = low.next;fast = fast.next.next;if(low==fast){return true;}}return false;}
}

16.合并两个有序数组

给出一个有序的整数数组 A 和有序的整数数组 B ,请将数组 B 合并到数组 A 中,变成一个有序的升序数组

import java.util.*;
public class Solution {public void merge(int A[], int m, int B[], int n) {int t=0,j=0,k =0;int a[] = new int[m];for(int i=0;i<m;i++)a[i] = A[i];while(t<m && j<n){A[k++] = a[t]<=B[j]? a[t++]:B[j++];}while(t<m)A[k++] = a[t++];while(j<n)A[k++] = B[j++];}
}

17.链表中环的入口结点

给一个长度为n链表,若其中包含环,请找出该链表的环的入口结点,否则,返回null。

/*public class ListNode {int val;ListNode next = null;ListNode(int val) {this.val = val;}
}
*/
public class Solution {public ListNode EntryNodeOfLoop(ListNode pHead) {ListNode fast = pHead;ListNode slow = pHead;boolean flag = false;while(slow!=null && fast.next!=null && fast!=null){slow = slow.next;fast = fast.next.next;if( fast==null) break;if(slow==fast){flag = true;break;}}if(flag==false)  return null;while(pHead!=slow){slow = slow.next;pHead = pHead.next;}return pHead;}
}

18.有效括号序列

给出一个仅包含字符’(‘,’)‘,’{‘,’}‘,’[‘和’]',的字符串,判断给出的字符串是否是合法的括号序列
括号必须以正确的顺序关闭,"()“和”()[]{}“都是合法的括号序列,但”(]“和”([)]"不合法。

import java.util.*;public class Solution {public boolean isValid (String s) {if(s.length()==0)  return true;char[] cs = s.toCharArray();Stack<Character> stack = new Stack<>();for(int i=0;i<cs.length;i++){if(stack.empty())stack.push(cs[i]);else{String str = "";str += stack.peek();str += cs[i];if(str.equals("()") || str.equals("{}") ||str.equals("[]") )stack.pop();elsestack.push(cs[i]);}}if(stack.empty())  return true;return false;}}

19.删除链表的倒数第n个节点

import java.util.*;/** public class ListNode {*   int val;*   ListNode next = null;* }*/public class Solution {public ListNode removeNthFromEnd (ListNode head, int n) {ListNode dummy = new ListNode(-1);dummy.next = head;int count = 0;while(head!=null){count += 1;head = head.next;}head = dummy.next;ListNode pre = dummy;int i= 0;while(i!=count-n){pre = head;head = head.next;i++;}pre.next = head.next;return dummy.next;}
}

20.大数加法

以字符串的形式读入两个数字,编写一个函数计算它们的和,以字符串形式返回。

import java.util.*;public class Solution {public String solve (String s, String t) {int i=s.length()-1;int j=t.length()-1;StringBuilder res = new StringBuilder();int more = 0;while(i>=0 || j>=0){int si = i>=0?s.charAt(i)-'0':0;int ti = j>=0?t.charAt(j)-'0':0;more += si+ti;res.append(more%10);more /= 10;i--;j--;}if(more>0) res.append(more);return res.reverse().toString();}
}

牛客网面试高频题top100(11~20)相关推荐

  1. 牛客网面试高频题top100(1~10)

    *牛客网面试高频题top100(1~10 java实现) 1.反转链表 描述: 给定一个单链表的头结点pHead(该头节点是有值的,比如在下图,它的val是1),长度为n,反转该链表后,返回新链表的表 ...

  2. 牛客网Veirlog刷题答案目录(持续更新)

    牛客网Veirlog刷题答案目录(持续更新) 基础篇 进阶篇 基础篇 1.VL1--四选一多路选择器 2.VL2--异步复位的串联T触发器 3.VL3--奇偶校验 4.VL4--移位运算与乘法 5.V ...

  3. 牛客网刷算法题的输入输出(C++)

    内容简述 该篇文章将对牛客网刷题中关于输入输出的一些问题作一个总结.每年互联网公司的招聘都必不可少会有算法题,因此平时很多人都会去一些刷题网站进行刷题来学习.这里面用的比较多的刷题网站是leetcod ...

  4. 【牛客网面试必刷TOP101】链表篇(一)

    链表 一.前言 二.学习刷题网站 1.推荐的原因 三.刷题 <1>反转链表 递归法 <2>链表内指定区间反转 ①头插法 ②递归法 <3>链表中的节点每k个一组翻转 ...

  5. 牛客网SQL刷题笔记(MySQL)

    牛客网SQL刷题笔记(MySQL) 此博客集合LeetCode.牛客网常见的题型及其解法,侵删 目录 牛客网SQL刷题笔记(MySQL) 类型1:查找排名第几的数据 SQL2 查找入职员工时间排名倒数 ...

  6. 牛客网Java刷题知识点之关键字static、static成员变量、static成员方法、static代码块和static内部类...

    不多说,直接上干货! 牛客网Java刷题知识点之关键字static static代表着什么 在Java中并不存在全局变量的概念,但是我们可以通过static来实现一个"伪全局"的概 ...

  7. 牛客网Java刷题知识点之构造函数可以调用一般函数,但是一般函数不可以直接调用构造函数...

    不多说,直接上干货! 通过 牛客网Java刷题知识点之构造函数是什么.一般函数和构造函数什么区别呢.构造函数的重载.构造函数的内存图解 我们对构造函数有了一个比较清楚的认识,当我们在创建对象时,我们会 ...

  8. 牛客网Java刷题知识点之Java 集合框架的构成、集合框架中的迭代器Iterator、集合框架中的集合接口Collection(List和Set)、集合框架中的Map集合...

    不多说,直接上干货! 集合框架中包含了大量集合接口.这些接口的实现类和操作它们的算法. 集合容器因为内部的数据结构不同,有多种具体容器. 不断的向上抽取,就形成了集合框架. Map是一次添加一对元素. ...

  9. 牛客网Java刷题知识点之ArrayList 、LinkedList 、Vector 的底层实现和区别

    不多说,直接上干货! 这篇我是从整体出发去写的. 牛客网Java刷题知识点之Java 集合框架的构成.集合框架中的迭代器Iterator.集合框架中的集合接口Collection(List和Set). ...

最新文章

  1. Centos7上安装Nginx两种方法
  2. ionic3相关知识收集
  3. React Mixins入门指南
  4. C++中const——由一个例子想到的
  5. UVa 11388 - GCD LCM
  6. 20165220预备作业3 Linux安装及学习
  7. Linux学习之安装jdk
  8. Deklarit3.0的确不错,推荐一下。
  9. 局域网桌面共享软件(优化版)
  10. 最新 Transformer 预训练模型综述!
  11. 商务网站建设与维护【12】
  12. css固定图片大小 vue_css3 实现图片等比例放大与缩小
  13. 部署策略对比:蓝绿部署、金丝雀发布及其他
  14. 感悟大学一年的成长经历
  15. 微软常用运行库合集(3264位)
  16. 服务器配置与软件安装合集
  17. 哈希表、红黑树、B树、B+树基础
  18. OpenWrt下使用docker安装icloudpd实现iPhone照片备份私有云盘nas
  19. CLIP,GLIP论文解读,清晰明了
  20. 驱动程序(8) Windriver初步使用和快速生成驱动程序代码

热门文章

  1. 辐射光电流测试软件,电磁骚扰辐射发射的测试方法
  2. 文件管理“桌面”变成“Desktop”解决方法
  3. Springboot简单介绍
  4. 计算机低音,如何调整电脑的高音和低音
  5. linux系统可以用pr吗,linux – 关于pr命令的一些问题
  6. 盘点微信中被隐藏的实用功能
  7. 在机器学习领域,怎样写好一篇学术论文
  8. 月薪 2~3W 的码农,是怎样度过一天的?
  9. 大疆无人机android登录闪退,你的DJI APP闪退了吗?附官方解决方案
  10. 涌泉蜜桔,皮薄汁甜,太爱这个桔子了