1. 上一道题的另一种解法(最大连续子序列和)

可以引入前缀和的概念,传建一个比原数组长度大一的原数组。只需要找出其中,最大的数字最大数字左边最小的数字,两两相减就可以得到最大前缀和

  1. 原数组 = (1,2,3,-6,1)
  2. 前缀和数组 = (0,1,3, 6,0,1)
  3. 这里只需要找到最大的数字再减去最大数字左边最小的数字就可以得到最大连续子序列和
  4. 6 - 0 = 6
  5. 同理:
  6. 原数组 = (1,-1,-3, 2, 3,4)
  7. 前缀和数组 = (0, 1, 0,-3,-1,2,6)
  8. 6 - (-3) = 9

2. 判断二维数组中对应的位置是否有值

  1. Node.java
  2. public class Node {
  3. private int value;
  4. private int key;
  5. private Node next;
  6. public Node() {
  7. }
  8. public Node(int key,int value) {
  9. this.key = key;
  10. this.value = value;
  11. }
  12. public int getValue() {
  13. return value;
  14. }
  15. public void setValue(int value) {
  16. this.value = value;
  17. }
  18. public int getKey() {
  19. return key;
  20. }
  21. public void setKey(int key) {
  22. this.key = key;
  23. }
  24. public Node getNext() {
  25. return next;
  26. }
  27. public void setNext(Node next) {
  28. this.next = next;
  29. }
  30. }
  31. Paixu.java
  32. public class Paixu {
  33. public static void main(String[] args) {
  34. //初始条件
  35. int[][] arr = new int[10][10];
  36. arr[1][1] = 6;
  37. Scanner sc = new Scanner(System.in);
  38. System.out.println("请输入x轴坐标:");
  39. int x = sc.nextInt();
  40. System.out.println("请输入y轴坐标:");
  41. int y = sc.nextInt();
  42. boolean result = cha(arr,x,y);
  43. }
  44. /**
  45. * 1. 普通查找
  46. * @param arr
  47. * @param x
  48. * @param y
  49. * @return
  50. */
  51. private static boolean cha(int[][] arr, int x, int y) {
  52. if (arr[x][y]!=0){
  53. return true;
  54. }
  55. return false;
  56. }
  57. /**
  58. * 2. 数组加链表查找
  59. * @param x
  60. * @param y
  61. * @return
  62. */
  63. private static boolean linke_cha(int x,int y){
  64. /**
  65. * 相当于HashMap源码中containsValue的书写
  66. * 1. 首先先遍历查找x
  67. * 2. 在x所对应的链表中查找y位置
  68. * 3. 判断值是否为0
  69. */
  70. ArrayList<LinkedList> array_head = new ArrayList<LinkedList>(10);
  71. LinkedList linkedList = array_head.get(x);
  72. Node value = (Node)linkedList.get(y);
  73. if (value.getValue()!=0){
  74. return true;
  75. }
  76. return false;
  77. }
  78. /**
  79. * 稀疏矩阵没有理解太清楚,随后补上
  80. */
  81. }