小工具知识网

当前位置: 首页 > 行业知识 > 算法题目记录

行业知识

算法题目记录

2026-11-25 19:52:43 来源:互联网转载

1.杂题

1.1 计算二进制中1的个数

AcWing

import java.util.*;public class Main{    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int n = sc.nextInt();        int[] num = new int[n];        for (int i = 0; i  

1.2 颜色分类

class Solution {    public void sortColors(int[] nums) {        int l = -1;//左指针        int r = nums.length;//右指针        int i = 0;//哨兵指针        while (i  1) {//如果比1大,就和右指针前一个数交换位置。但i不变,因为nums[i]是刚交换过去的,还没比较                     int temp = nums[i];                nums[i] = nums[r-1];                nums[r-1] = temp;                r--;            }            else  {                i++;            }        }            }}

2.链表

2.1判断是否是回文链表

1.方法一:利用栈反转链表

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode() {} *     ListNode(int val) { this.val = val; } *     ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */class Solution {    public boolean isPalindrome(ListNode head) {        Stack listNodes = new Stack();        ListNode p = head;        //利用栈反转链表,判断是否是回文链表        while (p != null) {//将链表中所有元素入栈            listNodes.push(p);            p = p.next;        }        while (!listNodes.empty()) {            if (listNodes.pop().val == head.val) {//                head = head.next;            } else {                return false;            }        }        return true;    }}

2.方法2:利用快慢指针

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode() {} *     ListNode(int val) { this.val = val; } *     ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */class Solution {    public boolean isPalindrome(ListNode head) {    //代表快指针,一次走两步        ListNode fast = head;        //代表慢指针,一次走一步        ListNode slow = head;        while (fast.next != null && fast.next.next != null) {            fast = fast.next.next;            slow = slow.next;        }        //退出循环时,如果链表节点是奇数个,快指针在尾节点,慢指针在中点。如果是偶数个,快指针还是在尾节点,慢指针在中点前一个。        //把右半部分链表反转        slow = reverseList(slow.next);        while (slow != null) {            if (head.val != slow.val) return false;//值不相同,直接返回false            head = head.next;            slow = slow.next;        }        return true;    }    //反转链表    public static ListNode reverseList(ListNode head) {        ListNode cur = head;        ListNode pre = null;        while (cur != null) {            ListNode temp = cur.next;            cur.next = pre;            pre = cur;            cur = temp;        }        return pre;    }}

2.2 模板题:反转链表

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode() {} *     ListNode(int val) { this.val = val; } *     ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */class Solution {    public ListNode reverseList(ListNode head) {        //cur:用于遍历链表元素的指针,代表当前遍历到的节点,初始化当然为head了        ListNode cur = head;        //pre:代表当前cur节点,反转后应该指向的节点。因为cur初始在head,反转以后就是尾节点了指向null,所以pre初始化为null        ListNode pre = null;        while(cur != null){//当元素还没遍历完的时候            //在cur指向pre前,用于保存cur.next,防止链表找不到了。            ListNode temp = cur.next;            //让当前节点cur,指向pre            cur.next = pre;            //让pre变为反转链表的最前面一个节点            pre = cur;            //让cur移动到原链表的头节点            cur = temp;        }        // 注意:pre的含义还是反转链表的头节点!        return pre;    }}

复杂度分析:

时间复杂度 O(N)O(N)O(N) : 遍历链表使用线性大小时间。

空间复杂度 O(1)O(1)O(1) : 变量 pre 和 cur 使用常数大小额外空间。

已经是最优的解法了,还有一种递归方法就不赘述了。

2.3 分割链表(将链表分为小于某个值,等于某个值,大于某个值)

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode() {} *     ListNode(int val) { this.val = val; } *     ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */class Solution {    public ListNode partition(ListNode head, int x) {if (head == null || head.next == null) return head;        //代表小于目标值区域的头和尾        ListNode h1 = null;        ListNode t1 = null;        //代表大于等于目标值的头和尾        ListNode h2 = null;        ListNode t2 = null;        //用于保存head的下一个节点        //注意:这里最后拼接好了以后,小于区域的头就是整个链表的新的头节点,因此,head可以作为遍历链表的指针。        ListNode next = head.next;        while (head != null) {//遍历            next = head.next;            head.next = null;            if (head.val  

2.4 随机链表的赋值

/*// Definition for a Node.class Node {    int val;    Node next;    Node random;    public Node(int val) {        this.val = val;        this.next = null;        this.random = null;    }}*/class Solution {    public Node copyRandomList(Node head) {        //创建一个map,key为老链表的节点。val为新链表的节点        HashMap map = new HashMap();        Node cur = head;        //遍历链表,设置map的key和value        while(cur != null){            map.put(cur,new Node(cur.val));            cur = cur.next;        }        cur = head;        //再次遍历老链表,给新链表设置每一个节点的next和random        while(cur != null){            //cur 老链表节点            //map.get(cur) cur对应的新链表            map.get(cur).next = map.get(cur.next);//设置新链表的next            map.get(cur).random = map.get(cur.random);//设置新链表的random            cur = cur.next;        }        return map.get(head);    }}

2.5环形链表的判断

方法一:利用HashSet集合。

思路:遍历当前链表,每次遍历判断当前节点是否已经存在于set集合中。如果不存在,则把当前节点放入集合。如果已经存在,说明当前节点就是第一个入环节点。

/** * 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) {        //创建一个set,用于存放链表中已经遍历了的节点        HashSet set = new HashSet();        while(head != null){            //如果当前节点已经存在于set,说明存在环形结构            if(set.contains(head)) return true;            set.add(head);            head = head.next;        }        return false;    }}

方法二:快慢指针

开始时,快慢指针都在头节点的位置。快指针一次走两步,慢指针一次走一步。

如果没有环结构,快指针一定先走到尾节点。

如果有环结构,快慢指针会在换里相遇。而相遇所要走的卷数不会大于两圈。

相遇以后,快指针/慢指针到头节点的位置。两个指针开始一次走一步。最终两个指针会在第一次入换节点相遇!(原理就不证明了)

/** * Definition for singly-linked list. * class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {    public ListNode detectCycle(ListNode head) {        if(head == null || head.next == null || head.next.next == null) return null;        //定义慢指针,一次走一步        ListNode n1 = head.next;        //定义快指针,一次走两步        ListNode n2 = head.next.next;        while(n1 != n2){//当n1 n2不相遇时循环,所以我开始时没有把两个指针都设置在头节点的位置            if(n2.next == null || n2.next.next == null){//说明没有环结构,直接返回空                return null;            }            n1 = n1.next;//慢指针一次走一步            n2 = n2.next.next;//慢指针一次走两步        }        //快指针移到头节点,开始一次走一步        n2 = head;        while(n1 != n2){//当两个指针相遇时,就走到了第一个入环节点            n1 = n1.next;            n2 = n2.next;        }        return n1;    }}

2.6 链表相交

思路:两个单链表,如何判断有没有相交点呢?

1.先遍历两个链表,到尾节点时停止。如果这时候,两个链表的尾节点都不想等。说明二者不相交。

2.如果二者尾节点是同一个,则计算二者链表长度的差值。让长的链表先走差值个距离。然后,短的链表从头开始走,二者一定会在相交点相遇!

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {        //定义两个指针用于遍历两条链表        ListNode cur1 = headA;        ListNode cur2 = headB;        int n = 0;//用于记录两条链表的差值        while(cur1.next != null){            cur1 = cur1.next;            n++;        }        while(cur2.next != null){            cur2 = cur2.next;            n--;        }        if(cur1 != cur2){//尾节点都不想等,说明二者不相交            return null;        }        //这样遍历完两条链表,n就是两条链表的长度差        cur1 = n > 0 ? headA : headB;//让cur1指向两条链表中长的那一条        cur2 = cur1 == headA ? headB : headA;//让cur2指向两条链表中短的那一条        n = Math.abs(n);//n取绝对值        while(n != 0){//让长的那条链表先移动两条链表差值的距离,再一起走,就会在相交部分汇合!            cur1 = cur1.next;            n--;        }        while(cur1 != cur2){            cur1 = cur1.next;            cur2 = cur2.next;        }        return cur1;    }}

3.二叉树

3.1二叉树的最大深度

思路:二叉树的最大深度 = 根节点的最大高度。因此本题可以转换为求二叉树的最大高度。

        而求高度的时候应该采用后序遍历。遍历顺序为:左右中。每次遍历的节点按后序遍历顺序,先收集左右孩子的最大高度,再最后处理当前节点的最大高度!因此用后序遍历。

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode() {} *     TreeNode(int val) { this.val = val; } *     TreeNode(int val, TreeNode left, TreeNode right) { *         this.val = val; *         this.left = left; *         this.right = right; *     } * } */class Solution {    public int maxDepth(TreeNode root) {        //注意:这题虽然求的是最大的深度,但我们可以转换思路。求树的最大深度 = 根节点的最大高度!        if(root == null) return 0;        //当前节点左孩子的高度        int leftHeight = maxDepth(root.left);        //当前节点右孩子的高度        int rightHeight = maxDepth(root.right);        return Math.max(leftHeight,rightHeight) + 1;//当前节点的最大高度就是左右孩子中更高的那个+1    }}

3.2 搜索二叉树的判断

思路:

        首先我们知道二叉搜索树的性质:任何一个节点的左子树的所有节点的值都小于该节点的值,右子树的所有节点的值都大于该节点的值。

        由这个性质我们可以知道,对于一个二叉搜索树,中序遍历这个树,得到的结构一定是升序的!

方法一:利用额外的集合,先中序遍历整个树,把每个值取到。再判断集合中是否为升序排序。

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode() {} *     TreeNode(int val) { this.val = val; } *     TreeNode(int val, TreeNode left, TreeNode right) { *         this.val = val; *         this.left = left; *         this.right = right; *     } * } */class Solution {    public static boolean isValidBST(TreeNode root) {        if(root.left == null && root.right == null) return true;        ArrayList arr = new ArrayList();//用于存放每个节点值的集合        f(root,arr);        for (int i = 0; i  

3.3 判断完全二叉树

先说下性质:

满二叉树:在一颗二叉树中,如果每个结点都存在左子树和右子树,并且所有叶节点都在同一层上,这样的树为满二叉树。

完全二叉树:相同深度的满二叉树的所有结点(不包含叶子)在该树上都有相应的节点(包含叶子)与之对应且所有左子树先存在,才会存在右子树,然后才会存在下层子树的情况,这样的树为完全二叉树 。

可根据下图区分:

思路:层序遍历,根据完全二叉树的性质。

1.当有节点存在有右孩子没左孩子的时候,直接返回false

2.当遍历到第一个叶子节点时,要确保接下来每一个节点都是叶子节点!

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode() {} *     TreeNode(int val) { this.val = val; } *     TreeNode(int val, TreeNode left, TreeNode right) { *         this.val = val; *         this.left = left; *         this.right = right; *     } * } */class Solution {    public boolean isCompleteTree(TreeNode root) {         if (root == null) return true;        //创建一个队列用来做层序遍历        LinkedList queue = new LinkedList();        queue.add(root);        TreeNode l = null;//代表当前节点的左孩子        TreeNode r = null;//代表当前节点的右孩子        Boolean leaf = false;//一个开关,代表当前有没有遍历到叶子节点        while (!queue.isEmpty()) {            root = queue.poll();            l = root.left;            r = root.right;            if (             (leaf && (l != null || r != null))//前面已经存在叶子节点了,但当前节点不是叶子节点                            ||             (l == null && r != null)//有右无左直接返回false            ) return false;            if (l == null || r == null) leaf = true;//如果当前节点是叶子节点            if (l != null) queue.add(l);            if (r != null) queue.add(r);        }        return true;    }}

3.4判断平衡二叉树

思路:

根据平衡二叉树的性质,判断当前节点下的树是不是平衡二叉树,只要做到一下几点判断:

1.左孩子要是平衡二叉树

2.右孩子要是平衡二叉树

3.左右孩子的高度差小于等于1

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode() {} *     TreeNode(int val) { this.val = val; } *     TreeNode(int val, TreeNode left, TreeNode right) { *         this.val = val; *         this.left = left; *         this.right = right; *     } * } */class Solution {    public boolean isBalanced(TreeNode root) {        if (root == null) return true;        boolean leftBalanced = isBalanced(root.left);//判断当前节点左子树是不是平衡二叉树        boolean rightBalanced = isBalanced(root.right);//判断当前节点右子树是不是平衡二叉树        int leftHeight = getHeight(root.left);//获取左子树高度        int rightHeight = getHeight(root.right);//获取右子树高度        //只有当左右子树都为平衡二叉树且左右子树高度差

上一篇:如何重置ts服务器?

下一篇:服务器主机的温度怎么调?