1. 上一道题的另一种解法(最大连续子序列和)
可以引入前缀和的概念,传建一个比原数组长度大一的原数组。只需要找出其中,最大的数字和最大数字左边最小的数字,两两相减就可以得到最大前缀和
原数组 = (1,2,3,-6,1)前缀和数组 = (0,1,3, 6,0,1)这里只需要找到最大的数字再减去最大数字左边最小的数字就可以得到最大连续子序列和6 - 0 = 6同理:原数组 = (1,-1,-3, 2, 3,4)前缀和数组 = (0, 1, 0,-3,-1,2,6)6 - (-3) = 9
2. 判断二维数组中对应的位置是否有值
Node.javapublic class Node {private int value;private int key;private Node next;public Node() {}public Node(int key,int value) {this.key = key;this.value = value;}public int getValue() {return value;}public void setValue(int value) {this.value = value;}public int getKey() {return key;}public void setKey(int key) {this.key = key;}public Node getNext() {return next;}public void setNext(Node next) {this.next = next;}}Paixu.javapublic class Paixu {public static void main(String[] args) {//初始条件int[][] arr = new int[10][10];arr[1][1] = 6;Scanner sc = new Scanner(System.in);System.out.println("请输入x轴坐标:");int x = sc.nextInt();System.out.println("请输入y轴坐标:");int y = sc.nextInt();boolean result = cha(arr,x,y);}/*** 1. 普通查找* @param arr* @param x* @param y* @return*/private static boolean cha(int[][] arr, int x, int y) {if (arr[x][y]!=0){return true;}return false;}/*** 2. 数组加链表查找* @param x* @param y* @return*/private static boolean linke_cha(int x,int y){/*** 相当于HashMap源码中containsValue的书写* 1. 首先先遍历查找x* 2. 在x所对应的链表中查找y位置* 3. 判断值是否为0*/ArrayList<LinkedList> array_head = new ArrayList<LinkedList>(10);LinkedList linkedList = array_head.get(x);Node value = (Node)linkedList.get(y);if (value.getValue()!=0){return true;}return false;}/*** 稀疏矩阵没有理解太清楚,随后补上*/}
