美文网首页
15道使用频率极高的基础算法题

15道使用频率极高的基础算法题

作者: 7i昂 | 来源:发表于2019-11-12 23:45 被阅读0次

1、合并排序,将两个已经排序的数组合并成一个数组,其中一个数组能容下两个数组的所有元素;

public static int[] merge(int[] a,int [] b) {

        int[] c = new int[a.length+b.length];
        //i用于标记数组a
        int i=0;
        //j用于标记数组b
        int j=0;
        //用于标记数组c
        int k=0;

        //a,b数组都有元素时
        while(i<a.length && j<b.length) {
            if(a[i]<b[j]) {
                c[k++] = a[i++];
            }else {
                c[k++] = b[j++];
            }
        }

        //若a有剩余
        while(i<a.length) {
            c[k++] = a[i++];
        }

        //若b有剩余
        while(j<b.length) {
            c[k++] = b[j++];
        }

        return c;

    }

2、合并两个已经排序的单链表;
3、倒序打印一个单链表;

/**
     * 递归方法逆序打印单链表
     * @param head
     */
    public static void printListReverse1(ListNode head)
    {
        if(head!=null)
        {
            if(head.next!=null)
            {
                printListReverse1(head.next);
            }
        }
        System.out.println(head.val);
    }
    /**
     * 使用栈逆序打印单链表
     * @param head
     */
    public static void printListReverse2(ListNode head)
    {
         Stack<Integer> stack=new Stack<Integer>();
        while(head!=null)
        {
            stack.push(head.val);
            head=head.next;     
        }
        while(!stack.isEmpty())
        {
            System.out.println(stack.pop());
        }
    }

4、给定一个单链表的头指针和一个指定节点的指针,在O(1)时间删除该节点;
5、找到链表倒数第K个节点;
6、反转单链表;
7、通过两个栈实现一个队列;
8、二分查找;
9、快速排序;
10、获得一个int型的数中二进制中的个数;
11、输入一个数组,实现一个函数,让所有奇数都在偶数前面;

public static void reorderOddEven(int[] array){
        if(array==null||array.length<=0)
            throw new RuntimeException("invalid array");
        int begin=0;
        int end=array.length-1;
        while(begin<end){
            while(begin<end&&(array[begin]&1)!=0)
                begin++;
            while(begin<end&&(array[end]&1)==0)
                end--;
            if(begin<end){
                int temp=array[end];
                array[end]=array[begin];
                array[begin]=temp;
            }
        }
    }

12、判断一个字符串是否是另一个字符串的子串;

public static void main(String[] args) {
        String test = "This is test for string";
        System.out.println(test.indexOf("This")); //0
        System.out.println(test.indexOf("is")); //2
        System.out.println(test.indexOf("test")); //8
        System.out.println(test.indexOf("for")); //13
        System.out.println(test.indexOf("for string       "));//-1
        
        if (test.indexOf("This")!=-1){
            //"只要test.indexOf('This')返回的值不是-1说明test字符串中包含字符串'This',相反如果包含返回的值必定是-1"
            System.out.println("存在包含关系,因为返回的值不等于-1");
            
        }else{
            
            System.out.println("不存在包含关系,因为返回的值等于-1");
        }
        
        
        if (test.indexOf("this")!=-1){
            //"只要test.indexOf('this')返回的值不是-1说明test字符串中包含字符串'this',相反如果包含返回的值必定是-1"
            System.out.println("存在包含关系,因为返回的值不等于-1");
            
        }else{
            
            System.out.println("不存在包含关系,因为返回的值等于-1");
        }
        

    }

13、把一个int型数组中的数字拼成一个串,这个串代表的数字最小;

public String PrintMinNumber(int [] numbers) {
   int n;
   String s="";
   ArrayList<Integer> list= new ArrayList<Integer>();
   n=numbers.length;
   for(int i=0;i<n;i++){
      list.add(numbers[i]);
   
   }
   Collections.sort(list, new Comparator<Integer>(){
      public int compare(Integer str1,Integer str2){
         String s1=str1+""+str2;
         String s2=str2+""+str1;
         return s1.compareTo(s2);
      }
   });
   for(int j:list){
      s+=j;
   }
   return s;
}

14、输入一颗二叉树,输出它的镜像(每个节点的左右子节点交换位置);
15、输入两个链表,找到它们第一个公共节点;

相关文章

  • 15道使用频率极高的基础算法题

    1、合并排序,将两个已经排序的数组合并成一个数组,其中一个数组能容下两个数组的所有元素; 2、合并两个已经排序的单...

  • 【算法】二叉树遍历算法总结:前序中序后序遍历

    前言 二叉树遍历是非常经典的算法题,也是二叉树的一道基础算法题。 但是在平常的笔试面试中,其出现的频率其实并不是特...

  • 八大算法

    算法中比较常用的有八种算法,基本算法的题,都是依靠这些基础算法或者结合使用出题的,所以要学会基础算法,才有可能去更...

  • java基础-String类API

    在java基本API中,String类出现的频率极高,很多大公司的算法题都是基于字符串的,所以今天对java中的S...

  • Freecodecamp算法

    今天做到了 Freecodecamp上面的第一个算法题,虽然很基础,但还是记一下自己用的知识每一道算法题都跟你罗列...

  • ARTS-W02 (12.27 - 1.02)

    Algorithm(一道算法题) 这周刷到的算法题:鸡蛋掉落具体描述如下:你将获得 K 个鸡蛋,并可以使用一栋从 ...

  • ListView的Adapter的封装以及ViewHolder的

    ListView是在Android开发中使用频率极高的高级控件,现在看来只要使用频率高的东西,那么就有必要去封装,...

  • [2]十道算法题【Java实现】

    前言 清明不小心就拖了两天没更了~~ 这是十道算法题的第二篇了~上一篇回顾:十道简单算法题 最近在回顾以前使用C写...

  • Android面经| 算法题解

    整理了校招面试算法题,部分《剑指offer》算法题,以及LeetCode算法题,本博文中算法题均使用Java实现校...

  • 关键词:算法结构化

    目前看了三十多道的算法题,以及提炼出基本的算法工具了。只要对一道题进行很好的拆解,每个部分使用能解决问题的算...

网友评论

      本文标题:15道使用频率极高的基础算法题

      本文链接:https://www.haomeiwen.com/subject/jwrcictx.html