Letcode刷题

1、二叉树

2、字典序

3、二分法

4、快排

5、分块运算

6、线段树

7、链表的运算

BM1 反转链表

struct ListNode {
int val;
struct ListNode *next;
};

 /**
  * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
  *
  *
  * @param head ListNode类
  * @return ListNode类
  */
struct ListNode* ReverseList(struct ListNode* head) {
	struct ListNode *pHead = head;
	if (pHead == NULL) {
		return NULL;
	}
	struct ListNode *pNext = pHead->next;
	struct ListNode *current = NULL;
	while (pNext) {
		pHead->next = current;
		current = pHead;
		pHead = pNext;
		pNext = pNext->next;
	}
	pHead->next = current;
	return pHead;
}

BM2 链表内指定区间反转

#include "pch.h"
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
int startpos, endpos;
struct ListNode* newNext;
 /**
  * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
  *
  *
  * @param head ListNode类
  * @return ListNode类
  */
struct ListNode* ReverseList(struct ListNode* head, int num) {
	struct ListNode *pHead = head;
	if (pHead == NULL) {
		return NULL;
	}
	struct ListNode *pNext = pHead->next;
	struct ListNode *current = NULL;
	while (pNext) {
		if (num == endpos - startpos+1) {
			newNext = pNext;
			pHead->next = current;
			return pHead;
		}
		pHead->next = current;
		current = pHead;
		pHead = pNext;
		pNext = pNext->next;
		num++;
	}
	newNext = NULL;
	pHead->next = current;
	return pHead;
}

int main(void)
{
	int i, j, n;
	struct ListNode* p2 = NULL;
	scanf("%d%d%d", &n,&startpos, &endpos);
	struct ListNode *p = (struct ListNode *)malloc(sizeof(struct ListNode));
	memset(p, 0, sizeof(p));
	struct ListNode *head = p;
	p->next = NULL;
	p->val = 0;
	for (i = 1; i <= n; i++) {
		struct ListNode *t = (struct ListNode *)malloc(sizeof(struct ListNode));
		memset(t, 0, sizeof(t));
		t->next = NULL;
		t->val = i;
		p->next = t;
		p = p->next;
	}
	head = head->next;
	struct ListNode *sReverse = head;
	for (i = 1; i < startpos; i++) {
		sReverse = sReverse->next;
	}
	if (startpos > 1) {
		struct ListNode *sAfter = sReverse->next;
		sReverse->next = ReverseList(sReverse->next, 0);
		sAfter->next = newNext;
	}
	else {
		struct ListNode *sAfter = sReverse;
		head = ReverseList(sReverse, 0);
		sAfter->next = newNext;
	}
	while (head != NULL) {
		printf("%d", head->val);
		head = head->next;
	}
	return 0;
}
#endif

8:HASH

#define MAXN 500005
long long m, n;
struct node {
	int pos;
	long long  value;
	struct node *next;
};
struct node array[MAXN];
void createHash(long  long  num, int apos)
{
	int tmppos;
	tmppos = num % MAXN;
	if (array[tmppos].value == 0) {
		array[tmppos].value = num;
		array[tmppos].pos = apos;
	}
	else {
		struct node * tmpp = (struct node *)malloc(sizeof(struct node));
		tmpp->value = num;
		tmpp->pos = apos;
		tmpp->next = NULL;
		while (array[tmppos].next != NULL) {
			array[tmppos].next = array[tmppos].next->next;
		}
		array[tmppos].next = tmpp;
	}
   // printf("liming11111111111\n");
}
int* twoSum(int* numbers, int numbersLen, int target, int* returnSize) {
	int i, j, k;
	int flag = 0;
	int secondnum = 0;
	for (i = 0; i < MAXN; i++) {
		array[i].pos = 0;
		array[i].next = NULL;
		array[i].value = 0;
	}
	for (i = 0; i < numbersLen; i++) {
		createHash(numbers[i] + 10, i);
	}
    printf("liming2222222222222222\n");
	for (i = 0; i < numbersLen; i++) {
        printf("liming33333333\n");
		long long  test;
		long long  ans;
		int flag = 0;
		test = target - numbers[i] + 10;
		ans = test % MAXN;
		struct node * tmpp = &array[ans];
		while (tmpp != NULL) {
			if (tmpp->value == test && tmpp->pos != i) {
				secondnum = tmpp->pos;
				flag = 1;
				break;
			}
			tmpp = tmpp->next;
		}
        if (flag == 1) {
			break;
		}
	}
    printf("%d %d\n",i, secondnum);
    printf("liming\n");
    *returnSize = 2;
    int *output = (int *)malloc(sizeof(int) * 2);
	output[0] = i + 1;
	output[1] = secondnum+1;
	return output;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值