elem

数据结构:线性结构之线性表

我们两清 提交于 2020-01-15 18:57:58
就算没有天分,只要你愿意每天花一点时间,做同样一件事情,不知不觉间,你就会走得很远。 什么是线性表? 线性表是n(n>= 0)个元素的有限序列。在表中,元素之间存在这线性的逻辑关系: (1)表中有且仅有一个开始结点; (2)有且仅有一个终端结点; (3)除开始结点外,表中的每一个结点均只有一个前驱结点; (4)除终端结点外,表中的每一个结点均只有一个后继结点; 根据他们之间的关系可以排成一个现线性序列,记作:(a1,a2,...,an) 例如:26个英文字母表(A,B,C,...,X,Y,Z)就是一个线性表 这里的ai(1<=i<=n)属于同一数据对象,具有相同的数据类型。线性表中的数据元素 个数n就是线性表的长度,称作表长,n=0时为空表。i则是数据元素ai的位序。 线性表的存储方式 线性表有两种存储方式:顺序存储和链式存储 (一)顺序表 顺序存储是在内存中用一块地址连续的存储空间按顺序存储线性表的各个数据元素。 采用顺序存储结构的性表称为顺序表,顺序表中逻辑上相邻的数据元素在物理存储位置上也是相邻的。 只要确定了存储线性表的起始位置,线性表中的任意一个数据都可随机存取: 第i个元素的地址为:Loc(ai) = Loc(a1) + size * (i - 1) 其中size是每个元素所占的空间大小。 C语言版的实现 #include <stdio.h> #include

找出数组第k小的元素

*爱你&永不变心* 提交于 2020-01-14 01:48:30
解法 首先容易想到的做 k k k 次冒泡, 复杂度为 O ( n k ) O(nk) O ( n k ) 或者先排序再找第 k k k 个元素,复杂度为 O ( n log ⁡ n ) O(n\log n) O ( n lo g n ) 建立 k k k 个元素的大根堆,遍历后续 n − k n-k n − k 个元素,如果比根小就替换根, 复杂度 O ( n log ⁡ k ) O(n\log k) O ( n lo g k ) 利用分割技术 (partitioning),复杂度为 O ( n log ⁡ k ) O(n\log k) O ( n lo g k ) 迭代形式 import random def kthSmallest ( data , k ) : "Find the nth rank ordered element (the least value has rank 0)." data = list ( data ) if not 0 <= k < len ( data ) : raise ValueError ( 'not enough elements for the given rank' ) while True : pivot = random . choice ( data ) pcount = 0 low , high = [ ] , [ ]

LeetCode Problem 136:Single Number

主宰稳场 提交于 2019-12-26 02:09:40
描述:Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 题目要求O(n)时间复杂度,O(1)空间复杂度。 思路1:初步使用暴力搜索,遍历数组,发现两个元素相等,则将这两个元素的标志位置为1,最后返回标志位为0的元素即可。时间复杂度O(n^2)没有AC,Status:Time Limit Exceed 1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 5 vector <int> flag(n,0); 6 7 for(int i = 0; i < n; i++) { 8 if(flag[i] == 1) 9 continue; 10 else { 11 for(int j = i + 1; j < n; j++) { 12 if(A[i] == A[j]) { 13 flag[i] = 1; 14 flag[j] = 1; 15 } 16 }

待办事项

萝らか妹 提交于 2019-12-25 22:26:39
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>待办事项</title> <style type="text/css"> *{ padding: 0; margin: 0; } </style> </head> <body> <h1>My To-Do List</h1> <input type="text" placeholder="New item" /> <button id="add">Add</button> <button id="sum">count</button> <ol id="my_list"></ol> <script type="text/javascript" src="jquery-3.4.1min.js"></script> <script type="text/javascript"> $(function() { $("#add").on("click", function() { var val = $("input").val(); if(val !== '') { var elem = $("<li></li>").text(val); $(elem).append("<button class='rem'>X</button>"); $("#my

Layui 表单 多选模拟单选,checkbox实现radio效果

Deadly 提交于 2019-12-24 06:08:24
< ! DOCTYPE html > < html lang = "zh" > < head > < meta charset = "UTF-8" > < title > Layui 表单 多选模拟单选,checkbox实现radio效果 < / title > < link rel = "stylesheet" href = "../assets/libs/layui/css/layui.css" media = "all" > < / head > < body > < div class = "layui-form" > < br > < div class = "layui-form-item" > < label class = "layui-form-label" > 选一个你的兴趣爱好 < / label > < div class = "layui-input-block" isradio = "true" > < input type = "checkbox" lay - skin = "primary" lay - filter = "cb_r" title = "唱" > < input type = "checkbox" lay - skin = "primary" lay - filter = "cb_r" title = "跳" > < input

golang的channel实现

夙愿已清 提交于 2019-12-23 23:41:27
golang 的 channel 实现位于 src/runtime/chan.go 文件。 golang 中的 channel 对应的结构是 : // Invariants: // At least one of c.sendq and c.recvq is empty, // except for the case of an unbuffered channel with a single goroutine // blocked on it for both sending and receiving using a select statement, // in which case the length of c.sendq and c.recvq is limited only by the // size of the select statement. // // For buffered channels, also: // c.qcount > 0 implies that c.recvq is empty. // c.qcount < c.dataqsiz implies that c.sendq is empty. type hchan struct { qcount uint // total data in the queue dataqsiz uint

Jquery需要掌握的技巧

折月煮酒 提交于 2019-12-18 19:48:37
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 检查 jQuery 是否加载 在使用 jQuery 进行任何操作之前,你需要先确认它已经加载: if (typeof jQuery == 'undefined') { console.log('jQuery hasn\'t loaded'); } else { console.log('jQuery has loaded'); } 返回顶部按钮 利用 jQuery 中的 animate 和 scrollTop 方法,你无需插件就可以创建简单的 scroll up 效果: // 返回顶部$('a.top').click(function (e) { e.preventDefault(); $(document.body).animate({scrollTop: 0}, 800); }); <!-- 设置锚 --><a class="top" href="#">Back to top</a> 调整 scrollTop 的值即可改变滚动着陆位置。你实际所做的是在 800 毫秒内不断设置文档主体的位置,直到它滚动到顶部。 预加载图片 如果你的网页使用了大量并非立即可见的图片(例如悬停鼠标触发的图片),那么预加载这些图片就显得很有意义了: $.preloadImages = function () { for (var

Revit 二次开发—隐藏组

对着背影说爱祢 提交于 2019-12-09 04:18:58
public static bool HideGroupById(Document doc, int groupidnum, bool n) { ElementId groupid = new ElementId(groupidnum); try { Element elem = doc.GetElement(groupid); Group g = elem as Group; IList<ElementId> ids = g.GetMemberIds(); if (n == true) { doc.ActiveView.HideElements(ids); return true; } else { doc.ActiveView.UnhideElements(ids); return true; } } catch (Exception ex) { return false; } } public static void HideGroupsById(Document doc, List<int> groupidnums) { foreach (var idnum in groupidnums) { ElementId groupid = new ElementId(idnum); Element elem = doc.GetElement(groupid); Group g =

将伪数组转为真正的数组

杀马特。学长 韩版系。学妹 提交于 2019-12-07 15:51:34
将伪数组转为真正的数组 2018.08.14 21:06:53字数 90阅读 2321 伪数组转为真数组 对DOM元素进行map、forEach操作时候需要进行遍历,伪数组遍历会报错:'elem.map is not a function',为了避免这个问题,需要进行转换。 (1) ES5 转为真数组 Array.prototype.slice.call(元素对象) let elem1 = Array.prototype.slice.call(elem) (2) ES6 转为真数组 Array.from(元素对象) let elem2 = Array.from(elem) (3) 例子 <ul> <li class="a" ></li> <li class="a" ></li> <li class="a" ></li> <li class="a" ></li> <li class="a" ></li> </ul> <script> /* 获取li元素*/ let elem = document.getElementsByClassName('a') /* 这样写是错误的,因为是伪数组*/ elem.map((item,index,elem) => { console.log(item +'---'+index + '----'+elem) /*elem.map is not a

二叉树(python实现)

非 Y 不嫁゛ 提交于 2019-12-06 04:17:11
二叉树的遍历和添加结点 class Node(): def __init__(self, item): self.elem = item self.l_child = None self.r_child = None class Tree(): def __init__(self): self.root = None def add(self, elem): """为树添加节点""" node = Node(elem) # 如果树是空的,则对根节点赋值 if self.root is None: self.root = node return else: queue = [] queue.append(self.root) # 对已有节点进行层次遍历 while queue: # 弹出队列的第一个元素 cur = queue.pop(0) if cur.l_child == None: cur.l_child = node return elif cur.r_child == None: cur.r_child = node return else: # 如果左右子树都不为空,加入队列继续判断 queue.append(cur.l_child) queue.append(cur.r_child) def preorder(self, root): if root == None: