文章目录
-
- [C语言][LeetCode][169] Majority Element
- [C语言][LeetCode][189] Rotate Array
- [C语言][LeetCode][206] Reverse Linked List
- [C语言][LeetCode][217] Contains Duplicate
- [C语言][LeetCode][219] Contains Duplicate II
- [C语言][LeetCode][234] Palindrome Linked List
- [C语言][LeetCode][237] Delete Node in a Linked List
- [C语言][LeetCode][268] Missing Number
- [C语言][LeetCode][283]Move Zeroes
- [C语言][LeetCode][344] Reverse String
- [C语言][LeetCode][345] Reverse Vowels of a String
- [C语言][LeetCode][383] Ransom Note
- [C语言][LeetCode][387] First Unique Character in a String
- [C语言][LeetCode][485] Max Consecutive Ones
- [C语言][LeetCode][551] Student Attendance Record I
- [C语言][LeetCode][561] Array Partition I
- [C语言][LeetCode][657] Judge Route Circle
[C语言][LeetCode][169] Majority Element
题目链接:https://leetcode.com/problems/majority-element/description/
题目意思是给定一个数组,找出一个出现次数大于n/2的数,题目假设一定有这个数且数组非空。
//解题思路:做减法操作,即比较两个数,不相同,就忽略,相同,则计数,那么最后剩下来的那个,就是你要的数。
#include <stdio.h>
int majorityElement(int* nums, int numsSize)
{
int i, num;
int count = 0;
for (i = 0; i<numsSize; i++)
{
if (count == 0)
{
num = nums[i];
count++;
}
else
{
if (num == nums[i])
count++;
else
count--;
}
}
return num;
}
int main()
{
int nums[] = {2,3,6,2,2,2};
int numsSize = sizeof(nums) / sizeof(nums[0]);
int num = majorityElement(nums,numsSize);
printf("%d\n",num);
return 0;
}
[C语言][LeetCode][189] Rotate Array
题目链接:https://leetcode.com/problems/rotate-array/description/
题目意思是给定一个整形数组,传入一个k参数,另其旋转k次,有点类似队列的感觉,从队头出去K个,然后按照顺序再插入到队尾。
//解题思路:
//仔细观察题目给的例子,可以看出
//| —— - | 开始位置 | 现在位置 |
//| 数字7 | —–7—– | —–3—– |
//| 数字6 | —–6—– | —–2—– |
//| 数字5 | —–5—– | —–1—– |
//从上面关系,可以看出cur_location = origin_location + k - array_size
//因为超出了array_size之后,会重新从队头插入,所以上面的等式变成如下关系
//cur_location = (origin_location + k )% array_size
#include <stdio.h>
void rotate(int* nums, int numsSize, int k) {
int i = 0;
int *array = (int *)malloc(sizeof(int)*numsSize);
for (i = 0; i<numsSize; i++)
{
array[(i + k) % numsSize] = nums[i];
}
for (i = 0; i<numsSize; i++)
nums[i] = array[i];
free(array);
}
int main()
{
int nums[] = {1,2,3,4,5,6,7};
int numsSize = 7;
int k = 4;
rotate(nums,numsSize,k);
for (int i=0;i<numsSize;i++)
{
printf("%d ",nums[i]);
}
return 0;
}
[C语言][LeetCode][206] Reverse Linked List
题目链接:https://leetcode.com/problems/reverse-linked-list/description/
//题目意思是将给定链表反转
//解题思路:
如下图给定一个存放5个数的链表。
???
首先对于链表设置两个指针:
然后依次将旧链表上每一项添加在新链表的后面,然后新链表的头指针NewH移向新的链表头,如下图所示。此处需要注意,不可以上来立即将上图中P->next直接指向NewH,这样存放2的地址就会被丢弃,后续链表保存的数据也随之无法访问。而是应该设置一个临时指针tmp,先暂时指向P->next指向的地址空间,保存原链表后续数据。然后再让P->next指向NewH,最后P=tmp就可以取回原链表的数据了,所有循环访问也可以继续展开下去。
指针继续向后移动,直到P指针指向NULL停止迭代。
最后一步:
#include <stdio.h>
struct ListNode
{
int val;
struct ListNode *next;
};
struct ListNode* reverseList(struct ListNode* head) {
if (NULL == head) {
return head;
}
struct ListNode* p = head, *newH = NULL;
while (p != NULL) //一直迭代到链尾
{
struct ListNode* tmp = p->next; //暂存p下一个地址,防止变化指针指向后找不到后续的数
p->next = newH; //p->next指向前一个空间
newH = p; //新链表的头移动到p,扩长一步链表
p = tmp; //p指向原始链表p指向的下一个空间
}
return newH;
}
int main()
{
struct ListNode a, b, c;
a.val = 1;
b.val = 3;
c.val = 2;
a.next = &b;
b.next = &c;
c.next = NULL;
struct ListNode *head;
head = &a;
//打印原链表
struct ListNode *q = head;
do
{
printf("%d ", q->val);
q = q->next;
} while (q != NULL);
printf("\n");
struct ListNode*p = reverseList(head);
//打印反转后的链表
do
{
printf("%d ",p->val);
p = p->next;
} while (p != NULL);
return 0;
}