1.解決問(wèn)題方法的效率,跟數(shù)據(jù)的組織方式是有關(guān)系的,比如圖書(shū)館借書(shū),需要刷門(mén)禁,找書(shū)架,拿到書(shū)找管理員登記等等流程
2.解決問(wèn)題方法的效率,和空間的利用率有關(guān)系 (遞歸打印1~100000(N), 方法一是循環(huán),方法二是遞歸胀莹,結(jié)果爆掉了直接輸出100000)
3.解決問(wèn)題方法的效率,也和算法的巧妙運(yùn)用程度有關(guān)系
數(shù)據(jù)結(jié)構(gòu)+算法=程序
抽象數(shù)據(jù)類(lèi)型(ATD)
1.抽象具有普適性,不會(huì)因?yàn)槲抑皩W(xué)的是C語(yǔ)言憨降,換成JAVA,PHP就不適用了。
2.抽象來(lái)源于實(shí)際该酗,高于實(shí)際授药,通過(guò)從一個(gè)問(wèn)題抽象出普適的理論,就可以解決其他相通的問(wèn)題
JavaScript基礎(chǔ)
操作符
算數(shù)操作符呜魄、賦值操作符悔叽、比較操作符、邏輯操作符爵嗅、位操作符娇澎、一元操作符和其他操作符 typeof,delete
真值和假值
在大多數(shù)編程語(yǔ)言中,布爾值true和false僅僅表示true/false
數(shù)值類(lèi)型 | 轉(zhuǎn)換成布爾值
-|-|-
undefined | false
null | false
布爾值 | true就是true,false就是false
數(shù)字 | +0,-0或NaN都是false,其他都是true
字符串 | 字符串為空(length為0)就是false,其他true
對(duì)象 | true
function testTruthy(val){
return val ? console.log('truthy') : console.log('falsy');
}
相等操作符
類(lèi)型x | 類(lèi)型y | 結(jié)果
-|-|-|-
null | undefined | true
undefined | null | true
數(shù)字 | 字符串 | x==toNumber(y)
字符串 | 數(shù)字 | toNumber(x)==y
布爾值 | 任何類(lèi)型 | toNumber(x)==y
任何類(lèi)型 | 布爾值 | x==toNumber(y)
字符串或數(shù)字 | 對(duì)象 | x == toPrimitive(y)
對(duì)象 | 字符串或數(shù)字 | toPrimitive(x) == y
如果x和y是相同類(lèi)型睹晒,JavaScript會(huì)比較它們的值或?qū)ο笾堤俗F渌麤](méi)有列在這個(gè)表格中的情況都會(huì)返回false括细。
條件語(yǔ)句
循環(huán)
do...while循環(huán)和while循環(huán)很相似。區(qū)別是
//在while循環(huán)里戚啥,先進(jìn)行條件判斷再執(zhí)行循環(huán)體中的代碼勒极,
var i = 0;
while (i < 10) {
console.log(i);
i++;
}
//而在do...while循環(huán)里,是先執(zhí)行循環(huán)體中的代碼再判斷循環(huán)條件
var i = 0;
do {
console.log(i);
i++;
} while (i < 10)
類(lèi)
function Book(title, pages, isbn){ //{1}
this.title = title;
this.pages = pages;
this.isbn = isbn;
}
Book.prototype.printTitle = function(){
console.log(this.title);
};
//我們可以用ES6把語(yǔ)法簡(jiǎn)化如下:
class Book { //{2}
constructor (title, pages, isbn) {
this.title = title;
this.pages = pages;
this.isbn = isbn;
}
printIsbn(){
console.log(this.isbn);
}
}
class ITBook extends Book { //{1}
constructor (title, pages, isbn, technology) {
super(title, pages, isbn); //{2}
this.technology = technology;
}
printTechnology(){
console.log(this.technology);
}
}
let jsBook = new ITBook('學(xué)習(xí)JS算法', '200', '1234567890', 'JavaScript');
console.log(jsBook.title);
console.log(jsBook.printTechnology());
棧
/**
* 堆棧:是具有一定操作約束的線(xiàn)性表虑鼎,只在一端做插入和刪除
* 案例說(shuō)明:日常使用的是中綴表達(dá)式辱匿,而后綴表達(dá)式是利用堆棧
* 特點(diǎn):后進(jìn)先出 LIFO,新插入的元素在棧頂,舊元素接近棧底
* 抽象描述:
* 1. 生成空的堆棧炫彩,最大長(zhǎng)度: createStack , maxSize
* 2. 判斷堆棧是否已滿(mǎn) isFull()
* 3. 壓入堆棧 push()
* 4. 判斷堆棧是否為空 isEmpty()
* 5. 刪除并返回棧頂元素 Pop()
*
* 以下是基于ES5實(shí)現(xiàn)
*
*/
function Stack() {
let items = []; //私有變量匾七,多個(gè)Stack實(shí)例會(huì)創(chuàng)建多個(gè)items副本
this.size = function () {
return items.length;
}
this.push = function (element) { //向棧添加元素
items.push(element);
},
this.pop = function () { //從棧中移除元素
return items.pop();
},
this.peek = function () { //查看棧頂元素
return items[items.length - 1];
},
this.isEmpty = function () { //檢查棧是否為空
return items.length == 0;
},
this.clear = function () { //清空棧
items = []
},
this.print = function () {
console.log(items.toString())
}
}
let stack = new Stack();
console.log(stack);
stack.push('A')
stack.push('B')
stack.push('C')
stack.pop() //移除棧頂元素 C
stack.print() // A,B
console.log(`是否為空: ${stack.isEmpty()}`); // false
console.log(`Stack的size: ${stack.size()}`); // 2
console.log(`棧頂元素: ${stack.peek()}`); //此時(shí)棧頂元素 B
ES6實(shí)現(xiàn)的方式
//第一種方式Symbol
let _items = Symbol() //利用symbol創(chuàng)建私有屬性
class Stack {
constructor() {
this[_items] = [];
}
push(element) {
this[_items].push(element)
}
pop(){
this[_items].pop()
}
isEmpty(){
return this[_items].length===0
}
}
//第二種方式利用weakMap
let Stack = (function() {
const items = new WeakMap(); //weakMap可以存儲(chǔ)鍵值對(duì),其中鍵是對(duì)象江兢,值可以是任意數(shù)據(jù)類(lèi)型
class Stack {
constructor() {
items.set(this, []);
}
push(element) {
let s = items.get(this);
s.push(element);
}
pop() {
let s = items.get(this);
let r = s.pop();
return r;
}
isEmpty() {
return items.get(this).length === 0;
}
print() {
console.log(items.get(this));
}
}
return Stack; //返回類(lèi)的一個(gè)實(shí)例
})();
案例
function divideBy2(decNumber) {
var remStack = new Stack();
var rem = ""; //余數(shù)
var binaryString = ""; //轉(zhuǎn)成二進(jìn)制后的
while (decNumber > 0) {
rem = Math.floor(decNumber % 2);
remStack.push(rem);
decNumber = Math.floor(decNumber / 2);
}
while (!remStack.isEmpty()) {
binaryString += remStack.pop();
}
return binaryString;
}
console.log(divideBy2(100000));