当前位置:   article > 正文

Java笔试模拟试题(三)

java笔试模拟

一,用二个栈实现队列

用两个栈实现一个队列。队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素,deleteHead 操作返回 -1 )

示例 1:

输入:
["CQueue","appendTail","deleteHead","deleteHead"]
[[],[3],[],[]]
输出:[null,null,3,-1]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

栈(stack):简单来说就是先进后出(先进入的元素,最后出来,比如给手枪压子弹的过程(先到的子弹最后打出)

使用方法:增加元素:push()  抛出元素:pop()  检查是否为空:isEmpty()

队列:先进先出(先进入的元素,先出来:比如排队一样(谁在前面,谁就先走)

  1. class CQueue {
  2. private Stack<Integer> stack1;
  3. private Stack<Integer> stack2;
  4. public CQueue() {
  5. stack1 = new Stack();
  6. stack2 = new Stack();
  7. }
  8. public void appendTail(int value) {
  9. stack1.push(value);
  10. }
  11. public int deleteHead() {
  12. if(!stack2.isEmpty()){
  13. return stack2.pop();
  14. }else{
  15. while(!stack1.isEmpty()){
  16. stack2.push(stack1.pop());
  17. }
  18. return stack2.isEmpty() ? -1 : stack2.pop();
  19. }
  20. }
  21. }
  22. /**
  23. * Your CQueue object will be instantiated and called as such:
  24. * CQueue obj = new CQueue();
  25. * obj.appendTail(value);
  26. * int param_2 = obj.deleteHead();
  27. */

二,包含main函数的栈

定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的 min 函数在该栈中,调用 min、push 及 pop 的时间复杂度都是 O(1)。

使用方法:增加元素:push()  抛出元素:pop()   检查是否为空:isEmpty() 查看栈顶元素:peek()

  1. class MinStack {
  2. Deque<Integer> stack;
  3. Deque<Integer> minStack;
  4. /** initialize your data structure here. */
  5. public MinStack() {
  6. stack = new LinkedList();
  7. minStack = new LinkedList();
  8. }
  9. public void push(int x) {
  10. stack.push(x);
  11. if(minStack.isEmpty() || x < minStack.peek() ){
  12. minStack.push(x);
  13. }else{
  14. minStack.push(minStack.peek());
  15. }
  16. }
  17. public void pop() {
  18. stack.pop();
  19. minStack.pop();
  20. }
  21. public int top() {
  22. return stack.peek();
  23. }
  24. public int min() {
  25. return minStack.peek();
  26. }
  27. }
  28. /**
  29. * Your MinStack object will be instantiated and called as such:
  30. * MinStack obj = new MinStack();
  31. * obj.push(x);
  32. * obj.pop();
  33. * int param_3 = obj.top();
  34. * int param_4 = obj.min();
  35. */

三,从头到尾打印链表

输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。

示例 1:

输入:head = [1,3,2]
输出:[2,3,1]

首先我们需要清楚什么是链表,链表存在一种叫节点的结构ListNode,有单项链表与双向链表.下图就是节点的属性

 当我们需要一个从头进末尾返回的结构时!我们应当考虑栈这个结构!(先进后出),还需要一个数组用来返回结果!

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { val = x; }
  7. * }
  8. */
  9. class Solution {
  10. public int[] reversePrint(ListNode head) {
  11. //定义指针
  12. ListNode corNode = head;
  13. //定义一个栈
  14. Stack<ListNode> stack = new Stack();
  15. //进栈
  16. while(corNode != null){
  17. stack.push(corNode);
  18. corNode = corNode.next; //!!!指针向下移动
  19. }
  20. int len = stack.size();
  21. //定义数组
  22. int[] arr = new int[len];
  23. for(int i = 0;i < len ;i++){
  24. arr[i] = stack.pop().val; //出栈,进入数组
  25. }
  26. return arr;
  27. }
  28. }

四,反转链表

定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。

示例:

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

递归方法:让next发生反向

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) { val = x; }
  7. * }
  8. */
  9. class Solution {
  10. public ListNode reverseList(ListNode head) {
  11. if ( head == null || head.next == null) {
  12. return head;
  13. }
  14. ListNode val = reverseList(head.next); //递归
  15. head.next.next = head; //next指向发生反向
  16. head.next = null; //原来指向断开
  17. return val;
  18. }
  19. }

五,复杂链表的复制

请实现 copyRandomList 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null。

示例 1:

输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  1. /*
  2. // Definition for a Node.
  3. class Node {
  4. int val;
  5. Node next;
  6. Node random;
  7. public Node(int val) {
  8. this.val = val;
  9. this.next = null;
  10. this.random = null;
  11. }
  12. }
  13. */
  14. class Solution {
  15. public Node copyRandomList(Node head) {
  16. if(head == null) return null;
  17. Node cur = head;
  18. Map<Node,Node> map = new HashMap();
  19. while(cur != null){
  20. Node newNode = new Node(cur.val);
  21. map.put(cur,newNode);
  22. cur = cur.next;
  23. }
  24. cur= head;
  25. while(cur != null){
  26. Node valcur = map.get(cur);
  27. Node nextcul = cur.next;
  28. Node randomcul = map.get(nextcul);
  29. valcur.next = randomcul;
  30. Node randomkey = cur.random;
  31. Node keyrandom = map.get(randomkey);
  32. valcur.random = keyrandom;
  33. cur=cur.next;
  34. }
  35. return map.get(head);
  36. }
  37. }

 

六,替换空格

请实现一个函数,把字符串 s 中的每个空格替换成"%20"。

示例 1:

输入:s = "We are happy."
输出:"We%20are%20happy."

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/ti-huan-kong-ge-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  1. class Solution {
  2. public String replaceSpace(String s) {
  3. int len = s.length();
  4. int count = 0;
  5. for(int i = 0 ; i < len ; i++ ){
  6. if(s.charAt(i) == ' '){
  7. count++;
  8. }
  9. }
  10. int newLen = len + count*2;
  11. char arr[] = new char[newLen];
  12. for(int i = len-1; i >= 0 ; i--){
  13. if(s.charAt(i) == ' '){
  14. arr[--newLen] = '0';
  15. arr[--newLen] = '2';
  16. arr[--newLen] = '%';
  17. len--;
  18. }else{
  19. arr[--newLen] = s.charAt(--len);
  20. }
  21. }
  22. return new String(arr);
  23. }
  24. }

七,左旋转字符串

字符串的左旋转操作是把字符串前面的若干个字符转移到字符串的尾部。请定义一个函数实现字符串左旋转操作的功能。比如,输入字符串"abcdefg"和数字2,该函数将返回左旋转两位得到的结果"cdefgab"。

示例 1:

输入: s = "abcdefg", k = 2
输出: "cdefgab"

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

切换为数组进行拼接(小编第一时间想到方法):

  1. class Solution {
  2. public String reverseLeftWords(String s, int n) {
  3. int len = s.length();
  4. char [] arr = new char[len];
  5. int val = 0;
  6. for(int j = n ; j < len ; j++ ){
  7. arr[val] = s.charAt(j);
  8. val++;
  9. }
  10. for(int i = 0 ; i < n ; i++){
  11. arr[val] = s.charAt(i);
  12. val++;
  13. }
  14. return new String(arr);
  15. }
  16. }

内置方法进行拼接:(大神方法!)

  1. class Solution {
  2. public String reverseLeftWords(String s, int n) {
  3. String start = s.substring(0,n);
  4. String end = s.substring(n);
  5. return end + start;
  6. }
  7. }

指针方法:(小编第二想到方法,效果最差

  1. class Solution {
  2. public String reverseLeftWords(String s, int n) {
  3. String str = "";
  4. int len = s.length();
  5. for (int i = 0; i < len ; i++) {
  6. char c = s.charAt(i);
  7. if (i > n-1){
  8. str+=c;
  9. }
  10. }
  11. for (int i = 0; i < len; i++) {
  12. char c = s.charAt(i);
  13. if (i < n){
  14. str+=c;
  15. }
  16. }
  17. return str;
  18. }
  19. }

八,数组中重复的数子

找出数组中重复的数字。
在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。

示例 1:

输入:
[2, 3, 1, 0, 2, 5, 3]
输出:2 或 3 

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

小编第一个想到的是这个使用位运算:使用一些

  1. class Solution {
  2. public int findRepeatNumber(int[] nums) {
  3. int val = 0 ;
  4. int tmp = 0;
  5. Arrays.sort(nums);
  6. int len = nums.length-1;
  7. for(int i = 1 ; i <= len ; i++ ){
  8. if(0 == (nums[tmp]^nums[i]) ){
  9. val = nums[i];
  10. break;
  11. }else{
  12. tmp++;
  13. }
  14. }
  15. return val;
  16. }
  17. }

看大神使用的是引用变量,确实比小编强!

  1. class Solution {
  2. public int findRepeatNumber(int[] nums) {
  3. Arrays.sort(nums);
  4. int val = -1;
  5. int len = nums.length;
  6. for (int i = 0; i < len ; i++) {
  7. if (nums[i] == val ){
  8. val = nums[i];
  9. break;
  10. }else {
  11. val = nums[i];
  12. }
  13. }
  14. return val;
  15. }
  16. }

小编突然想到如果二个方法和为一个方法,效率会不会提高许多呢!(效果并不理想

  1. class Solution {
  2. public int findRepeatNumber(int[] nums) {
  3. Arrays.sort(nums);
  4. int val = -1;
  5. int len = nums.length;
  6. for (int i = 0; i < len ; i++) {
  7. if ( 0 == (nums[i] ^ val) ){
  8. val = nums[i];
  9. break;
  10. }else {
  11. val = nums[i];
  12. }
  13. }
  14. return val;
  15. }
  16. }

十,在二维数组中查找数值

在一个 n * m 的二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个高效的函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

示例:

现有矩阵 matrix 如下:

[
  [1,   4,  7, 11, 15],
  [2,   5,  8, 12, 19],
  [3,   6,  9, 16, 22],
  [10, 13, 14, 17, 24],
  [18, 21, 23, 26, 30]
]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/er-wei-shu-zu-zhong-de-cha-zhao-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

首先记住:二个for循环解决一个二维数组!!!

  1. //行与列的表示
  2. public class Test {
  3. public static void main(String[] args) {
  4. int[][] arr = new int[3][4];
  5. int line = arr.length; //行
  6. int column = arr[0].length; //列
  7. System.out.println("行:"+line+" 列:"+column);
  8. }
  9. }

 好了,我们来看看题目在二维数组中查找一个元素,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序(从下面二维数组中就可以看见我们可以查找每一列的末尾元素(15,19,22,24,30),当末尾元素小于查找元素时(比如23,就直接找末尾二行就行),就能证明该行没有查找元素(从右上角开始和右下脚开始都行),大大提高查找速度!!!)

[
  [1,   4,  7, 11, 15],
  [2,   5,  8, 12, 19],
  [3,   6,  9, 16, 22],
  [10, 13, 14, 17, 24],
  [18, 21, 23, 26, 30]
]

  1. class Solution {
  2. public boolean findNumberIn2DArray(int[][] matrix, int target) {
  3. if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
  4. //考虑特殊情况
  5. return false;
  6. }
  7. int line = matrix.length; //行
  8. int column = matrix[0].length; //列
  9. //创建查找的指针,从左上角开始查找(第一行末尾开始)
  10. int findLine = 0;
  11. int findColumn = column-1;
  12. while(line-1 >= findLine && findColumn >= 0){
  13. //开始循环遍历
  14. if(target == matrix[findLine][findColumn]){
  15. return true;
  16. }else if(target > matrix[findLine][findColumn]){
  17. //目标比查找值末尾还大,向下一行前进
  18. findLine ++;
  19. }else{
  20. //目标比查找值末尾还小,可能存在,列向前进
  21. findColumn--;
  22. }
  23. }
  24. //没有找到。返回false
  25. return false;
  26. }
  27. }

使用二个for循环解决

  1. class Solution {
  2. public boolean findNumberIn2DArray(int[][] matrix, int target) {
  3. if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
  4. //考虑特殊情况
  5. return false;
  6. }
  7. int line = matrix.length ; //行
  8. int column = matrix[0].length; //列
  9. //二个for循环解决问题
  10. for(int i = 0; i < line ;i++){
  11. for(int j = column -1 ; j >= 0 ; j--){
  12. if(target == matrix[i][j]){
  13. return true;
  14. }
  15. }
  16. }
  17. return false;
  18. }
  19. }

十一,在排列数组中查找数值1

统计一个数字在排序数组中出现的次数。

示例 1:

输入: nums = [5,7,7,8,8,10], target = 8
输出: 2
示例 2:

输入: nums = [5,7,7,8,8,10], target = 6
输出: 0

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/zai-pai-xu-shu-zu-zhong-cha-zhao-shu-zi-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

遍历方式:(小编第一想到方式)

  1. class Solution {
  2. public int search(int[] nums, int target) {
  3. int len = nums.length;
  4. int count = 0; //统计次数
  5. for(int i = 0; i < len ; i++){
  6. if(target == nums[i]){
  7. count++;
  8. }
  9. }
  10. return count;
  11. }
  12. }

二分法:(提高效率方式)

  1. class Solution {
  2. public int search(int[] nums, int target) {
  3. int i = 0, j = nums.length - 1;
  4. while(i <= j) {
  5. int m = (i + j) / 2;
  6. if(nums[m] <= target) i = m + 1;
  7. else j = m - 1;
  8. }
  9. int right = i;
  10. // 若数组中无 target ,则提前返回
  11. if(j >= 0 && nums[j] != target) return 0;
  12. // 搜索左边界 right
  13. i = 0; j = nums.length - 1;
  14. while(i <= j) {
  15. int m = (i + j) / 2;
  16. if(nums[m] < target) i = m + 1;
  17. else j = m - 1;
  18. }
  19. int left = j;
  20. return right - left - 1;
  21. }
  22. }

十二,0~n中缺失的数子

一个长度为n-1的递增排序数组中的所有数字都是唯一的,并且每个数字都在范围0~n-1之内。在范围0~n-1内的n个数字中有且只有一个数字不在该数组中,请找出这个数字。

示例 1:

输入: [0,1,3]
输出: 2
示例 2:

输入: [0,1,2,3,4,5,6,7,9]
输出: 8

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/que-shi-de-shu-zi-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

位运算:(将缺少数数组+不少数的数组相结合,相同异或(^)为0,并且异或存在交换定理,0与任何数异或为它本身!

  1. class Solution {
  2. public int missingNumber(int[] nums) {
  3. int len1 = nums.length;
  4. int val = 0;
  5. int tmp = 0;
  6. int [] arr = new int[(2*len1)+1];
  7. int len2 = arr.length;
  8. for (int i = 0; i < len2 ; i++) {
  9. if (i < len1){
  10. arr[i] = nums[i];
  11. }else {
  12. arr[i] = tmp;
  13. tmp++;
  14. }
  15. }
  16. for (int i = 0; i < len2 ; i++) {
  17. val =val^arr[i];
  18. }
  19. return val;
  20. }
  21. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/582197
推荐阅读
相关标签
  

闽ICP备14008679号