算法笔试题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;
    }
}

原文地址:https://www.cnblogs.com/livepeace/p/8214050.html

时间: 2024-08-30 13:08:53

算法笔试题2-Java的相关文章

【笔试题】Java笔试题知识点

Java高概率笔试题知识点 Java语法基础部分 [解析]java命令程序执行字节码文件是,不能跟文件的后缀名! 1.包的名字都应该是由小写单词组成,它们全都是小写字母,即便中间的单词亦是如此 2.类名的第一个字母一定要大写 3.变量名第一个字母应该为小写 4.与属性对应的get, set方法为: set(get)+属性名, 属性名第一个字母为大写. 存储一个24*24点阵的汉字(每个点1bit)需要多少字节? (72) [解析]24*24/8=72.意思是:24*24的结果是二进制位,而八个二

UC算法笔试题

[QQ群: 189191838,对算法和C++感兴趣可以进来] 说实话,昨天UC的笔试题基本全是基础,但是太基础,直接导致很多都不能确定了.看来不管找工作还是找实习,一定要复习到位.好在我也一直是抱着打酱油的味道,实习与否不是特别在意,否则真心要鄙视死自己啦. 好的,言归正传,题目是一道经典的题目:在一个字符串中找到第一个只出现一次的字符.要求 1.必须用C/C++编写代码. 2.不能使用嵌套的for/while循环. 看到这道题时,我坑爹的看成不能使用for/while循环了.我当时想的是,要

Android开发工程师必看笔试题:Java基础选择题(一)

1.在Java中,( )类提供定位本地文件系统,对文件或目录及其属性进行基本操作.(单选) A) FileInputStream B) FileReader C) FileWriter D) File 2. Java中的集合类包括ArrayList.LinkedList.HashMap等类,下列关于集合类描述错误的是()(单选) A) ArrayList和LinkedList均实现了List接口 B) ArrayList的访问速度比LinkedList快 C) 添加和删除元素时,ArrayLis

算法笔试题

1.假定x=500,求下面函数的返回值__6_ . 1 2 3 4 5 6 7 8 9 10 int fun(int x) {     int countx = 0;     while (x)     {         countx++;         x = x & (x – 1);     }     return countx; } 解析:x&(x-1) 就是从右到左遇到的第一个1后面的(包括1)全都变成 0 ,更直观的:会导致x二进制中的1减少一个.本题答案:x二进制中有多少

【笔试题】Java final keyword

Java 知识测试 Java final keyword Question 1 What is the use of final keyword in Java? A. When a class is made final, a sublcass of it can not be created. B. When a method is final, it can not be overridden. C. When a variable is final, it can be assigned

华为算法笔试题

#include <vector> #include <iostream> using namespace std; int main() { int k; cin>>k; vector<vector<int> > arr; vector<int> a; int tmp; while(cin>>tmp){ int i=0; if(arr.size()<i+1) arr.push_back(a); arr[i].pus

[实用向] 算法笔试题中常用的一些函数

1.pow 幂运算(math.h) pow(2,n);//2^n 2.sqrt  开方运算 (math.h) sqrt(n) //开方 x*x = n,返回x https://support.office.com/zh-cn/article/SQRT-%E5%87%BD%E6%95%B0-654975c2-05c4-4831-9a24-2c65e4040fdf -----------------------------随时更新-------------------------------- 原文

java各公司笔试题集1

IBM笔试题 注:IBM笔试题一小时之内完成,题目全部用英文描述,这里用中文表述 一.名词解释 1.Eclipse 2.J2EE 3.EJB 4.Ajax 5.Web service 二.找出以下代码问题 public class test{ public void print(String str){ char[] s=str: } } public class a{ public static void main(String [] args){ puts() } } 三.写出以下输出 pu

java面试笔试题大汇总

java面试笔试题大汇总(一)JAVA相关基础知识 1.面向对象的特征有哪些方面 1.抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时不用部分细节.抽象包括两个方面,一是过程抽象,二是数据抽象. 2.继承:继承是一种联结类的层次模型,并且允许和鼓励类的重用,它提供了一种明确表述共性的方法.对象的一个新类可以从现有的类中派生,这个过程称为类继承.新类继承了原始类的特性,新类称为原始类的派生类(子类),