Android第四次面试总结(基础算法篇)

发布于:2025-03-19 ⋅ 阅读:(11) ⋅ 点赞:(0)

一、反转链表

// 定义链表节点类
class ListNode {
    // 节点存储的值
    int val;
    // 指向下一个节点的引用
    ListNode next;
    // 构造函数,用于初始化节点的值
    ListNode(int x) { val = x; }
}

class Solution {
    // 反转链表的方法
    public ListNode reverseList(ListNode head) {
        // 初始化前一个节点为 null
        ListNode prev = null;
        // 初始化当前节点为头节点
        ListNode current = head;
        // 遍历链表,直到当前节点为 null
        while (current != null) {
            // 保存当前节点的下一个节点
            ListNode nextNode = current.next;
            // 反转当前节点的指针,指向前一个节点
            current.next = prev;
            // 更新前一个节点为当前节点
            prev = current;
            // 更新当前节点为下一个节点
            current = nextNode;
        }
        // 返回反转后链表的头节点
        return prev;
    }

    public static void main(String[] args) {
        // 创建链表 1->2->3->4->5
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);

        // 创建 Solution 类的实例
        Solution solution = new Solution();
        // 反转链表
        ListNode reversedHead = solution.reverseList(head);

        // 打印反转后的链表
        ListNode current = reversedHead;
        while (current != null) {
            System.out.print(current.val);
            if (current.next != null) {
                System.out.print(" -> ");
            }
            current = current.next;
        }
    }
}

二、viewgroup的广度搜索遍历子view(不限制二叉树) 

import android.view.View;
import android.view.ViewGroup;
import java.util.LinkedList;
import java.util.Queue;

/**
 * 该类提供了一个静态方法,用于对 ViewGroup 进行广度优先搜索(BFS)遍历。
 * 广度优先搜索是一种按层次顺序遍历 ViewGroup 及其子 View 的算法。
 */
public class ViewGroupBFS {

    /**
     * 对传入的 ViewGroup 进行广度优先搜索遍历。
     * 遍历过程中会打印每个 View 的简单类名,你可以根据实际需求修改处理逻辑。
     *
     * @param viewGroup 要进行遍历的根 ViewGroup
     */
    public static void traverseViewGroupBFS(ViewGroup viewGroup) {
        // 创建一个队列,用于存储待遍历的 View。
        // 使用 LinkedList 实现 Queue 接口,方便进行入队和出队操作。
        Queue<View> queue = new LinkedList<>();
        // 将传入的根 ViewGroup 添加到队列中,作为遍历的起始点。
        queue.add(viewGroup);

        // 当队列不为空时,继续进行遍历操作。
        while (!queue.isEmpty()) {
            // 从队列的头部取出一个 View 进行处理。
            // poll 方法会移除并返回队列的头部元素,如果队列为空则返回 null。
            View currentView = queue.poll();
            // 处理当前取出的 View。这里只是简单地打印该 View 的简单类名。
            // 你可以根据实际需求修改这部分逻辑,例如更新 UI、执行特定操作等。
            System.out.println("View: " + currentView.getClass().getSimpleName());

            // 检查当前 View 是否为 ViewGroup 类型。
            // 如果是 ViewGroup 类型,说明它可能包含子 View,需要进一步遍历其子 View。
            if (currentView instanceof ViewGroup) {
                // 将当前 View 强制转换为 ViewGroup 类型,以便访问其与子 View 相关的方法。
                ViewGroup currentViewGroup = (ViewGroup) currentView;
                // 获取当前 ViewGroup 包含的子 View 的数量。
                int childCount = currentViewGroup.getChildCount();
                // 遍历当前 ViewGroup 的所有子 View。
                for (int i = 0; i < childCount; i++) {
                    // 获取当前索引位置的子 View。
                    // getChildAt 方法用于根据索引获取 ViewGroup 中的子 View。
                    View childView = currentViewGroup.getChildAt(i);
                    // 将获取到的子 View 添加到队列的尾部,以便后续进行遍历。
                    queue.add(childView);
                }
            }
        }
    }
}    

这是我在Android面试中遇到的算法题 ,希望可以对你有所帮助!!!

感谢观看!!!