数据结构
韩韩韩韩韩美丽
这个作者很懒,什么都没留下…
展开
-
数据结构 排序 基数排序
#include <stdio.h>#include <stdlib.h>#include <string.h>#define lowbit(x) (x & 0xffff)#define highbit(x) ((x >> 16) & 0xffff)#define swap(a,b){ \ __typeof(a) _...原创 2018-07-28 12:15:51 · 110 阅读 · 0 评论 -
LeetCode 有效的括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.An input string is valid if: Open brackets must be closed by the same type of...原创 2018-07-20 16:05:35 · 179 阅读 · 0 评论 -
LeetCode 二叉树路径257
Given a binary tree, return all root-to-leaf paths.Note: A leaf is a node with no children.Example:Input: 1 / \2 3 \ 5Output: ["1->2->5", "1->3"]Explanation: All ...原创 2018-07-25 18:01:09 · 213 阅读 · 0 评论 -
数据结构 树与二叉树
一.树结点的数量=边数+1;二.二叉树1.每个结点的度最多为22.度为0的结点比度为2的结点多1个 结点的个数 = 边的个数 + 1; N0 + N1 + N2 = N1 + 2 * N2 + 1; N0 = N2 + 1;3.前序遍历 根 左 右中序遍历 左 根 右后序遍历 左 右 根(逆波兰表达式容易计算)4.删除度为2的结点转化为删除度为0的结点;#include <stdio.h&g...原创 2018-07-14 15:42:54 · 229 阅读 · 0 评论 -
数据结构 栈与队列
一.队列FIFO操作系统是分时系统,分时系统有队列,给进程排队(优先队列)先入先出适合处理具有先后顺序的问题; 【循环队列】树的层序遍历 广度优先搜索 【单调队列】区间的最大(小)值队列的结构定义:需要连续的存储区,任意类型的指针(内建类型int ,float,其他类型:struct,里面也可以存放链表)head:队列头指针;tail:位置不是固定的,可以指向第一个为空...原创 2018-07-14 12:28:15 · 169 阅读 · 0 评论 -
LeetCode 路径总和112
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.Note: A leaf is a node with no children.Example:...原创 2018-07-25 11:01:51 · 257 阅读 · 0 评论 -
数据结构 顺序表与链表
数据元素是数据的基本单位 数据元素不能再分 原子项 数据元素可以再分 组合项若干数据项组成一个数据元素数据项是数据的最小单位算法 :有穷性 确切性 输入项 输出项 可行性时间复杂度 : O(1) O(logn) O(n) O(nlogn)O(n2)O(n3)O(2n)O(n!)线性表中所有元素的数据类型都是相同的一:综述程序 = 算法 + 数据结构数据结构 = 结构定义 + 结构操作*1.顺序...原创 2018-07-13 17:22:35 · 309 阅读 · 0 评论 -
LeetCode 平衡二叉树 110
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as:a binary tree in which the depth of the two subtrees of every node never diff...原创 2018-07-25 10:05:39 · 148 阅读 · 0 评论 -
LeetCode 两数之和(无序版)
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same ...原创 2018-07-19 09:42:09 · 171 阅读 · 0 评论 -
LeetCode count and say 38
count-and-say序列是整数序列,前五个术语如下:1. 12. 113. 214. 12115. 1112211作为"one 1"或读出11。11作为"two 1s"或读出21。21被读出"one 2,然后one 1"或1211。给定整数n,生成count-and-say序列的第n项。注意:整数序列的每个术语将表示为一个字符串。例1:输入: 1 输出:...原创 2018-07-29 12:36:47 · 115 阅读 · 0 评论 -
数据结构 堆与优先队列
完全二叉树除了最后一层,其余层都是满二叉树;最后一层只能是右侧缺失1.编号为i的子结点:左孩子编号2i;右孩子编号2 * i + 1(从1开始编号) ***[方便] 编号为i的子结点:左孩子编号2i + 1;右孩子编号2 * i + 2(从0开始编号)2.可用连续空间存储(数组),由于编号连续[通过计算得到孩子结点的编号,节省存储空间];堆大顶堆:二叉树中每三个单元看作一个元素,父结点的值永远大...原创 2018-07-15 18:16:16 · 97 阅读 · 0 评论 -
LeetCode Merge Two Sorted Lists21
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.Example:Input: 1->2->4, 1->3->4Output: 1-...原创 2018-07-29 13:26:07 · 105 阅读 · 0 评论 -
Two Sum II
÷Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers...原创 2018-07-31 10:46:34 · 151 阅读 · 0 评论 -
LeetCode 相同的树100
Given two binary trees, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical and the nodes have the same value.Example ...原创 2018-07-23 09:29:56 · 218 阅读 · 0 评论 -
LeetCode 搜索插入位置 35
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array....原创 2018-07-22 20:06:15 · 143 阅读 · 0 评论 -
LeetCode 两个排序数组的中位数4
There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).Example 1:nums1 = [1, 3...原创 2018-07-22 20:04:24 · 145 阅读 · 0 评论 -
LeetCode Kth Smallest Element in a Sorted Matrix378
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.Note that it is the kth smallest element in the sorted order, not t...原创 2018-07-29 19:38:38 · 200 阅读 · 0 评论 -
LeetCode First Bad Version278
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the...原创 2018-07-29 16:02:21 · 175 阅读 · 0 评论 -
LeetCode Contains Duplicate217
Given an array of integers, find if the array contains any duplicates.Your function should return true if any value appears at least twice in the array, and it should return false if every element i...原创 2018-07-29 15:23:24 · 143 阅读 · 0 评论 -
LeetCode Merge Sorted Array 88
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note: The number of elements initialized in nums1 and nums2 are m and n respectively. You may assu...原创 2018-07-29 14:43:25 · 163 阅读 · 0 评论 -
LeetCode 二进制搜索树的最低共同祖先235
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between ...原创 2018-07-24 17:25:33 · 285 阅读 · 0 评论 -
LeetCode 二叉树的最小深度 111
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.Note: A leaf is a node with no childre...原创 2018-07-24 16:46:18 · 153 阅读 · 0 评论 -
LeetCode删除链表中的结点
1.增加虚拟头节点,存在头节点的删改时操作简单2.设置当前结点current_node,若current_node->next ->val = val,删除current_node->next,否则后移 /** * Definition for singly-linked list. * struct ListNode { * int val; ...原创 2018-06-26 21:44:30 · 87 阅读 · 0 评论 -
LeetCode快乐数
思路:1.快乐数可以用链表判断环实现2.每个数字是链表的一个结点3.运算一次,指向next4.判断是否有环,有环则证明是快乐数int next_num(int x){ int ret = 0,temp; while(x){ temp = x % 10; ret += temp *temp; x /= 10...原创 2018-06-26 21:36:12 · 227 阅读 · 0 评论 -
LeetCode相交链表
1.分别计算A、B链表的长度 countA、countB2.求A、B链表的差值index = abs(countA - countB),哪个链表长哪个链表先走index步3.A、B链表同时走,若走到相同结点,说明有相交结点/** * Definition for singly-linked list. * struct ListNode { * int va...原创 2018-06-26 21:30:03 · 133 阅读 · 0 评论 -
LeetCode环形链表
给定一个链表,判断链表中是否有环。进阶:你能否不使用额外空间解决此题?思路:1.设置快慢指针,快指针每次走两步,慢指针每次走一步2.若两指针相遇,有环存在/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; ...原创 2018-06-26 21:23:40 · 407 阅读 · 0 评论 -
LeetCode删除排序链表中的重复元素
1.判断val值是否相等2.相等时删除重复元素/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* deleteDuplicates(struct ListNode* ...原创 2018-06-26 21:18:28 · 95 阅读 · 0 评论 -
LeetCode交换链表相邻结点
不对结点的val值作改动,而是交换结点增加虚拟头节点减少代码量/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* swapPairs(struct ListNode* ...原创 2018-06-26 21:06:19 · 150 阅读 · 0 评论 -
LeetCode删除链表的倒数第n个结点
1.首先遍历一遍链表,用count计数记录一共有多少个结点2.第二遍便利链表,用count1找到删除结点的位置3.删除结点/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ...原创 2018-06-26 20:54:22 · 110 阅读 · 0 评论 -
链表的简单的实现
#include <stdio.h>#include <stdlib.h>#include <time.h>typedef struct LinkNode { int data; struct LinkNode *next; } LinkNode;typedef struct LinkList { LinkNode *head; int ...原创 2018-06-26 20:40:15 · 115 阅读 · 0 评论 -
LeetCode反转链表
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode* reverseList(struct ListNode* head) { struct ListNode * ...原创 2018-06-26 21:58:25 · 439 阅读 · 0 评论 -
LeetCode回文链表
1.设置快指针fast慢指针slow找到链表的中间结点2.两个指针同时从头节点和中间结点的next结点遍历3.val值相同就是回文链表/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */b...原创 2018-06-27 09:52:50 · 211 阅读 · 0 评论 -
数据结构 排序 稳定排序
稳定排序:插入排序、冒泡排序、归并排序如果有两个数字的数值大小相同,排序后的相对顺序为被改变。一.插入排序时间复杂度:n^21.将数组分成【已排序区】和【待排序区】2.将【已排序区】后面一个元素,向前插入到【待排序区】中3.直到【待排序区】没有元素为止二.冒泡排序(非随机存储的排序)时间复杂度:n^2将数组分成【已排序区】和【待排序区】2.从头到尾扫面【待排...原创 2018-07-16 16:39:28 · 887 阅读 · 0 评论 -
LeetCode 反转二叉树 226
Invert a binary tree.Example:Input: 4 / \ 2 7 / \ / \1 3 6 9Output: 4 / \ 7 2 / \ / \9 6 3 1思路1.如果树的根为空,直接返回NULL即可;2.当根的左子树或者右子树存在的时候,将左...原创 2018-07-24 16:29:06 · 141 阅读 · 0 评论 -
LeetCode 两书之和(无序版)
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same ...原创 2018-07-19 09:18:10 · 194 阅读 · 0 评论 -
LeetCode 没有重复字符的最长子串
Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "b", with...原创 2018-07-18 14:37:27 · 139 阅读 · 0 评论 -
LeetCode 树的最大深度104
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.Note: A leaf is a node with no childre...原创 2018-07-23 15:04:33 · 148 阅读 · 0 评论 -
LeetCode 对称树 101
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3 4 4 3B...原创 2018-07-23 10:47:59 · 172 阅读 · 0 评论 -
数据结构 查找 哈希表
哈希:从高维空间【信息量更大的空间】到低维空间【所能容纳信息量较少的空间】的映射(会发生冲突)冲突处理方法:4种结构定义:size:数组大小data_type:任意类型映射到整型提供数组下标找值时间复杂度O(1)#include <stdio.h>#include <stdlib.h>#include <string.h>t...原创 2018-07-17 14:14:36 · 122 阅读 · 0 评论 -
数据结构 查找 二分查找与三分查找
有序条件下,才能查找一.二分查找(具有单调性,不一定严格单调)(min 与max 是查找区间,通过不断缩小查找区间,来查找x的值)min是头指针; max是尾指针;mid = (min + max) / 2;终止条件:min >= max如果arr[mid] < x, min = mid + 1;如果arr[mid] > x, max = mid - ...原创 2018-07-17 10:14:33 · 2011 阅读 · 0 评论