【牛客小题练手】- - - Java小题

#「笔耕不辍」--生命不息,写作不止

作者 :whispar
专栏 :小题练手

刷题,面试,求职,快来牛客网一起成为offer收割机!

点击注册收割offer


目录

 BM1 反转链表

 BM84 最长公共前缀

 BM83 字符串变形

 BM12 单链表的排序


BM1 反转链表

【牛客小题练手】- - - Java小题_第1张图片 1.  使用栈解决

链表的反转是老生常谈的一个问题了,同时也是面试中常考的一道题。最简单的一种方式就是使用栈,因为栈是先进后出的。实现原理就是把链表节点一个个入栈,当全部入栈完之后再一个个出栈,出栈的时候在把出栈的结点串成一个新的链表。

import java.util.Stack;
public class Solution {
public ListNode ReverseList(ListNode head) {
    Stack stack= new Stack<>();
   
    while (head != null) {
        stack.push(head);
        head = head.next;
    }
    if (stack.isEmpty())
        return null;
    ListNode node = stack.pop();
    ListNode dummy = node;
  
    while (!stack.isEmpty()) {
        ListNode tempNode = stack.pop();
        node.next = tempNode;
        node = node.next;
    }
  
    node.next = null;
    return dummy;
}
}

2.  双链表求解

双链表求解是把原链表的结点一个个摘掉,每次摘掉的链表都让他成为新的链表的头结点,然后更新新链表。

public ListNode ReverseList(ListNode head) {
    ListNode newHead = null;
    while (head != null) {
        ListNode temp = head.next;
        head.next = newHead;
 
        newHead = head;
        head = temp;
    }
    return newHead;
}

BM84 最长公共前缀

【牛客小题练手】- - - Java小题_第2张图片

import java.util.*;

public class Solution {
    public String longestCommonPrefix (String[] strs) {
        if(strs.length==0 || strs==null){
            return "";
        }

        int rows = strs.length;
        int cols = strs[0].length();
        //开始扫描
        for(int i=0;i
  • 将字符串数组看作一个二维空间,每一次从第一列开始。
  • 确定所有字符子串中第一列字符。
  • 逐层扫描后面每一列,遇到不同字符停止扫描。

BM83 字符串变形

【牛客小题练手】- - - Java小题_第3张图片

import java.util.*;
public class Solution {
    public String trans(String s, int n) {
        if(n==0) 
            return s;
        StringBuffer res=new StringBuffer();
        for(int i = 0; i < n; i++){
  
            if(s.charAt(i) <= 'Z' && s.charAt(i) >= 'A')   
                res.append((char)(s.charAt(i) - 'A' + 'a'));
            else if(s.charAt(i) >= 'a' && s.charAt(i) <= 'z') 
                res.append((char)(s.charAt(i) - 'a' + 'A'));
            else 
            
                res.append(s.charAt(i));  
        } 

        res = res.reverse();
        for (int i = 0; i < n; i++){
            int j = i;
            while(j < n && res.charAt(j) != ' ')  
                j++;
            String temp = res.substring(i,j);
            StringBuffer buffer = new StringBuffer(temp);
            temp = buffer.reverse().toString();
            res.replace(i,j,temp);
            i = j;
        }
        return res.toString();
    }
}
  • 遍历字符串,遇到小写字母,转换成大写,遇到大写字母,转换成小写,遇到空格正常不变。
  • 第一次反转整个字符串,这样基本的单词逆序就有了,但是每个单词的字符也是逆的。
  • 再次遍历字符串,以每个空间为界,将每个单词反转回正常。

 BM12 单链表的排序

【牛客小题练手】- - - Java小题_第4张图片

import java.util.*;
public class Solution {
    public ListNode sortInList (ListNode head) {
        ArrayList nums = new ArrayList(); 
        ListNode p = head;
        while(p != null){ 
            nums.add(p.val);
            p = p.next;
        }
        p = head;
        Collections.sort(nums); 
        //遍历数组
        for(int i = 0; i < nums.size(); i++){ 
            p.val = nums.get(i); 
            p = p.next;
        }
        return head;
    }
}

  • 遍历链表,将节点值加入数组。
  • 使用内置的排序函数对数组进行排序。
  • 依次遍历数组和链表,按照位置将链表中的节点值修改为排序后的数组值。

 如果文章对你有帮助,请多多点赞、收藏、评论、关注支持!!   

ced485cbb11e458d81a746890b32cf3f.gif

你可能感兴趣的:(小题练手,链表,数据结构,算法,java,开发语言)