zoukankan      html  css  js  c++  java
  • 算法笔试题2-Java

    (1)翻转链表

    链表节点定义:
    public class ListNode<T> {
        T val;
        ListNode next;
    }
    翻转链表关键在于处理中间状态。中间状态是有一部分链表已翻转(用head表示),一部分链表未翻转(用next表示),将next指向的节点加入到head指向的节点,
    并且head移动到next的位置,next移动到next.next的位置,此时又回到中间状态。一直迭代直到next为null,此时链表全部已翻转,head指向的就是翻转链表的头。
    public ListNode reverse(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode next = head.next;
        head.next = null;
        while (next != null) {
            ListNode tmp1 = head;
            head = next;
            ListNode tmp2 = next.next;
            next.next = tmp1;
            next = tmp2;
        }
        return head;
    }

    (2)小易有一个长度为N的正整数数列A = {A[1], A[2], A[3]..., A[N]}。
    牛博士给小易出了一个难题:
    对数列A进行重新排列,使数列A满足所有的A[i] * A[i + 1](1 ≤ i ≤ N - 1)都是4的倍数。
    小易现在需要判断一个数列是否可以重排之后满足牛博士的要求。

    输入描述:
    输入的第一行为数列的个数t(1 ≤ t ≤ 10),
    接下来每两行描述一个数列A,第一行为数列长度n(1 ≤ n ≤ 10^5)
    第二行为n个正整数A[i](1 ≤ A[i] ≤ 10^9)
    输出描述:
    对于每个数列输出一行表示是否可以满足牛博士要求,如果可以输出Yes,否则输出No。
    输入例子1:
    2
    3
    1 10 100
    4
    1 2 3 4
    输出例子1:
    Yes
    No
    /**
    本题关键是确定所有相邻两数相乘能被4整除的条件,设能被4整除的数个数为t_4,能被2整除不能被4整除的数个数为t_2,其它数个数为t_1,则能被4整除需要满足如下条件:
    (1)t_2>0时,t_4>=t1,此时把所有t_2数排在左边,t_4/t_1间隔排在右边即可,t_4/t_1数不存在也不影响
    (2)t_2=0时,t_4必须有,则t_4>=1,把t_4插入所有t_1数中间需要的t_4数量最少,此时t_4=t_1-1,所以t_4>=t1-1,t_4数量多无影响
    */
    import
    java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); for (int i = 0; i < t; i++) { int count = scanner.nextInt(); int[] arr = new int[count]; for (int j = 0; j < count; j++) { arr[j] = scanner.nextInt(); } int t_4 = 0, t_2 = 0, t_1 = 0; for(int num: arr) { int n = num; if (n % 4 == 0) { t_4++; } else if (n % 2 == 0) { t_2++; } else { t_1++; } } boolean can = false; if (t_2 > 0) { can = t_4 >= t_1; } else { can = t_4 > 0 && (t_4 >= t_1 - 1); } System.out.println(can ? "Yes" : "No"); } } }

    (3)归并两个排好序的链表

    /*
    public class ListNode {
        int val;
        ListNode next = null;
    
        ListNode(int val) {
            this.val = val;
        }
    }*/
    public class Main {
        public ListNode Merge(ListNode list1,ListNode list2) {
            if (list1 == null) {
                return list2;
            }
            if (list2 == null) {
                return list1;
            }
            ListNode head = null, iter = null;
         // 确定头元素
    if (list1.val < list2.val) { head = iter = list1; list1 = list1.next; } else { head = iter = list2; list2 = list2.next; } while (list1 != null && list2 != null) { if (list1.val < list2.val) { iter = iter.next = list1; list1 = list1.next; } else { iter = iter.next = list2; list2 = list2.next; } } while (list1 != null) { iter = iter.next = list1; list1 = list1.next; } while (list2 != null) { iter = iter.next = list2; list2 = list2.next; } return head; } }
  • 相关阅读:
    UEditor用法
    String,StringBuffer与StringBuilder差异??
    TsFltMgr.sys其原因是,该系统蓝屏QQ计算机管理器!
    Linux编程实现守护进程
    开机黑屏 只显示鼠标 电脑黑屏 有只老鼠 举 [我们已经成功地解决了]
    页面背景图像的代码
    动态规划01背包问题
    关键部分CCriticalSection使用
    编程:获取股票实时行情数据大全
    iphone开发教程下载
  • 原文地址:https://www.cnblogs.com/livepeace/p/8214050.html
Copyright © 2011-2022 走看看