美文网首页
数据结构与算法整理一

数据结构与算法整理一

作者: gqs已经存在 | 来源:发表于2021-05-10 20:51 被阅读0次

数组

// 找出某个目标数 在一二维数组中是否存在 
public boolean find(int target,int [][] array){

    if(array == null || array.length <= 0){
        return false;
    }

    int row = array.length - 1;

    int col = 0;
    int cols = array[0].length;

    while(row >= 0 && col < cols){
        int cur = array[row][col];
        if(cur < target){
            col ++;
        }else if(cur > target){
            row --;
        }else{
            return true;
        }
    }

    return false;
}

//求连续子列 最大和
int MaxSubSeqSub(int [] a,int N){
        int thisSum = 0;
        int maxSum = 0;
        for(int i = 0;i < N;i++){
            thisSum +=a[i];
            if(thisSum > maxSum){
                maxSum = thisSum;
            }else if(thisSum < 0){
                thisSum = 0;
            }
        }
        return maxSum;
    }

链表

 // 单链表节点的结构
public static class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}
//链表反转
ListNode reverse(ListNode head) {
        if (head.next == null) return head;
        ListNode last = reverse(head.next);
        head.next.next = head;
        head.next = null;
        return last;
    }

//反转链表前n 个节点
ListNode successor = null;
    ListNode reverseN(ListNode head,int n){
        if(n == 1){
            //记录第 n+1 个节点
            successor = head.next;
            return head;
        }

        //以head.next 为起点,需要反转前n-1个节点
        ListNode last = reverseN(head.next, n - 1);
        head.next.next = head;
        head.next = successor;
        return last;
    }

// 打印链表
static void printListNode(ListNode head){
        if(head.next == null){
            System.out.println(head.val);
            System.out.println("end");
            return;
        }
        ListNode node = head.next;
        System.out.println(head.val);
        printListNode(node);
        System.out.println(" end "+node.val);
    }

相关文章

  • 数据结构与算法 - 查找

    数据结构与算法系列文章数据结构与算法 - 时间复杂度数据结构与算法 - 线性表数据结构与算法 - 树形结构数据结构...

  • 思维导图之数据结构+算法

    数据结构+算法 = 程序 数据结构比较 参考文章 数据结构与算法数据结构与算法(java)

  • 数据结构与算法 - 树形结构

    数据结构与算法系列文章数据结构与算法 - 时间复杂度数据结构与算法 - 线性表数据结构与算法 - 树形结构 目录 ...

  • 最新完整数据结构与算法

    最新完整数据结构与算法 P11_课程介绍 P22_数据结构与算法概述_数据结构 P33_数据结构与算法概述_算法 ...

  • 数据结构与算法

    数据结构与算法之美 数据结构与算法之美1--如何学数据结构与算法之美2--复杂度分析(上)数据结构与算法之美3--...

  • 《数据结构与算法》学习笔记之总纲

    数据结构与算法学习笔记 一、学习资源 github无疑是我们学习编程与代码知识的一个良好平台,以下整理《数据结构与...

  • Swift 实现 7 种常见的排序算法

    排序算法可以说是数据结构与算法当中最为基础的部分,针对的是数组这一数据结构。将数组中的无序数据元素通过算法整理为有...

  • 算法与数据结构(1),List

    算法与数据结构(1),List 算法与数据结构(2),Map 算法与数据结构(3),并发结构 习惯了,深夜更新博客...

  • Java数据结构算法(五)排序

    算法这点粗略整理一下,后面完善 Java数据结构算法(一)链表 Java数据结构算法(二)栈和队列 Java数据结...

  • Java数据结构算法(二)栈和队列

    本文旨作于收集整理使用!! 导航 Java数据结构算法(一)链表 Java数据结构算法(三)树 Java数据结构算...

网友评论

      本文标题:数据结构与算法整理一

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