面试相关高频算法考点2

21 篇文章 3 订阅
13 篇文章 2 订阅

一、替换空格

牛客链接
描述:
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy
解题思路:根据题目描述中将一个字符串中的每个空格替换成“%20”,则相比于原字符串长度每出现一个空格进行替换时都会增加2个位置的长度,因此我们可以先计算出整个字符串中增加的长度,然后用一个指针指向原来字符串长度的结尾位置,另一个指针指向增加长度之后其字符串长度结尾的位置,依次从后往前遍历字符串,遇到空格时就在后面一个指针处替换空格,直到遍历完整个字符串。
代码示例:

import java.util.*;
public class Solution {
    public String replaceSpace(StringBuffer str) {
        if(str == null){
            return null;
        }
        int count = 0;
    	int len = str.length();
        for(int i = 0;i < str.length();i++){
            if(str.charAt(i) == ' '){
                count++;
            }
        }
        int new_len = len + 2 * count;
        int old_index = len - 1;
        int new_index = new_len - 1;
        str.setLength(new_len);//设置新的字符串大小,防止越界
        while(old_index >= 0 && new_index >= 0){
                if(str.charAt(old_index) == ' '){
                    str.setCharAt(new_index--,'0');
                    str.setCharAt(new_index--,'2');
                    str.setCharAt(new_index--,'%');
                    old_index--;
                }else{
                    str.setCharAt(new_index--,str.charAt(old_index));
                    old_index--;
                }
        }
        return str.toString();
    }
}

二、从尾到头打印链表

牛客链接
描述:
输入一个链表的头节点,按链表从尾到头的顺序返回每个节点的值(用数组返回)。

如输入{1,2,3}的链表如下图:
在这里插入图片描述
返回一个数组为[3,2,1];0 <= 链表长度 <= 10000;
方法一:用栈,根据栈先进后出的原则,可以把链表中的元素都入栈,然后再依次弹出即可得到逆置链表的目的。

import java.util.*;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> list = new ArrayList<>();
        Stack <Integer> stack = new Stack<>();
        if(listNode == null){
            return list;
        }
        while(listNode != null){
            stack.push(listNode.val);
            listNode = listNode.next;
        }
        while(!stack.empty()){
            list.add(stack.pop());
        }
        return list;
    }
}

方法二:逆置数组,将链表里面的元素都放在一个list当中,然后将其第一个元素与最后一个元素依次互换,直到换完整个链表里面的元素,即可达到逆置链表的目的。

import java.util.*;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> list = new ArrayList<>();
         while(listNode != null){
            list.add(listNode.val);
            listNode = listNode.next;
            }
        int i = 0;
        int j = list.size() - 1;
        while(i < j){
            int tmp = list.get(i);
            list.set(i,list.get(j));
            list.set(j,tmp);
            i++;
            j--;
        }
        return list;
    }
}

方法三:递归方式

import java.util.*;
public class Solution {
     public void printListFromTailToHeadCore(ArrayList<Integer> list,ListNode listNode){
         if(listNode == null){
             return;
         }
         printListFromTailToHeadCore(list,listNode.next);
         list.add(listNode.val);
     }
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> list = new ArrayList<>();
        printListFromTailToHeadCore(list,listNode);
        return list;
     }
}

三、斐波那契数列

牛客链接
方法一:迭代方法

public class Solution {
    public int Fibonacci(int n) {
        if(n == 0)
            return 0;
        int first = 1;
        int second = 1;
        int third = 1;//不能设置为0,如果n = 2,则直接输出它的值1
        while(n > 2){
            third = first + second;
            first = second;
            second = third;
            n--;
        }
        return third;
    }
}

上述迭代方法会在n达到一定数时计算量增大。
方法二:递归方法

  if (n == 1 || n == 2){
            return 1;
        }
        return Fibonacci(n -1) + Fibonacci(n - 2);
import java.util.*;
public class Solution {
      Map<Integer,Integer> map = new HashMap<>();
    public int Fibonacci(int n) {
        if(n == 0 || n == 1){
            return n;
        }
        int pre = 0;
        if(map.containsKey(n - 1)){
           pre = map.get(n - 1);
        }else{
           pre = Fibonacci(n - 1);
            map.put(n - 1,pre);
        }
        int ppre = 0;
        if(map.containsKey(n - 2)){
          ppre = map.get(n - 2);
        }else{
            ppre = Fibonacci(n - 2);
            map.put(n - 2,ppre);
        }
        return pre + ppre;
    }
}

方法三:动态规划方法
状态:F(n)
状态递推:F(n)=F(n-1)+F(n-2)
初始值:F(1)=F(2)=1
返回结果:F(N)

public class Solution {
    public int Fibonacci(int n) {
        if(n <= 0){
            return 0;
        }
        int[] arr = new int[n + 1];
        arr[0] = 0;
        arr[1] = 1;
        for(int i = 2; i <= n;i++){
            arr[i] = arr[i - 1] + arr[i -2];
        }
        return arr[n];
     }
  }

四、青蛙台阶跳

方法一:简单动归方式
状态定义:f(i): 跳到i台阶的总跳法
状态递推:f(i) = f(i-1)+f(i-2)
初始状态: f(0) = 1(0台阶,就是起点,到达0台阶的方法有一种,就是不跳[这里可能有点奇怪,但是想想,如果方法次数为0,就说明不可能开始…]), f(1) = 1;

public class Solution {
    public int jumpFloor(int target) {
        int[] tmp = new int[target + 1];
        tmp[0] = 1;
        tmp[1] = 1;
        for(int i = 2;i <= target;i++){
            tmp[i] = tmp[i - 1] + tmp[i - 2];
        }
        return tmp[target];
    }
}

方法二:迭代方法

public class Solution {
    public int jumpFloor(int target) {
        int first = 1;
        int second = 2;
        int third = target;
        while(target > 2){
            third = first + second;
            first = second;
            second = third;
            target--;
        }
        return third;
    }
}

五、矩形覆盖

牛客链接
描述:
我们可以用 2*1 的小矩形横着或者竖着去覆盖更大的矩形。请问用 n2*1 的小矩形无重叠地覆盖一个 2*n 的大矩形,从同一个方向看总共有多少种不同的方法?
采用动态规划的方法解析:
用n个21的小矩形无重叠地覆盖一个2n的大矩形,每次放置的时候,无非两种放法,横着放或竖着放,其中,横着放一个之后,下一个的放法也就确定了,故虽然放置了两个矩形,但属于同一种放法;
其中,竖着放一个之后,本轮放置也就完成了,也属于一种方法;所以,当2*n的大矩形被放满的时候,它无非就是从上面两种放置方法放置来的。
状态定义f(n) : 用n2*1的小矩形无重叠地覆盖一个2*n的大矩形所用的总方法数;
状态递推f(n) = f(n-1)【最后一个竖着放】 + f(n-2)【最后两个横着放】;
初始化f(1) = 1,f(2) = 2,f(0)=1,注意f(0)我们这个可以不考虑,如果考虑值设为1。
代码示例:

public class Solution {
    public int rectCover(int target) {
        if(target < 2){
            return target;
        }
        int[] tmp = new int[target + 1];
        tmp[0] = 1;
        tmp[1] = 1;
        tmp[2] = 2;
        for(int i = 3;i <= target;i++){
            tmp[i] = tmp[i - 1] + tmp[i - 2];
        }
        return tmp[target];
    }
}

六、二进制中1的个数

牛客链接
描述:
输入一个整数 n ,输出该数32位二进制表示中1的个数。其中负数用补码表示。
代码示例:

public class Solution {
    public int NumberOf1(int n) {
        int count = 0;
        while(n != 0){
            n = n & (n - 1);
            count++;
        }
        return count;
    }
}

七、返回链表中倒数第K个节点

牛客链接
思路:双指针,先将快指针向前走k步,这样快慢指针之间就相差k的距离,然后同时将快慢指针向后移动,直到快指针为空,这时慢指针指向的位置就是倒数第K个节点的位置。

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        if(head == null || k < 0){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(k != 0 && fast != null){
            fast = fast.next;
            k--;
        }
        while(fast != null){
            fast = fast.next;
            slow = slow.next;
        }
        return k > 0 ? null : slow;
    }
}

八、反转链表

牛客链接
描述:
给定一个单链表的头结点pHead(该头节点是有值的,比如在下图,它的val是1),长度为n,反转该链表后,返回新链表的表头。
在这里插入图片描述
方法一:

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        ListNode left = head;
        ListNode mid = left.next;
        ListNode right = mid.next;
        while(right != null){
            mid.next = left;
            left = mid;
            mid = right;
            right = right.next;
        }
        //当翻转到最后只剩下两个节点或者链表本来就只有两个节点
        mid.next = left;
        head.next = null;
        head = mid;
        return mid;
    }
}

方法二:

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode ReverseList(ListNode head) {
     if(head == null){
            return null;
        }
        ListNode pre = null;
        ListNode cur = head;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = pre;
            pre = cur;
            cur = curNext;
        }
        return pre;
    }
}

九、合并两个排序的链表

牛客链接
方法一:直接在原来两个升序的链表中进行合并,无序创建新的链表

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode headA,ListNode headB) {
        if(headA == null) return headB;
        if(headB == null) return headA;
        ListNode newHead = new ListNode(-1);
        ListNode tmp = newHead;
        while(headA != null && headB != null ){
            if(headA.val < headB.val){
                tmp.next = headA;
                headA = headA.next;
                tmp = tmp.next;
            }else{
                tmp.next = headB;
                headB = headB.next;
                tmp = tmp.next;
            }
        }
        if(headA != null){
            tmp.next = headA;
        }
        if(headB != null){
            tmp.next = headB;
        }
        return newHead.next;
    }
}

方法二:合并中,无非是比较各自首节点谁小,就把该节点从原链表中删除,再尾插到新节点处,比较中,两个链表任何一个都不能为空。

public class Solution {
    public ListNode Merge(ListNode headA,ListNode headB) {
        if(headA == null) return headB;
        if(headB == null) return headA;
        ListNode new_head = null;
        ListNode new_tail = null;
        while(headA != null && headB != null ){
        ListNode p = headA.val < headB.val ? headA : headB;
            if(p == headA){
                headA = headA.next;
            }else{
                headB = headB.next;
            }
            if(new_head == null){
                new_head = p;
                new_tail = p;
            }else{
                new_tail.next = p;
                new_tail = p;
            }
        }
        if(headA != null){
            new_tail.next = headA;
        }
        if(headB != null){
            new_tail.next = headB;
        }
        return new_head;
    }
 }

方法三:使用递归的方法

public class Solution {
    public ListNode Merge(ListNode headA,ListNode headB) {
        if(headA == null) return headB;
        if(headB == null) return headA;
        ListNode cur = null;
            if(headA.val < headB.val){
                cur = headA;
                headA = headA.next;
            }else{
                cur = headB;
                headB = headB.next;
            }
        cur.next = Merge(headA,headB);
        return cur;
    }
 }

十、删除链表中重复的节点

牛客链接
描述:
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表 1->2->3->3->4->4->5 处理后为 1->2->5
方法一:

/*
 public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public ListNode deleteDuplication(ListNode pHead) {
        ListNode cur = pHead;
        ListNode newHead = new ListNode(-1);//虚拟节点
        ListNode tmp = newHead;
        while(cur != null){
            if(cur.next != null && cur.val == cur.next.val){
                while(cur.next != null && cur.val == cur.next.val){
                    cur = cur.next;
                }
                //多走一步,要走到不是重复元素的后一个
                cur = cur.next;
            }else{
                tmp.next = cur;
                cur = cur.next;
                tmp = tmp.next;
            }
        }
        //防止最后一个节点的值也是重复的
        tmp.next = null;
        return newHead.next;
    }
}

方法二:

public class Solution {
    public ListNode deleteDuplication(ListNode pHead) {
        if(pHead == null){
            return pHead;
        }
        ListNode tmp = new ListNode(0);//虚拟节点
        tmp.next = pHead;
        
        ListNode pre = tmp;
        ListNode last = pre.next;
        while(last != null){
            while(last.next != null && last.val != last.next.val){
                   pre = pre.next;
                   last = last.next;
            }
             while(last.next != null && last.val == last.next.val){
                   last = last.next;
            }
            if(pre.next != last){
                pre.next = last.next;
            }
            last = last.next;
        }
     return tmp.next;  
   }
}    
  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值