• 面试中可能遇到的算法题


    1、如何找出单链表中的倒数第K个元素

    两个指针,一个指针比另一个指针先移动k-1个元素,然后同时开始继续网下面移动,先移动的next==null的时候,这个时候第二个指针指向的就是倒数第k个元素

    2、如何从链表中删除重复数据

     private static List<Integer> getRemoveRepeat(List<Integer> ids) {
            Map<Integer, Object> map = new HashMap<>();
            Iterator iterator = ids.iterator();
            while (iterator.hasNext()) {
                Integer key = Integer.valueOf(iterator.next().toString());
                if (map.get(key) != null) {
                    iterator.remove();
                } else {
                    map.put(key, new Object());
                }
            }
            return ids;
        }

    使用map保存数据,map的get操作是一个o(1)的操作,所以会很快,但是空间复杂度会增高

     3、如何找出单链表中的倒数第k个元素 (两个指针法)

    public class LastKNodeFindTest {
        public static void main(String[] args) {
            Node last = new Node(1, null);
            Node last_1 = new Node(2, last);
            Node last_2 = new Node(3, last_1);
            Node last_3 = new Node(4, last_2);
            Node last_4 = new Node(5, last_3);
            Node last_5 = new Node(6, last_4);
            Node last_6 = new Node(7, last_5);
            Node findNode = findKNode(last_6, 6);
            System.out.println(findNode);
        }
    
        private static Node findKNode(Node first, int k) {
            int begin = 0;
            Node knode = first;
            Node knode1 = first;
            while (knode.next != null) {
                if (begin >= k - 1) {
                    knode1 = knode1.next;
                }
                knode = knode.next;
                begin++;
            }
            return knode1;
        }
    
    
        static class Node {
            private int i;
            private Node next;
    
            public Node(int i, Node next) {
                this.i = i;
                this.next = next;
            }
    
            public int getI() {
                return i;
            }
    
            public void setI(int i) {
                this.i = i;
            }
    
            public Node getNext() {
                return next;
            }
    
            public void setNext(Node next) {
                this.next = next;
            }
        }
    }

    如何实现链表的反转

    倒序输出链表

     private static void revise(Node first){
            if(first != null){
                revise(first.next);
                System.out.println(first.i);
            }
        }

    如何寻找数组中的最大值和最小值

    双元素法比较好

    public class MaxMinTest {
    
        static int max = 0;
        static int min = 10;
    
        public static void main(String[] args) {
            int[] arr = {1, 23, 4, 88, 21, 53, 77, 9, 12, 1313, 77, 31};
            maxmin(arr);
            System.out.println(max + min);
        }
    
        private static void maxmin(int[] arr) {
            for (int i = 0; i < arr.length; i = i + 2) {
                int tempMax = 0;
                int tempMin = 0;
                if (i + 1 > arr.length) {
                    max = arr[i] > max ? arr[i] : max;
                    min = arr[i] > min ? min : arr[i];
                } else if (arr[i] > arr[i + 1]) {
                    tempMax = arr[i];
                    tempMin = arr[i + 1];
                } else {
                    tempMax = arr[i + 1];
                    tempMin = arr[i];
                }
                max = tempMax > max ? tempMax : max;
                min = tempMin > min ? min : tempMin;
            }
    
        }
    }
  • 相关阅读:
    解决Linux下Qt程序报『QString::arg: Argument missing: 无法解析SSLv2_client_method中的符号』错误
    中介者模式之我们结婚吧
    POJ 2141 Message Decowding(map)
    Cocos2dx之Box2D具体解释 设置物体回复力
    Android之Window
    opencv源代码分析:cvCreateMTStumpClassifier最优弱分类器的代码框架
    LeetCode总结,二分法一般性总结
    Change Number to English By Reading rule of money
    在多台PC上进行ROS通讯-学习笔记
    编程之美初赛第二场 奇妙的数列
  • 原文地址:https://www.cnblogs.com/zhangchiblog/p/12040484.html
Copyright © 2020-2023  润新知