【滴滴笔试】得到数组的最大子序列和

题目:得到一个数组的最大子序列和
 * 输入:-23 17 -7 11 -2 1 -34

 * 输出:21

子序列:连续的子序列,不能中间断开

解法:

用一次遍历,两个变量,算法复杂度为O(n)

1.遍历时,连续相加,如果相加结果大于0,则可以继续遍历相加,相加结果用变量cur保存

2.用变量max表示最大序列和,每次与cur比较,谁大赋值为max

3.如果cur小于0了,则不能继续用cur,因为这样会越加越小,则cur=0,再继续累加

import java.util.Scanner;
public class FIndMaxSubArraySum {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);	

		String str = null;

		while(scanner.hasNext()){
			str = scanner.nextLine();
			String[] str1 =  str.split(" ");
			int length = str1.length;
			int[] arr = new int[length];
			for(int i =0; i<length; i++){
				arr[i] = Integer.valueOf(str1[i]);
			}
			int max = maxSum(arr);
			System.out.println(max);
		}
	}
	
	public static int maxSum(int[] arr) {
		if (arr == null || arr.length == 0) {
			return 0;
		}
		int max = Integer.MIN_VALUE;
		int cur = 0;
		for (int i = 0; i != arr.length; i++) {
			cur += arr[i];
			max = Math.max(max, cur);
			cur = cur < 0 ? 0 : cur;
		}
		return max;
	}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 请编写一个函数,将一个数组转化成链表,并返回链表的头指针。 ```c #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node *next; } Node; Node* array_to_linkedlist(int arr[], int size) { Node *head = NULL, *tail = NULL; for (int i = 0; i < size; i++) { Node *node = (Node*)malloc(sizeof(Node)); node->data = arr[i]; node->next = NULL; if (head == NULL) { head = tail = node; } else { tail->next = node; tail = node; } } return head; } int main() { int arr[] = {1, 2, 3, 4, 5}; int size = sizeof(arr) / sizeof(arr[0]); Node *head = array_to_linkedlist(arr, size); // 遍历链表 Node *p = head; while (p != NULL) { printf("%d ", p->data); p = p->next; } return 0; } ``` 2. 请编写一个函数,判断一个链表是否是循环链表,如果是循环链表,返回 1,否则返回 0。 ```c #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node *next; } Node; int is_circular_linkedlist(Node *head) { if (head == NULL) { return 0; } Node *p = head, *q = head; while (q != NULL && q->next != NULL) { p = p->next; q = q->next->next; if (p == q) { return 1; } } return 0; } int main() { Node *head = (Node*)malloc(sizeof(Node)); head->data = 1; Node *p1 = (Node*)malloc(sizeof(Node)); p1->data = 2; head->next = p1; Node *p2 = (Node*)malloc(sizeof(Node)); p2->data = 3; p1->next = p2; Node *p3 = (Node*)malloc(sizeof(Node)); p3->data = 4; p2->next = p3; // 将链表变成循环链表 p3->next = head; printf("%d\n", is_circular_linkedlist(head)); return 0; } ``` 3. 请编写一个函数,实现队列的入队和出队操作。 ```c #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 100 typedef struct Queue { int data[MAX_SIZE]; int front, rear; } Queue; Queue* create_queue() { Queue *q = (Queue*)malloc(sizeof(Queue)); q->front = q->rear = 0; return q; } int is_full(Queue *q) { return (q->rear + 1) % MAX_SIZE == q->front; } int is_empty(Queue *q) { return q->front == q->rear; } void enqueue(Queue *q, int x) { if (is_full(q)) { printf("Queue is full.\n"); return; } q->data[q->rear] = x; q->rear = (q->rear + 1) % MAX_SIZE; } int dequeue(Queue *q) { if (is_empty(q)) { printf("Queue is empty.\n"); return -1; } int x = q->data[q->front]; q->front = (q->front + 1) % MAX_SIZE; return x; } int main() { Queue *q = create_queue(); enqueue(q, 1); enqueue(q, 2); enqueue(q, 3); printf("%d\n", dequeue(q)); printf("%d\n", dequeue(q)); printf("%d\n", dequeue(q)); printf("%d\n", dequeue(q)); return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值