Java笔试常见基础算法练习

单链表反转
1.断开节点之间的连接
2.前节点变为党前节点的下一个节点

//节点类
public class Node<E> {
    public Node<E> next;//当前节点的下一个节点
    public E value;//当前节点存储的值

    public Node(E value) {
        this.value = value;
    }
    public void getList(Node<Integer> node) {//输出单链表
        while (node != null) {
            System.out.print(node.value+" ");
            node = node.next;
        }
    }
}

反转单链表

public class ReverLinkedList {
    public Node<Integer> reverLinklist(Node<Integer> head){
        if(head == null){
            return null;
        }
        Node<Integer> pre = null;//当前节点的前一个节点
        Node<Integer> next = null;//当前节点的下一个节点
        while(head != null){
           next = head.next;//现将当前节点的下一个节点保存即断开当前节点与下一个节点的连接
            head.next = pre;//将当前节点的下一个节点换为前一个节点(pre)
            pre = head;//将当前节点作为下一个节点的前节点
            head = next;
        }
        return  pre;//返回当前节点
    }
    //测试类
public static void main(String[] args) {

        Node<Integer> head = new Node<Integer>(1);
        Node<Integer> first = new Node<Integer>(2);
        Node<Integer> second = new Node<Integer>(3);
        head.next = first;
        first.next = second;
        head.getList(head);
        ReverLinkedList rlist = new ReverLinkedList();
        Node<Integer> p =rlist.reverLinklist(head);
        System.out.println();
        p.getList(p);


    }
}

将数组中奇数放于偶数前
找到数组中奇偶相邻的两个数例如:{2,1} 即a[i]为偶数a[i+1]为奇数
将奇数与偶数互换
双层循环实现

public class OddBeforeEven {
    public static void oddBeforeEven (int[] a){
        if(a.length == 0){
            return;
        }
        for(int i =0;i<a.length;i++){
            for(int j=0;j<a.length-1;j++){
                if(a[j]%2==0 && a[j+1]%2!=0){
                    int temp = a[j];
                    a[j] = a[j+1];
                    a[j+1] = temp;
                }
            }
        }
    }
    //测试方法
     public static void main(String[] args) {
        int[] a ={7,2,3,5,4,6,1};
        OddBeforeEven.oddBeforeEven(a);
        System.out.println(Arrays.toString(a));
    }
}

字符串的子字符串或数组的子数组
**分析:**我们可以将字符串中的每个字符看成二叉树的一个节点,根节点为空,每个节点都会有两种选择:要 和 不要 两种选择 。那么我们就可以利用递归实现。
在这里插入图片描述

public class Substring {
  /*字符串子数组
  public static void printAllSubstring(String str) {
        if (str.length() == 0) {
            System.out.println("字符串为空");
        }
        char[] chars = str.toCharArray();
        if(chars.length == 0){
            System.out.println(" ");
        }else{
            String pre = new String(" ");
            printAllSubstring(0,pre,chars);
        }
    }
        public static void printAllSubstring(int i,String pre ,char[] chars){
            if(i == chars.length){
                System.out.println(pre);
                return;
            }
            printAllSubstring(i+1,pre,chars);//放弃当前字符
            printAllSubstring(i+1,pre+String.valueOf(chars[i]),chars);
            //留下当前字符
    }*/
    //数组的子数组
  public static void printAllSubstring(int[] a) {
      if (a.length== 0) {
          System.out.println("数组为空");
      }
      if(a.length == 0){
          System.out.println(" ");
      }else{
          String pre = new String(" ");
          printAllSubstring(0,pre,a);
      }
  }
    public static void printAllSubstring(int i,String pre ,int[] a){
        if(i == a.length){

                for (int j = 0; j < pre.length(); j++) {
                    if (pre.charAt(j) == ' ') {
                        String s = pre.substring(j+1, pre.length());
                        System.out.println(s);
                    }
            }
            return;
        }
        printAllSubstring(i+1,pre,a);
        printAllSubstring(i+1,pre+String.valueOf(a[i]),a);
    }
     public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] a = {1,1,2};
        Substring.printAllSubstring(a);
    }
}

子数组的最大和
动态规划实现

public class FindMaxSumOfSubArray {

   /**
    * 如数组:{6,-3,-2,7,-15,1,2,2}
    * 1.  初始状态:
    * F(0) = 6;
    * res = 6;

    * 2.  i = 1:
    * F(1) = max(F(0) - 3, -3) = max(6-3,  -3) = 3;
    * res = max(F(1), res) = max(3, 6) = 6;

    *     3.  i = 2:
    * F(2) = max(F(1) - 2,  -2) = max(3 - 2,  -2) = 1;
    * res = max(F(2),  res) = max(1, 6) = 6;

    *     4.  i = 3:
    * F(3) = max(F(2) + 7, 7) = max(1 + 7, 7) = 8;
    * res = max(F(3),res) = max(6, 8) = 8;

    *     5.  i = 4:
    * F(4) = max(F(3) - 15,  -15) = max(8 - 15,  -15) = -7;
    * res = max(F(4),  res) = max(-7, 8) = 8;
    * ......以此类推,最终 res 的值为8。
    * **/
   public static int  Maxsub(int[] a){
        if(a.length == 0){
            return 0;
        }
        int max = a[0];//当前子数组中的最大值
        int res = a[0];//已遍历子数组中的最大值
        for(int i =1;i<a.length;i++){
            max = Math.max(max+ a[i],a[i]);
            res = Math.max(max,res);
        }
        return res;}
        public static void main(String[] args) {
        int[] a ={7,2,-3,5,4,6,-1};
        int b = FindMaxSumOfSubArray.Maxsub(a) ;
        System.out.println(b);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值